Class: Tire::Results::Item

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion
Defined in:
lib/tire/results/item.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Item

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

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object

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



26
27
28
# 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

Instance Method Details

#[](key) ⇒ Object



30
31
32
# File 'lib/tire/results/item.rb', line 30

def [](key)
  @attributes[key.to_sym]
end

#classObject

Let’s pretend we’re someone else in Rails



67
68
69
70
71
# File 'lib/tire/results/item.rb', line 67

def class
  defined?(::Rails) && @attributes[:_type] ? @attributes[:_type].camelize.constantize : super
rescue NameError
  super
end

#errorsObject



46
47
48
# File 'lib/tire/results/item.rb', line 46

def errors
  ActiveModel::Errors.new(self)
end

#idObject



34
35
36
# File 'lib/tire/results/item.rb', line 34

def id
  @attributes[:_id]   || @attributes[:id]
end

#inspectObject



73
74
75
76
# File 'lib/tire/results/item.rb', line 73

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

#persisted?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tire/results/item.rb', line 42

def persisted?
  !!id
end

#to_hashObject



58
59
60
61
62
63
# File 'lib/tire/results/item.rb', line 58

def to_hash
  @attributes.reduce({}) do |sum, item|
    sum[ item.first ] = item.last.respond_to?(:to_hash) ? item.last.to_hash : item.last
    sum
  end
end

#to_json(options = nil) ⇒ Object Also known as: to_indexed_json



78
79
80
# File 'lib/tire/results/item.rb', line 78

def to_json(options=nil)
  @attributes.to_json(options)
end

#to_keyObject



54
55
56
# File 'lib/tire/results/item.rb', line 54

def to_key
  persisted? ? [id] : nil
end

#typeObject



38
39
40
# File 'lib/tire/results/item.rb', line 38

def type
  @attributes[:_type] || @attributes[:type]
end

#valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/tire/results/item.rb', line 50

def valid?
  true
end