Class: Slingshot::Results::Item

Inherits:
Hash
  • Object
show all
Defined in:
lib/slingshot/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)


9
10
11
12
13
14
# File 'lib/slingshot/results/item.rb', line 9

def initialize(args={})
  raise ArgumentError, "Please pass a Hash-like object" unless args.respond_to?(:each_pair)
  args.each_pair do |key, value|
    self[key.to_sym] = value.respond_to?(:to_hash) ? self.class.new(value) : value
  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.



19
20
21
# File 'lib/slingshot/results/item.rb', line 19

def method_missing(method_name, *arguments)
  self.has_key?(method_name.to_sym) ? self[method_name.to_sym] : nil
end

Instance Method Details

#idObject

Get ID



25
26
27
# File 'lib/slingshot/results/item.rb', line 25

def id
  self[:id]
end

#inspectObject



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

def inspect
  s = []; self.each { |k,v| s << "#{k}: #{v.inspect}" }
  %Q|<Item #{s.join(', ')}>|
end