Module: C3sModel

Defined in:
lib/model.rb

Constant Summary collapse

IGNORE_ATTR =

Attributes ignored and not included on model xml by default

["created_at", "updated_at", "id", "jid"]

Instance Method Summary collapse

Instance Method Details

#_class_nameObject

Returns the class name for this model without module name



17
18
19
# File 'lib/model.rb', line 17

def _class_name
  @class_name ||= self.class.name.split("::").last.downcase
end

#first_element(options = {}) ⇒ Object

Returns the first element for the model xml



48
49
50
51
52
53
54
# File 'lib/model.rb', line 48

def first_element(options={})
  return options[:parent] if options[:parent]
  
  name = self._class_name
  name = name.pluralize if options[:plural]
  REXML::Element.new(name)
end

#nodesObject

Gets the model nodes hash



23
24
25
26
27
28
29
30
31
# File 'lib/model.rb', line 23

def nodes
  nodes = self.attributes
  nodes.each do |key, val|
    if IGNORE_ATTR.include?(key) or key.match(/.+_id/)
      nodes.delete(key)
    end
  end
  nodes.keys
end

#to_xml(options = {}) ⇒ Object

Gets the model xml to include in a packet.

options
Hash

options



36
37
38
39
40
41
42
43
44
# File 'lib/model.rb', line 36

def to_xml(options={})
  xml = first_element(options)
  self.attributes.each do |key, val|
    if !IGNORE_ATTR.include?(key) && !key.match(/.+_id/)
      xml.add REXML::Element.new(key).add_text(val.to_s)
    end
  end
  xml
end

#update_attributes(attributes = {}) ⇒ Object

Updates multiple attributes at once

attributes
Hash

the attributes hash



11
12
13
# File 'lib/model.rb', line 11

def update_attributes(attributes={})
  self.send(:attributes=, attributes, false)
end

#xmlnsObject

Returns a default namespace based on class name



58
59
60
# File 'lib/model.rb', line 58

def xmlns
  "http://c3s.hng.av.it.pt/#{self._class_name}"
end