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', transformable: 'Transformable' }
Instance Attribute Summary collapse
-
#abstract ⇒ Object
readonly
Returns the value of attribute abstract.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#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(schema, 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(schema, name, options = {}) ⇒ Entity
Returns a new instance of Entity.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/xcdm/entity.rb', line 30 def initialize(schema, name, = {}) @options = .dup @schema = schema @name = name @class_name = @options.delete(:class_name) || name @properties = [] @relationships = [] @parent = @options.delete(:parent) if @parent @options['parentEntity'] = parent end @abstract = @options.delete(:abstract) if @abstract @options['isAbstract'] = normalize_value(@abstract) end end |
Instance Attribute Details
#abstract ⇒ Object (readonly)
Returns the value of attribute abstract.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def abstract @abstract end |
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def class_name @class_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
27 28 29 |
# File 'lib/xcdm/entity.rb', line 27 def parent @parent 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
137 138 139 |
# File 'lib/xcdm/entity.rb', line 137 def self.convert_type(type) TYPE_MAPPING[type] end |
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/xcdm/entity.rb', line 106 def belongs_to(name, = {}) case @schema.xcode_version when /4\..*/ = {maxCount: 1, minCount: 1, plural_inverse: true}.merge() else = {maxCount: 1, plural_inverse: true}.merge() end relationship(name, ) end |
#has_many(name, options = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/xcdm/entity.rb', line 126 def has_many(name, = {}) case @schema.xcode_version when /4\..*/ = {maxCount: -1, minCount: 1}.merge() else = {toMany: true}.merge() end relationship(name, ) end |
#has_one(name, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/xcdm/entity.rb', line 116 def has_one(name, = {}) case @schema.xcode_version when /4\..*/ = {maxCount: 1, minCount: 1}.merge() else = {maxCount: 1}.merge() end relationship(name, ) end |
#property(name, type, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/xcdm/entity.rb', line 51 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 .key?(:default) .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
47 48 49 |
# File 'lib/xcdm/entity.rb', line 47 def raw_property() @properties << DEFAULT_PROPERTY_ATTRIBUTES.merge() end |
#raw_relationship(options) ⇒ Object
79 80 81 |
# File 'lib/xcdm/entity.rb', line 79 def raw_relationship() @relationships << DEFAULT_RELATIONSHIP_ATTRIBUTES.merge() end |
#relationship(name, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/xcdm/entity.rb', line 83 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/xcdm/entity.rb', line 141 def to_xml(builder = nil) builder ||= Builder::XmlMarkup.new(:indent => 2) = { name: name, syncable: 'YES', representedClassName: class_name, codeGenerationType: 'class' }.merge(@options) builder.entity() do |xml| properties.each do |property| xml.attribute(property) end relationships.each do |relationship| xml.relationship(relationship) end end end |