class CIMI::Model::Schema::Struct

Attributes

schema[RW]

Public Class Methods

new(name, opts={}, &block) click to toggle source
# File lib/cimi/models/schema.rb, line 101
def initialize(name, opts={}, &block)
  content = opts[:content]
  super(name, opts)
  if opts[:schema]
    if block_given?
      raise "Cannot provide :schema option and a block"
    end
    @schema = opts[:schema]
  else
    @schema = CIMI::Model::Schema.new
    @schema.instance_eval(&block) if block_given?
    @schema.scalar(content, :text => :direct, :required => opts[:required]) if content
  end
end

Public Instance Methods

convert(value) click to toggle source
# File lib/cimi/models/schema.rb, line 166
def convert(value)
  if @klass
    @klass.new(value || {})
  else
    super(value)
  end
end
convert_from_json(json) click to toggle source
# File lib/cimi/models/schema.rb, line 142
def convert_from_json(json)
  sub = struct.new
  @schema.from_json(json, sub)
  sub
end
convert_from_xml(xml) click to toggle source
# File lib/cimi/models/schema.rb, line 136
def convert_from_xml(xml)
  sub = struct.new
  @schema.from_xml(xml, sub)
  sub
end
convert_to_json(model) click to toggle source
# File lib/cimi/models/schema.rb, line 154
def convert_to_json(model)
  json = {}
  @schema.to_json(model, json)
  json
end
convert_to_xml(model) click to toggle source
# File lib/cimi/models/schema.rb, line 148
def convert_to_xml(model)
  xml = OrderedHash.new
  @schema.to_xml(model, xml)
  xml
end
from_json(json, model) click to toggle source
# File lib/cimi/models/schema.rb, line 121
def from_json(json, model)
  json = json.has_key?(json_name) ? json[json_name] : {}
  model[name] = convert_from_json(json)
end
from_xml(xml, model) click to toggle source
# File lib/cimi/models/schema.rb, line 116
def from_xml(xml, model)
  xml = xml.has_key?(xml_name) ? xml[xml_name].first : {}
  model[name] = convert_from_xml(xml)
end
to_json(model, json) click to toggle source
# File lib/cimi/models/schema.rb, line 131
def to_json(model, json)
  conv = convert_to_json(model[name])
  json[json_name] = conv unless conv.empty?
end
to_xml(model, xml) click to toggle source
# File lib/cimi/models/schema.rb, line 126
def to_xml(model, xml)
  conv = convert_to_xml(model[name])
  xml[xml_name] = [conv] unless conv.empty?
end
valid?(value) click to toggle source
# File lib/cimi/models/schema.rb, line 160
def valid?(value)
  @schema.required_attributes.all? { |a|
    a.valid?(value.send(a.name))
  }
end