Class: CIMI::Model::Schema::Scalar

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

Instance Attribute Summary

Attributes inherited from Attribute

#json_name, #name, #required, #xml_name

Instance Method Summary collapse

Methods inherited from Attribute

#convert, #from_json, #required?, #to_json, #valid?

Constructor Details

#initialize(name, opts) ⇒ Scalar

Returns a new instance of Scalar.



66
67
68
69
70
71
72
# File 'lib/cimi/models/schema.rb', line 66

def initialize(name, opts)
  @text = opts[:text]
  if ! [nil, :nested, :direct].include?(@text)
    raise "text option for scalar must be :nested or :direct"
  end
  super(name, opts)
end

Instance Method Details

#from_xml(xml, model) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/cimi/models/schema.rb', line 78

def from_xml(xml, model)
  case @text
    when :nested then model[@name] = xml[@xml_name].first["content"] if xml[@xml_name]
    when :direct then model[@name] = xml["content"]
    else model[@name] = xml[@xml_name]
  end
end

#nested_text?Boolean

Returns:

  • (Boolean)


76
# File 'lib/cimi/models/schema.rb', line 76

def nested_text?; @text == :nested; end

#text?Boolean

Returns:

  • (Boolean)


74
# File 'lib/cimi/models/schema.rb', line 74

def text?; @text; end

#to_xml(model, xml) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/cimi/models/schema.rb', line 86

def to_xml(model, xml)
  return unless model
  return unless model[@name]
  case @text
    when :nested then xml[@xml_name] = [{ "content" => model[@name] }]
    when :direct then xml["content"] = model[@name]
    else xml[@xml_name] = model[@name]
  end
end