Class: CoreDataMotion::EntityDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/core_data_motion/entity_descriptor.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  syncable:   "YES"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ EntityDescriptor

Returns a new instance of EntityDescriptor.



8
9
10
11
12
13
# File 'lib/core_data_motion/entity_descriptor.rb', line 8

def initialize(name, options = {})
  options     = DEFAULT_OPTIONS.merge options

  @name       = name.to_s
  @syncable   = options[:syncable]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &b) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/core_data_motion/entity_descriptor.rb', line 19

def method_missing(method_name, *args, &b)
  if CDM::DATA_TYPES.keys.include?(method_name)
    self.class.instance_eval do
      define_method(method_name) do |attribute_name, options = {}|
        fields.push CDM::Attribute.new(method_name, attribute_name, options)
      end
    end
    method(method_name).call(args)
  else
    super(method_name, args, b)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/core_data_motion/entity_descriptor.rb', line 5

def name
  @name
end

#syncableObject (readonly)

Returns the value of attribute syncable.



5
6
7
# File 'lib/core_data_motion/entity_descriptor.rb', line 5

def syncable
  @syncable
end

Instance Method Details

#fieldsObject



15
16
17
# File 'lib/core_data_motion/entity_descriptor.rb', line 15

def fields
  @fields ||= []
end

#to_xmlObject



44
45
46
47
48
49
50
51
# File 'lib/core_data_motion/entity_descriptor.rb', line 44

def to_xml
  xml = [
    "  <entity #{xml_attributes_as_string}>",
    fields.map { |f| f.to_xml },
    "  </entity>"
  ]
  xml.flatten.join("\n")
end

#xml_attributesObject



32
33
34
35
36
37
38
# File 'lib/core_data_motion/entity_descriptor.rb', line 32

def xml_attributes
  attrs = {
    name:       name,
    syncable:   syncable
  }
  attrs.reject { |k, v| v.nil? }
end

#xml_attributes_as_stringObject



40
41
42
# File 'lib/core_data_motion/entity_descriptor.rb', line 40

def xml_attributes_as_string
  xml_attributes.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
end