Class: CIMI::Model::Schema::Attribute
- Inherits:
-
Object
- Object
- CIMI::Model::Schema::Attribute
- 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
-
#json_name ⇒ Object
readonly
Returns the value of attribute json_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#xml_name ⇒ Object
readonly
Returns the value of attribute xml_name.
Instance Method Summary collapse
- #convert(value) ⇒ Object
- #from_json(json, model) ⇒ Object
- #from_xml(xml, model) ⇒ Object
-
#initialize(name, opts = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #required? ⇒ Boolean
- #to_json(model, json) ⇒ Object
- #to_xml(model, xml) ⇒ Object
- #valid?(value) ⇒ Boolean
Constructor Details
#initialize(name, opts = {}) ⇒ Attribute
Returns a new instance of Attribute.
29 30 31 32 33 34 |
# File 'lib/cimi/models/schema.rb', line 29 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) @required = opts[:required] || false end |
Instance Attribute Details
#json_name ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/cimi/models/schema.rb', line 26 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
27 28 29 |
# File 'lib/cimi/models/schema.rb', line 27 def required @required end |
#xml_name ⇒ Object (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
52 53 54 |
# File 'lib/cimi/models/schema.rb', line 52 def convert(value) value end |
#from_json(json, model) ⇒ Object
40 41 42 |
# File 'lib/cimi/models/schema.rb', line 40 def from_json(json, model) model[@name] = json[@json_name] end |
#from_xml(xml, model) ⇒ Object
36 37 38 |
# File 'lib/cimi/models/schema.rb', line 36 def from_xml(xml, model) model[@name] = xml[@xml_name].first if xml.has_key?(@xml_name) end |
#required? ⇒ Boolean
56 57 58 |
# File 'lib/cimi/models/schema.rb', line 56 def required? @required end |
#to_json(model, json) ⇒ Object
48 49 50 |
# File 'lib/cimi/models/schema.rb', line 48 def to_json(model, json) json[@json_name] = model[@name] if model and model[@name] end |
#to_xml(model, xml) ⇒ Object
44 45 46 |
# File 'lib/cimi/models/schema.rb', line 44 def to_xml(model, xml) xml[@xml_name] = [model[@name]] if model[@name] end |
#valid?(value) ⇒ Boolean
60 61 62 |
# File 'lib/cimi/models/schema.rb', line 60 def valid?(value) !value.nil? and !value.to_s.empty? end |