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

Returns a new instance of Struct.



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

def initialize(name, opts, &block)
  content = opts[:content]
  super(name, opts)
  if opts[:class]
    opts[:schema] = opts[:class].schema
  end
  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



135
136
137
138
139
# File 'lib/cimi/models/schema.rb', line 135

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

#convert_from_xml(xml) ⇒ Object



129
130
131
132
133
# File 'lib/cimi/models/schema.rb', line 129

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

#convert_to_json(model) ⇒ Object



147
148
149
150
151
# File 'lib/cimi/models/schema.rb', line 147

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

#convert_to_xml(model) ⇒ Object



141
142
143
144
145
# File 'lib/cimi/models/schema.rb', line 141

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

#from_json(json, model) ⇒ Object



114
115
116
117
# File 'lib/cimi/models/schema.rb', line 114

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



109
110
111
112
# File 'lib/cimi/models/schema.rb', line 109

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



124
125
126
127
# File 'lib/cimi/models/schema.rb', line 124

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

#to_xml(model, xml) ⇒ Object



119
120
121
122
# File 'lib/cimi/models/schema.rb', line 119

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