class Tire::Results::Item

Public Class Methods

new(args={}) click to toggle source

Create new instance, recursively converting all Hashes to Item and leaving everything else alone.

# File lib/tire/results/item.rb, line 11
def initialize(args={})
  raise ArgumentError, "Please pass a Hash-like object" unless args.respond_to?(:each_pair)
  @attributes = {}
  args.each_pair do |key, value|
    if value.is_a?(Array)
      @attributes[key.to_sym] = value.map { |item| @attributes[key.to_sym] = item.is_a?(Hash) ? Item.new(item.to_hash) : item }
    else
      @attributes[key.to_sym] = value.is_a?(Hash) ? Item.new(value.to_hash) : value
    end
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/tire/results/item.rb, line 30
def [](key)
  @attributes[key]
end
class() click to toggle source

Let's pretend we're someone else in Rails

# File lib/tire/results/item.rb, line 60
def class
  defined?(::Rails) && @attributes[:_type] ? @attributes[:_type].camelize.constantize : super
rescue NameError
  super
end
errors() click to toggle source
# File lib/tire/results/item.rb, line 42
def errors
  ActiveModel::Errors.new(self)
end
id() click to toggle source
# File lib/tire/results/item.rb, line 34
def id
  @attributes[:_id] || @attributes[:id]
end
inspect() click to toggle source
# File lib/tire/results/item.rb, line 66
def inspect
  s = []; @attributes.each { |k,v| s << "#{k}: #{v.inspect}" }
  %Q<Item#{self.class.to_s == 'Tire::Results::Item' ? '' : " (#{self.class})"} #{s.join(', ')}>|
end
method_missing(method_name, *arguments) click to toggle source

Delegate method to a key in underlying hash, if present, otherwise return nil.

# File lib/tire/results/item.rb, line 26
def method_missing(method_name, *arguments)
  @attributes.has_key?(method_name.to_sym) ? @attributes[method_name.to_sym] : nil
end
persisted?() click to toggle source
# File lib/tire/results/item.rb, line 38
def persisted?
  !!id
end
to_hash() click to toggle source
# File lib/tire/results/item.rb, line 54
def to_hash
  @attributes
end
to_indexed_json(options=nil) click to toggle source
Alias for: to_json
to_json(options=nil) click to toggle source
# File lib/tire/results/item.rb, line 71
def to_json(options=nil)
  @attributes.to_json(options)
end
Also aliased as: to_indexed_json
to_key() click to toggle source
# File lib/tire/results/item.rb, line 50
def to_key
  persisted? ? [id] : nil
end
valid?() click to toggle source
# File lib/tire/results/item.rb, line 46
def valid?
  true
end