Class: HyperResource::Links

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource = nil) ⇒ Links

Returns a new instance of Links.



5
6
7
# File 'lib/hyper_resource/links.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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hyper_resource/links.rb', line 46

def method_missing(method, *args) # @private
  unless self[method]
    raise NoMethodError, "undefined method `#{method}' for #{self.inspect}"
  end

  if args.count > 0
    self[method].where(*args)
  else
    self[method]
  end
end

Instance Attribute Details

#_resourceObject

Returns the value of attribute _resource.



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

def _resource
  @_resource
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
43
44
# File 'lib/hyper_resource/links.rb', line 40

def [](key) # @private
  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



36
37
38
# File 'lib/hyper_resource/links.rb', line 36

def []=(attr, value) # @private
  super(attr.to_s, value)
end

#_hr_create_methods!(opts = {}) ⇒ Object

Creates accessor methods in self.class and self._resource.class. Protects against method creation into HyperResource::Links 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
31
32
33
34
# File 'lib/hyper_resource/links.rb', line 12

def _hr_create_methods!(opts={}) # @private
  return if self.class.to_s == 'HyperResource::Links' ||
            self._resource.class.to_s == 'HyperResource'

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

    self.class.send(:define_method, attr_sym) do |*args|
      if args.count > 0
        self[attr].where(*args)
      else
        self[attr]
      end
    end

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