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 Data',
  transformable: 'Transformable'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @name = name
  @properties = []
  @relationships = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
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



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, options = {})
  relationship(name, {maxCount: 1, minCount: 1, plural_inverse: true}.merge(options))
end

#has_many(name, options = {}) ⇒ Object



101
102
103
# File 'lib/xcdm/entity.rb', line 101

def has_many(name, options = {})
  relationship(name, {maxCount: -1, minCount: 1}.merge(options))
end

#has_one(name, options = {}) ⇒ Object



97
98
99
# File 'lib/xcdm/entity.rb', line 97

def has_one(name, options = {})
  relationship(name, {maxCount: 1, minCount: 1}.merge(options))
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, 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 [: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



36
37
38
# File 'lib/xcdm/entity.rb', line 36

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

#raw_relationship(options) ⇒ Object



66
67
68
# File 'lib/xcdm/entity.rb', line 66

def raw_relationship(options)
  @relationships << DEFAULT_RELATIONSHIP_ATTRIBUTES.merge(options)
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, 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



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