class Frodo::Properties::Complex

Abstract base class for Frodo ComplexTypes @see [Frodo::Schema::ComplexType]

Public Class Methods

from_xml(property_xml, options = {}) click to toggle source

Creates a new property instance from an XML element @param property_xml [Nokogiri::XML::Element] @param options [Hash] @return [Frodo::Property]

# File lib/frodo/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
new(name, value, options = {}) click to toggle source
Calls superclass method Frodo::Property::new
# File lib/frodo/properties/complex.rb, line 6
def initialize(name, value, options = {})
  super(name, value, options)
  init_properties
end

Public Instance Methods

[](property_name) click to toggle source

Returns the value of the requested property. @param property_name [to_s] @return [*]

# File lib/frodo/properties/complex.rb, line 41
def [](property_name)
  properties[property_name.to_s].value
end
[]=(property_name, value) click to toggle source

Sets the value of the named property. @param property_name [to_s] @param value [*] @return [*]

# File lib/frodo/properties/complex.rb, line 49
def []=(property_name, value)
  properties[property_name.to_s].value = value
end
property_names() click to toggle source

Returns a list of this ComplexType's property names. @return [Array<String>]

# File lib/frodo/properties/complex.rb, line 34
def property_names
  @property_names ||= properties.keys
end
to_xml(xml_builder) click to toggle source

Returns the XML representation of the property to the supplied XML builder. @param xml_builder [Nokogiri::XML::Builder]

# File lib/frodo/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
value() click to toggle source

Returns the property value, properly typecast @return [Hash, nil]

# File lib/frodo/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) click to toggle source

Sets the property value @params new_value [Hash]

# File lib/frodo/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

Private Instance Methods

complex_type() click to toggle source
# File lib/frodo/properties/complex.rb, line 80
def complex_type
  raise NotImplementedError, 'Subclass must override'
end
init_properties() click to toggle source
# File lib/frodo/properties/complex.rb, line 88
def init_properties
  @properties = complex_type.send(:collect_properties)
  set_properties(JSON.parse(@value)) unless @value.nil?
end
properties() click to toggle source
# File lib/frodo/properties/complex.rb, line 84
def properties
  @properties
end
set_properties(new_properties) click to toggle source
# File lib/frodo/properties/complex.rb, line 93
def set_properties(new_properties)
  property_names.each do |prop_name|
    self[prop_name] = new_properties[prop_name]
  end
end
validate(value) click to toggle source
# File lib/frodo/properties/complex.rb, line 99
def validate(value)
  return if value.nil? && allows_nil?
  raise ArgumentError, 'Value must be a Hash' unless value.is_a?(Hash)
  value.keys.each do |name|
    unless property_names.include?(name) || name =~ /@odata/
      raise ArgumentError, "Invalid property #{name}"
    end
  end
end
validate_options(options) click to toggle source
# File lib/frodo/properties/complex.rb, line 109
def validate_options(options)
  raise ArgumentError, 'Type is required' unless options[:type]
end