Class: XCDM::Entity
- Inherits:
-
Object
- Object
- XCDM::Entity
- Defined in:
- lib/xcdm/entity.rb
Constant Summary collapse
- DEFAULT_PROPERTY_ATTRIBUTES =
{ optional: 'YES', syncable: 'YES' }
- DEFAULT_RELATIONSHIP_ATTRIBUTES =
{ optional: 'YES', deletionRule: 'Nullify', syncable: 'YES' }
- TYPE_MAPPING =
{ integer16: 'Integer 16', integer32: 'Integer 32', integer64: 'Integer 64', decimal: 'Decimal', double: 'Double', float: 'Float', string: 'String', boolean: 'Boolean', datetime: 'Date', binary: 'Binary Data', transformable: 'Transformable' }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
Class Method Summary collapse
Instance Method Summary collapse
- #belongs_to(name, options = {}) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
- #has_one(name, options = {}) ⇒ Object
-
#initialize(name, options = {}) ⇒ Entity
constructor
A new instance of Entity.
- #property(name, type, options = {}) ⇒ Object
- #raw_property(options) ⇒ Object
- #raw_relationship(options) ⇒ Object
- #relationship(name, options = {}) ⇒ Object
- #to_xml(builder = nil) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Entity
Returns a new instance of Entity.
30 31 32 33 34 |
# File 'lib/xcdm/entity.rb', line 30 def initialize(name, = {}) @name = name @properties = [] @relationships = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def name @name end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def properties @properties end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def relationships @relationships end |
Class Method Details
.convert_type(type) ⇒ Object
106 107 108 |
# File 'lib/xcdm/entity.rb', line 106 def self.convert_type(type) TYPE_MAPPING[type] end |
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
93 94 95 |
# File 'lib/xcdm/entity.rb', line 93 def belongs_to(name, = {}) relationship(name, {maxCount: 1, minCount: 1, plural_inverse: true}.merge()) end |
#has_many(name, options = {}) ⇒ Object
101 102 103 |
# File 'lib/xcdm/entity.rb', line 101 def has_many(name, = {}) relationship(name, {maxCount: -1, minCount: 1}.merge()) end |
#has_one(name, options = {}) ⇒ Object
97 98 99 |
# File 'lib/xcdm/entity.rb', line 97 def has_one(name, = {}) relationship(name, {maxCount: 1, minCount: 1}.merge()) end |
#property(name, type, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/xcdm/entity.rb', line 40 def property(name, type, = {}) property = {} property[:attributeType] = self.class.convert_type(type) property[:name] = name.to_s if ![:default].nil? property[:defaultValueString] = normalize_value(.delete(:default)) elsif [:integer16, :integer32, :integer64].include?(type) property[:defaultValueString] = "0" elsif [:float, :double, :decimal].include?(type) property[:defaultValueString] = "0.0" end normalize_values(, property) raw_property(property) end |
#raw_property(options) ⇒ Object
36 37 38 |
# File 'lib/xcdm/entity.rb', line 36 def raw_property() @properties << DEFAULT_PROPERTY_ATTRIBUTES.merge() end |
#raw_relationship(options) ⇒ Object
66 67 68 |
# File 'lib/xcdm/entity.rb', line 66 def raw_relationship() @relationships << DEFAULT_RELATIONSHIP_ATTRIBUTES.merge() end |
#relationship(name, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/xcdm/entity.rb', line 70 def relationship(name, = {}) relationship = {} relationship[:name] = name.to_s if [:inverse] entity, relation = .delete(:inverse).split('.') relationship[:destinationEntity] = relationship[:inverseEntity] = entity relationship[:inverseName] = relation .delete(:plural_inverse) else relationship[:destinationEntity] = relationship[:inverseEntity] = name.to_s.classify if .delete(:plural_inverse) relationship[:inverseName] = self.name.underscore.pluralize else relationship[:inverseName] = self.name.underscore end end normalize_values(, relationship) raw_relationship(relationship) end |
#to_xml(builder = nil) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/xcdm/entity.rb', line 110 def to_xml(builder = nil) builder ||= Builder::XmlMarkup.new(:indent => 2) builder.entity(name: name, syncable: 'YES') do |xml| properties.each do |property| xml.attribute(property) end relationships.each do |relationship| xml.relationship(relationship) end end end |