Class: HyperResource::Attributes
- Inherits:
-
Hash
- Object
- Hash
- HyperResource::Attributes
- Defined in:
- lib/hyper_resource/attributes.rb
Instance Attribute Summary collapse
-
#_resource ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#[](key) ⇒ Object
:nodoc:.
-
#[]=(attr, value) ⇒ Object
:nodoc:.
-
#changed?(attr = nil) ⇒ Boolean
Returns
trueif the given attribute has been changed since creation time,falseotherwise. -
#changed_attributes ⇒ Object
Returns a hash of the attributes and values which have been changed since creation time.
-
#create_methods!(opts = {}) ⇒ Object
Creates accessor methods in self.class and self._resource.class.
-
#initialize(resource = nil) ⇒ Attributes
constructor
:nodoc:.
-
#method_missing(method, *args) ⇒ Object
:nodoc:.
Constructor Details
#initialize(resource = nil) ⇒ Attributes
:nodoc:
6 7 8 |
# File 'lib/hyper_resource/attributes.rb', line 6 def initialize(resource=nil) # :nodoc: 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:
72 73 74 75 |
# File 'lib/hyper_resource/attributes.rb', line 72 def method_missing(method, *args) # :nodoc: return self[method] if self[method] raise NoMethodError, "undefined method `#{method}' for #{self.inspect}" end |
Instance Attribute Details
#_resource ⇒ Object
:nodoc:
4 5 6 |
# File 'lib/hyper_resource/attributes.rb', line 4 def _resource @_resource end |
Instance Method Details
#[](key) ⇒ Object
:nodoc:
66 67 68 69 70 |
# File 'lib/hyper_resource/attributes.rb', line 66 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:
61 62 63 64 |
# File 'lib/hyper_resource/attributes.rb', line 61 def []=(attr, value) # :nodoc: _hr_mark_changed(attr) super(attr.to_s, value) 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_attributes ⇒ Object
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 |
#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 create_methods!(opts={}) 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 |