Sha256: 9021a07939967b0f69ec1b9f0e6934470f41f33796b336ea73040b27ca9fdfd9

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

class CDM::EntityDescriptor
  DEFAULT_OPTIONS = {
    syncable:   "YES"
  }
  attr_reader :name,
              :syncable

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

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

  def fields
    @fields ||= []
  end

  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

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
core_data_motion-0.0.1 lib/core_data_motion/entity_descriptor.rb