Class: CIMI::Model::Schema::Struct

Inherits:
Attribute
  • Object
show all
Defined in:
lib/cimi/models/schema.rb

Instance Attribute Summary collapse

Attributes inherited from Attribute

#json_name, #name, #xml_name

Instance Method Summary collapse

Methods inherited from Attribute

#convert

Constructor Details

#initialize(name, opts, &block) ⇒ Struct



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cimi/models/schema.rb', line 91

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) if content
  end
end

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



89
90
91
# File 'lib/cimi/models/schema.rb', line 89

def schema
  @schema
end

Instance Method Details

#convert_from_json(json) ⇒ Object



132
133
134
135
136
# File 'lib/cimi/models/schema.rb', line 132

def convert_from_json(json)
  sub = struct.new
  @schema.from_json(json, sub)
  sub
end

#convert_from_xml(xml) ⇒ Object



126
127
128
129
130
# File 'lib/cimi/models/schema.rb', line 126

def convert_from_xml(xml)
  sub = struct.new
  @schema.from_xml(xml, sub)
  sub
end

#convert_to_json(model) ⇒ Object



144
145
146
147
148
# File 'lib/cimi/models/schema.rb', line 144

def convert_to_json(model)
  json = {}
  @schema.to_json(model, json)
  json
end

#convert_to_xml(model) ⇒ Object



138
139
140
141
142
# File 'lib/cimi/models/schema.rb', line 138

def convert_to_xml(model)
  xml = OrderedHash.new
  @schema.to_xml(model, xml)
  xml
end

#from_json(json, model) ⇒ Object



111
112
113
114
# File 'lib/cimi/models/schema.rb', line 111

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) ⇒ Object



106
107
108
109
# File 'lib/cimi/models/schema.rb', line 106

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) ⇒ Object



121
122
123
124
# File 'lib/cimi/models/schema.rb', line 121

def to_json(model, json)
  conv = convert_to_json(model[name])
  json[json_name] = conv unless conv.empty?
end

#to_xml(model, xml) ⇒ Object



116
117
118
119
# File 'lib/cimi/models/schema.rb', line 116

def to_xml(model, xml)
  conv = convert_to_xml(model[name])
  xml[xml_name] = [conv] unless conv.empty?
end