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.



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

def initialize(args={})
  if args.is_a? Hash
    args.each_pair do |key, value|
      self[key.to_sym] = value.is_a?(Hash) ? self.class.new(value) : value
    end
    super.replace self
  else
    super
  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.



23
24
25
# File 'lib/slingshot/results/item.rb', line 23

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

Instance Method Details

#inspectObject



27
28
29
30
# File 'lib/slingshot/results/item.rb', line 27

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