class FrOData::Property

FrOData::Property represents an abstract property, defining the basic interface and required methods, with some default implementations. All supported property types should be implemented under the FrOData::Properties namespace.

Attributes

name[R]

The property's name

options[R]

The property's options

value[RW]

The property's value

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 [FrOData::Property]

# File lib/frodata/property.rb, line 102
def self.from_xml(property_xml, options = {})
  if property_xml.attributes['null'].andand.value == 'true'
    content = nil
  else
    content = property_xml.content
  end

  new(property_xml.name, content, options)
end
new(name, value, options = {}) click to toggle source

Default intialization for a property with a name, value and options. @param name [to_s] @param value [to_s,nil] @param options [Hash]

# File lib/frodata/property.rb, line 18
def initialize(name, value, options = {})
  @name = name.to_s
  @value = value.nil? ? nil : value.to_s
  @options = default_options.merge(options)
end

Public Instance Methods

==(other) click to toggle source

Provides for value-based equality checking. @param other [value] object for comparison @return [Boolean]

# File lib/frodata/property.rb, line 34
def ==(other)
  self.value == other.value
end
allows_nil?() click to toggle source

Whether the property permits a nil value. (Default=true) @return [Boolean]

# File lib/frodata/property.rb, line 41
def allows_nil?
  options[:allows_nil]
end
concurrency_mode() click to toggle source

The configured concurrency mode for the property. @return [String]

# File lib/frodata/property.rb, line 60
def concurrency_mode
  @concurrency_mode ||= options[:concurrency_mode]
end
json_value() click to toggle source

Value to be used in JSON. @return [*]

# File lib/frodata/property.rb, line 72
def json_value
  value
end
strict?() click to toggle source

Whether the property uses strict validation. (Default=false) @return [Boolean]

# File lib/frodata/property.rb, line 48
def strict?
  if options.key? :strict
    options[:strict]
  elsif service
    service.options[:strict]
  else
    true
  end
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/frodata/property.rb, line 85
def to_xml(xml_builder)
  attributes = {
    'metadata:type' => type,
  }

  if value.nil?
    attributes['metadata:null'] = 'true'
    xml_builder['data'].send(name.to_sym, attributes)
  else
    xml_builder['data'].send(name.to_sym, attributes, xml_value)
  end
end
type() click to toggle source

Abstract implementation, should return property type, based on FrOData::Service metadata in proper implementation. @raise NotImplementedError

# File lib/frodata/property.rb, line 27
def type
  raise NotImplementedError
end
url_value() click to toggle source

Value to be used in URLs. @return [String]

# File lib/frodata/property.rb, line 78
def url_value
  @value
end
xml_value() click to toggle source

Value to be used in XML. @return [String]

# File lib/frodata/property.rb, line 66
def xml_value
  @value
end

Protected Instance Methods

default_options() click to toggle source
# File lib/frodata/property.rb, line 114
def default_options
  {
    allows_nil:       true,
    concurrency_mode: :none
  }
end
logger() click to toggle source
# File lib/frodata/property.rb, line 125
def logger
  # Use a dummy logger if service is not available (-> unit tests)
  @logger ||= service.andand.logger || Logger.new('/dev/null')
end
service() click to toggle source
# File lib/frodata/property.rb, line 121
def service
  options[:service]
end
validation_error(message) click to toggle source
# File lib/frodata/property.rb, line 130
def validation_error(message)
  if strict?
    raise ArgumentError, "#{name}: #{message}"
  else
    logger.warn "#{name}: #{message}"
    nil
  end
end