Module: Exchanger::Attributes

Included in:
Element
Defined in:
lib/exchanger/attributes.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

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

#attributesObject

Return the attributes hash with indifferent access.



10
11
12
# File 'lib/exchanger/attributes.rb', line 10

def attributes
  @attributes.with_indifferent_access
end

#attributes=(values = {}) ⇒ Object



3
4
5
6
7
# File 'lib/exchanger/attributes.rb', line 3

def attributes=(values = {})
  values.each do |name, value|
    public_send("#{name}=", value)
  end
end

#change_keyObject



41
42
43
# File 'lib/exchanger/attributes.rb', line 41

def change_key
  identifier && identifier.change_key
end

#idObject



37
38
39
# File 'lib/exchanger/attributes.rb', line 37

def id
  identifier && identifier.id
end

#identifierObject



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

#read_attribute(name) ⇒ Object

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

#respond_to?(name) ⇒ Boolean

Override respond_to? so it responds properly for dynamic attributes

Returns:



46
47
48
# File 'lib/exchanger/attributes.rb', line 46

def respond_to?(name)
  (@attributes && @attributes.has_key?(name.to_s)) || super
end

#write_attribute(name, value) ⇒ Object

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