Class: HyperResource::Objects

Inherits:
Hash
  • Object
show all
Defined in:
lib/hyper_resource/objects.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource = nil) ⇒ Objects

Returns a new instance of Objects.



5
6
7
# File 'lib/hyper_resource/objects.rb', line 5

def initialize(resource=nil)
  self._resource = resource || HyperResource.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

:nodoc:

Raises:

  • (NoMethodError)


53
54
55
56
# File 'lib/hyper_resource/objects.rb', line 53

def method_missing(method, *args) # :nodoc:
  return self[method] if self[method]
  raise NoMethodError, "undefined method `#{method}' for #{self.inspect}"
end

Instance Attribute Details

#_resourceObject

Returns the value of attribute _resource.



3
4
5
# File 'lib/hyper_resource/objects.rb', line 3

def _resource
  @_resource
end

Instance Method Details

#[](key) ⇒ Object

:nodoc:



47
48
49
50
51
# File 'lib/hyper_resource/objects.rb', line 47

def [](key) # :nodoc:
  return super(key.to_s) if self.has_key?(key.to_s)
  return super(key.to_sym) if self.has_key?(key.to_sym)
  nil
end

#[]=(attr, value) ⇒ Object

:nodoc:



43
44
45
# File 'lib/hyper_resource/objects.rb', line 43

def []=(attr, value) # :nodoc:
  super(attr.to_s, value)
end

#create_methods!(opts = {}) ⇒ Object

Creates accessor methods in self.class and self._resource.class. Protects against method creation into HyperResource::Objects and HyperResource classes. Just subclasses, please!



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hyper_resource/objects.rb', line 12

def create_methods!(opts={})
  return if self.class.to_s == 'HyperResource::Objects' ||
            self._resource.class.to_s == 'HyperResource'

  self.keys.each do |attr|
    attr_sym = attr.to_sym

    self.class.send(:define_method, attr_sym) do
      self[attr]
    end

    ## Don't stomp on _resource's methods
    unless _resource.respond_to?(attr_sym)
      _resource.class.send(:define_method, attr_sym) do
        objects.send(attr_sym)
      end
    end
  end
end

#firstObject



34
35
36
# File 'lib/hyper_resource/objects.rb', line 34

def first
  self.first_orig[1][0]
end

#first_origObject

Returns the first item in the first collection in self.



33
# File 'lib/hyper_resource/objects.rb', line 33

alias_method :first_orig, :first

#ith(i) ⇒ Object

Returns the ith item in the first collection in self.



39
40
41
# File 'lib/hyper_resource/objects.rb', line 39

def ith(i)
  self.first_orig[1][i]
end