Class: CIMI::Model::Schema::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/cimi/models/schema.rb

Overview

Attributes describe how we extract values from XML/JSON

Direct Known Subclasses

Array, Collection, Hash, Scalar, Struct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Attribute

Returns a new instance of Attribute.



28
29
30
31
32
# File 'lib/cimi/models/schema.rb', line 28

def initialize(name, opts = {})
  @name = name
  @xml_name = opts[:xml_name] || name.to_s.camelize(true)
  @json_name = opts[:json_name] || name.to_s.camelize(true)
end

Instance Attribute Details

#json_nameObject (readonly)

Returns the value of attribute json_name.



26
27
28
# File 'lib/cimi/models/schema.rb', line 26

def json_name
  @json_name
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/cimi/models/schema.rb', line 26

def name
  @name
end

#xml_nameObject (readonly)

Returns the value of attribute xml_name.



26
27
28
# File 'lib/cimi/models/schema.rb', line 26

def xml_name
  @xml_name
end

Instance Method Details

#convert(value) ⇒ Object



50
51
52
# File 'lib/cimi/models/schema.rb', line 50

def convert(value)
  value
end

#from_json(json, model) ⇒ Object



38
39
40
# File 'lib/cimi/models/schema.rb', line 38

def from_json(json, model)
  model[@name] = json[@json_name]
end

#from_xml(xml, model) ⇒ Object



34
35
36
# File 'lib/cimi/models/schema.rb', line 34

def from_xml(xml, model)
  model[@name] = xml[@xml_name].first if xml.has_key?(@xml_name)
end

#to_json(model, json) ⇒ Object



46
47
48
# File 'lib/cimi/models/schema.rb', line 46

def to_json(model, json)
  json[@json_name] = model[@name] if model and model[@name]
end

#to_xml(model, xml) ⇒ Object



42
43
44
# File 'lib/cimi/models/schema.rb', line 42

def to_xml(model, xml)
  xml[@xml_name] = [model[@name]] if model[@name]
end