class Frodo::Property
Frodo::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 Frodo::Properties
namespace.
Attributes
The property's name
The property's options
The property's value
Public Class Methods
Creates a new property instance from an XML element @param property_xml [Nokogiri::XML::Element] @param options [Hash] @return [Frodo::Property]
# File lib/frodo/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
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/frodo/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
Provides for value-based equality checking. @param other [value] object for comparison @return [Boolean]
# File lib/frodo/property.rb, line 34 def ==(other) self.value == other.value end
Whether the property permits a nil value. (Default=true) @return [Boolean]
# File lib/frodo/property.rb, line 41 def allows_nil? options[:allows_nil] end
The configured concurrency mode for the property. @return [String]
# File lib/frodo/property.rb, line 60 def concurrency_mode @concurrency_mode ||= options[:concurrency_mode] end
Value to be used in JSON. @return [*]
# File lib/frodo/property.rb, line 72 def json_value value end
Whether the property uses strict validation. (Default=false) @return [Boolean]
# File lib/frodo/property.rb, line 48 def strict? if options.key? :strict options[:strict] elsif service service.options[:strict] else true end end
Returns the XML representation of the property to the supplied XML builder. @param xml_builder [Nokogiri::XML::Builder]
# File lib/frodo/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
Abstract implementation, should return property type, based on Frodo::Service
metadata in proper implementation. @raise NotImplementedError
# File lib/frodo/property.rb, line 27 def type raise NotImplementedError end
Value to be used in URLs. @return [String]
# File lib/frodo/property.rb, line 78 def url_value @value end
Value to be used in XML. @return [String]
# File lib/frodo/property.rb, line 66 def xml_value @value end
Protected Instance Methods
# File lib/frodo/property.rb, line 114 def default_options { allows_nil: true, concurrency_mode: :none } end
# File lib/frodo/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
# File lib/frodo/property.rb, line 121 def service options[:service] end
# File lib/frodo/property.rb, line 130 def validation_error(message) if strict? raise ArgumentError, "#{name}: #{message}" else logger.warn "#{name}: #{message}" nil end end