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, #xml_name

Instance Method Summary collapse

Methods inherited from Attribute

#convert, #from_json, #to_json

Constructor Details

#initialize(name, opts) ⇒ Scalar

Returns a new instance of Scalar.



56
57
58
59
60
61
62
# File 'lib/cimi/models/schema.rb', line 56

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



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

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)


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

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

#text?Boolean

Returns:

  • (Boolean)


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

def text?; @text; end

#to_xml(model, xml) ⇒ Object



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

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