class DataModel::DataType

Attributes

attributes[R]
name[R]

Public Class Methods

attribute(name, type, *args, multiplicity: SINGLE, description: "", links: nil) click to toggle source
# File src/data_model.rb, line 47
def attribute(name, type, *args, multiplicity: SINGLE, description: "", links: nil)
  type = getType(type)
  options = {:multiplicity => multiplicity, :description => description, :name => name, :type => type}
  options[:links] = getType(links)

  for opt in args
    if opt.is_a? Range
      options[:multiplicity] = opt
    elsif opt.is_a? String
      options[:description] = opt
    else
      raise "optional arguments should be string (description), or range\n " << opt.to_s
    end
  end

  @attributes[name] = options
end
extends() click to toggle source
# File src/data_model.rb, line 43
def extends
  @extends < DataType ? @extends.name : nil
end
init(name, domain, extends, description) click to toggle source
# File src/data_model.rb, line 15
def init(name, domain, extends, description)
  @attributes = {}
  @typename = name
  @domain = domain
  @extends = extends
  @description = description

  self.define_singleton_method :domain do
    @domain
  end
  self.define_singleton_method :description do
    @description
  end

  self.define_singleton_method(:attributes) do |inherited = true|
    if inherited && self.superclass.respond_to?(:attributes)
      self.superclass.attributes.merge @attributes
    else
      @attributes
    end
  end

end
new(name, attributes = {}, &block) click to toggle source
# File src/data_model.rb, line 75
def initialize(name, attributes = {}, &block)
  @name = name
  @attributes = attributes

  self.class.attributes.keys.each do |k|
    # add a definition / accessor for each attribute
    self.define_singleton_method(k) do |*args, &block|
      if args.length == 0 && !block
        unless @attributes[k]
          puts ("Warning - reading unset attribute '#{k}' on #{self.class}")
        end
        return @attributes[k]
      end
      if block
        value = valueof(k, *args, &block)
      else
        value = args[0]
      end
      if self.class.attributes[k][:multiplicity].end != 1
        @attributes[k] = [] unless @attributes[k]
        @attributes[k] << value
      else
        @attributes[k] = value
      end
      return value
    end
    self.define_singleton_method("#{k}=".to_sym) do |val|
      @attributes[k] = val
      val
    end
    self.define_singleton_method :include do |&block|
      self.instance_exec &block
    end
  end

  if block_given?
    self.instance_exec &block
  end
end
typename() click to toggle source
# File src/data_model.rb, line 39
def typename
  @typename
end

Private Class Methods

getType(typeref) click to toggle source
# File src/data_model.rb, line 67
def getType(typeref)
  domain.getType typeref
end

Public Instance Methods

to_s() click to toggle source
# File src/data_model.rb, line 115
def to_s
  "#{self.class.typename} #{self.name} #{@attributes}"
end

Private Instance Methods

valueof(sym, *args, &block) click to toggle source
# File src/data_model.rb, line 121
def valueof(sym, *args, &block)
  if self.class.attributes[sym][:type] < DataType
    at = self.class.attributes[sym][:type].new(self.class.attributes[sym][:name])
    if nil == block
      raise "Need a block for a nested type #{sym} in #{self}"
    end
    at.instance_exec &block
    return at
  else
    args[0]
  end
end