Class: FrOData::Properties::Complex

Inherits:
FrOData::Property show all
Defined in:
lib/frodata/properties/complex.rb

Overview

Abstract base class for FrOData ComplexTypes

See Also:

  • [FrOData[FrOData::Schema[FrOData::Schema::ComplexType]

Instance Attribute Summary

Attributes inherited from FrOData::Property

#name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FrOData::Property

#==, #allows_nil?, #concurrency_mode, #json_value, #strict?, #type, #url_value, #xml_value

Constructor Details

#initialize(name, value, options = {}) ⇒ Complex

Returns a new instance of Complex.



6
7
8
9
# File 'lib/frodata/properties/complex.rb', line 6

def initialize(name, value, options = {})
  super(name, value, options)
  init_properties
end

Class Method Details

.from_xml(property_xml, options = {}) ⇒ FrOData::Property

Creates a new property instance from an XML element

Parameters:

  • property_xml (Nokogiri::XML::Element)
  • options (Hash) (defaults to: {})

Returns:



72
73
74
75
76
# File 'lib/frodata/properties/complex.rb', line 72

def self.from_xml(property_xml, options = {})
  nodes = property_xml.element_children
  props = Hash[nodes.map { |el| [el.name, el.content] }]
  new(property_xml.name, props.to_json, options)
end

Instance Method Details

#[](property_name) ⇒ *

Returns the value of the requested property.

Parameters:

  • property_name (to_s)

Returns:

  • (*)


41
42
43
# File 'lib/frodata/properties/complex.rb', line 41

def [](property_name)
  properties[property_name.to_s].value
end

#[]=(property_name, value) ⇒ *

Sets the value of the named property.

Parameters:

  • property_name (to_s)
  • value (*)

Returns:

  • (*)


49
50
51
# File 'lib/frodata/properties/complex.rb', line 49

def []=(property_name, value)
  properties[property_name.to_s].value = value
end

#property_namesArray<String>

Returns a list of this ComplexType’s property names.

Returns:



34
35
36
# File 'lib/frodata/properties/complex.rb', line 34

def property_names
  @property_names ||= properties.keys
end

#to_xml(xml_builder) ⇒ Object

Returns the XML representation of the property to the supplied XML builder.

Parameters:

  • xml_builder (Nokogiri::XML::Builder)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/frodata/properties/complex.rb', line 56

def to_xml(xml_builder)
  attributes = {
      'metadata:type' => type,
  }

  xml_builder['data'].send(name.to_sym, attributes) do
    properties.each do |name, property|
      property.to_xml(xml_builder)
    end
  end
end

#valueHash?

Returns the property value, properly typecast

Returns:

  • (Hash, nil)


13
14
15
16
17
18
19
# File 'lib/frodata/properties/complex.rb', line 13

def value
  if allows_nil? && properties.values.all?(&:nil?)
    nil
  else
    Hash[properties.map { |key, value| [key, value.value] }]
  end
end

#value=(new_value) ⇒ Object

Sets the property value



23
24
25
26
27
28
29
30
# File 'lib/frodata/properties/complex.rb', line 23

def value=(new_value)
  validate(new_value)
  if new_value.nil?
    property_names.each { |name| self[name] = nil }
  else
    property_names.each { |name| self[name] = new_value[name] }
  end
end