Class: XCDM::Entity

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = {})
  @options = 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

#abstractObject (readonly)

Returns the value of attribute abstract.



27
28
29
# File 'lib/xcdm/entity.rb', line 27

def abstract
  @abstract
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



27
28
29
# File 'lib/xcdm/entity.rb', line 27

def class_name
  @class_name
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/xcdm/entity.rb', line 27

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



27
28
29
# File 'lib/xcdm/entity.rb', line 27

def parent
  @parent
end

#propertiesObject (readonly)

Returns the value of attribute properties.



27
28
29
# File 'lib/xcdm/entity.rb', line 27

def properties
  @properties
end

#relationshipsObject (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, options = {})
  case @schema.xcode_version
  when /4\..*/
    options = {maxCount: 1, minCount: 1, plural_inverse: true}.merge(options)
  else
    options = {maxCount: 1, plural_inverse: true}.merge(options)
  end
  relationship(name, options)
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, options = {})
  case @schema.xcode_version
  when /4\..*/
    options = {maxCount: -1, minCount: 1}.merge(options)
  else
    options = {toMany: true}.merge(options)
  end
  relationship(name, options)
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, options = {})
  case @schema.xcode_version
  when /4\..*/
    options = {maxCount: 1, minCount: 1}.merge(options)
  else
    options = {maxCount: 1}.merge(options)
  end
  relationship(name, options)
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, options = {})
  property = {}

  property[:attributeType] = self.class.convert_type(type)
  property[:name] = name.to_s

  if !options[:default].nil?
    property[:defaultValueString] = normalize_value(options.delete(:default))
  elsif options.key?(:default)
    options.delete(:default)
  elsif [:integer16, :integer32, :integer64].include?(type)
    property[:defaultValueString] = "0"
  elsif [:float, :double, :decimal].include?(type)
    property[:defaultValueString] = "0.0"
  end

  normalize_values(options, property)
  raw_property(property)
end

#raw_property(options) ⇒ Object



47
48
49
# File 'lib/xcdm/entity.rb', line 47

def raw_property(options)
  @properties << DEFAULT_PROPERTY_ATTRIBUTES.merge(options)
end

#raw_relationship(options) ⇒ Object



79
80
81
# File 'lib/xcdm/entity.rb', line 79

def raw_relationship(options)
  @relationships << DEFAULT_RELATIONSHIP_ATTRIBUTES.merge(options)
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, options = {})
  relationship = {}
  relationship[:name] = name.to_s

  if options[:inverse]
    entity, relation = options.delete(:inverse).split('.')
    relationship[:destinationEntity] = relationship[:inverseEntity] = entity
    relationship[:inverseName] = relation
    options.delete(:plural_inverse)
  else
    relationship[:destinationEntity] = relationship[:inverseEntity] = name.to_s.classify
    if options.delete(:plural_inverse)
      relationship[:inverseName] = self.name.underscore.pluralize
    else
      relationship[:inverseName] = self.name.underscore
    end
  end

  normalize_values(options, 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)
  options = {
    name: name,
    syncable: 'YES',
    representedClassName: class_name,
    codeGenerationType: 'class'
  }.merge(@options)
  builder.entity(options) do |xml|
    properties.each do |property|
      xml.attribute(property)
    end

    relationships.each do |relationship|
      xml.relationship(relationship)
    end
  end
end