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

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

Direct Known Subclasses

Ref

Instance Attribute Summary collapse

Attributes inherited from Attribute

#json_name, #name, #required, #xml_name

Instance Method Summary collapse

Methods inherited from Attribute

#required?

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Struct



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



99
100
101
# File 'lib/cimi/models/schema.rb', line 99

def schema
  @schema
end

Instance Method Details

#convert(value) ⇒ Object



166
167
168
169
170
171
172
# 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) ⇒ Object



142
143
144
145
146
# 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) ⇒ Object



136
137
138
139
140
# 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) ⇒ Object



154
155
156
157
158
# 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) ⇒ Object



148
149
150
151
152
# 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) ⇒ Object



121
122
123
124
# 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) ⇒ Object



116
117
118
119
# 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) ⇒ Object



131
132
133
134
# 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) ⇒ Object



126
127
128
129
# 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) ⇒ Boolean



160
161
162
163
164
# File 'lib/cimi/models/schema.rb', line 160

def valid?(value)
  @schema.required_attributes.all? { |a|
    a.valid?(value.send(a.name))
  }
end