Class: HyperResource::Attributes

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource = nil) ⇒ Attributes



6
7
8
# File 'lib/hyper_resource/attributes.rb', line 6

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Raises:

  • (NoMethodError)


72
73
74
75
# File 'lib/hyper_resource/attributes.rb', line 72

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

Instance Attribute Details

#_resourceObject



4
5
6
# File 'lib/hyper_resource/attributes.rb', line 4

def _resource
  @_resource
end

Instance Method Details

#[](key) ⇒ Object



66
67
68
69
70
# File 'lib/hyper_resource/attributes.rb', line 66

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



61
62
63
64
# File 'lib/hyper_resource/attributes.rb', line 61

def []=(attr, value) # @private
  _hr_mark_changed(attr)
  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::Attributes and HyperResource classes. Just subclasses, please!



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hyper_resource/attributes.rb', line 13

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

  self.keys.each do |attr|
    attr_sym = attr.to_sym
    attr_eq_sym = "#{attr}=".to_sym

    self.class.send(:define_method, attr_sym) do
      self[attr]
    end
    self.class.send(:define_method, attr_eq_sym) do |val|
      self[attr] = val
    end

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

  ## This is a good time to mark this object as not-changed
  _hr_clear_changed
end

#changed?(attr = nil) ⇒ Boolean

Returns true if the given attribute has been changed since creation time, false otherwise. If no attribute is given, return whether any attributes have been changed.



49
50
51
52
53
# File 'lib/hyper_resource/attributes.rb', line 49

def changed?(attr=nil)
  @_hr_changed ||= Hash.new(false)
  return @_hr_changed[attr.to_sym] if attr
  return @_hr_changed.keys.count > 0
end

#changed_attributesObject

Returns a hash of the attributes and values which have been changed since creation time.



57
58
59
# File 'lib/hyper_resource/attributes.rb', line 57

def changed_attributes
  @_hr_changed.select{|k,v| v}.keys.inject({}) {|h,k| h[k]=self[k]; h}
end