Module: Exchanger::Attributes
- Included in:
- Element
- Defined in:
- lib/exchanger/attributes.rb
Instance Method Summary (collapse)
-
- (Object) attributes
Return the attributes hash with indifferent access.
- - (Object) attributes=(values = {})
- - (Object) change_key
- - (Object) id
- - (Object) identifier
-
- (Object) method_missing(name, *args)
Used for allowing accessor methods for dynamic attributes.
-
- (Object) read_attribute(name)
TODO: Add typecasting Read a value from the Document attributes.
-
- (Boolean) respond_to?(name)
Override respond_to? so it responds properly for dynamic attributes.
-
- (Object) write_attribute(name, value)
TODO: Add typecasting.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name, *args)
Used for allowing accessor methods for dynamic attributes
51 52 53 54 55 56 57 58 59 |
# File 'lib/exchanger/attributes.rb', line 51 def method_missing(name, *args) attr = name.to_s.sub("=", "") return super unless attributes.has_key?(attr) if name.to_s.ends_with?("=") write_attribute(attr, *args) else read_attribute(attr) end end |
Instance Method Details
- (Object) attributes
Return the attributes hash with indifferent access.
10 11 12 |
# File 'lib/exchanger/attributes.rb', line 10 def attributes @attributes.with_indifferent_access end |
- (Object) attributes=(values = {})
3 4 5 6 7 |
# File 'lib/exchanger/attributes.rb', line 3 def attributes=(values = {}) values.each do |name, value| write_attribute(name, value) end end |
- (Object) change_key
41 42 43 |
# File 'lib/exchanger/attributes.rb', line 41 def change_key identifier && identifier.change_key end |
- (Object) id
37 38 39 |
# File 'lib/exchanger/attributes.rb', line 37 def id identifier && identifier.id end |
- (Object) identifier
29 30 31 32 33 34 35 |
# File 'lib/exchanger/attributes.rb', line 29 def identifier if self.class.identifier_name @identifier ||= self.send(self.class.identifier_name) @identifier.tag_name = self.class.identifier_name.to_s.camelize if @identifier @identifier end end |
- (Object) read_attribute(name)
TODO: Add typecasting Read a value from the Document attributes. If the value does not exist it will return nil.
17 18 19 20 21 |
# File 'lib/exchanger/attributes.rb', line 17 def read_attribute(name) name = name.to_s value = @attributes[name] accessed(name, value) end |
- (Boolean) respond_to?(name)
Override respond_to? so it responds properly for dynamic attributes
46 47 48 |
# File 'lib/exchanger/attributes.rb', line 46 def respond_to?(name) (@attributes && @attributes.has_key?(name.to_s)) || super end |
- (Object) write_attribute(name, value)
TODO: Add typecasting
24 25 26 27 |
# File 'lib/exchanger/attributes.rb', line 24 def write_attribute(name, value) name = name.to_s modify(name, @attributes[name], value) end |