class ClientForPoslynx::Data::AbstractData

Attributes

source_data[RW]

Public Class Methods

attr_element_mappings() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 58
def attr_element_mappings
  @attr_element_mappings ||= (
    self == AbstractData ?
      [] :
      superclass.attr_element_mappings + []
  )
end
blank_new()
Alias for: new
defining_property_mappings() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 50
def defining_property_mappings
  @defining_property_mappings ||= (
    self == AbstractData ?
      [] :
      superclass.defining_property_mappings + []
  )
end
fits_properties?(property_contents) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 45
def fits_properties?(property_contents)
  unmatched = unmatched_defining_properties( property_contents )
  unmatched.empty?
end
load_from_properties(property_contents) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 37
def load_from_properties(property_contents)
  verify_defining_properties property_contents
  variable_property_contents = select_variable_property_contents(property_contents)
  instance = blank_new
  populate_instance_from_properties instance, variable_property_contents
  instance
end
new() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 15
def new
  blank_new
end
Also aliased as: blank_new
short_name() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 19
def short_name
  name.split( '::' ).last
end
xml_deserialize(xml) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 29
def xml_deserialize(xml)
  doc = XmlDocument.from_xml( xml )
  doc.verify_root_element_name root_element_name
  instance = load_from_properties( doc.property_element_contents )
  instance.source_data = xml
  instance
end
xml_parse(source_xml) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 23
def xml_parse(source_xml)
  doc = XmlDocument.from_xml( source_xml )
  data_class = concrete_data_class_for_nokogiri_document( doc )
  data_class.xml_deserialize( source_xml )
end

Private Class Methods

attr_element_mapping(options) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 98
def attr_element_mapping(options)
  mapping = AbstractData::AttributeElementMapping.new( options )
  attr_element_mappings << mapping
  attr_accessor mapping.attribute_name
end
concrete_data_class_for_nokogiri_document(doc) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 76
def concrete_data_class_for_nokogiri_document(doc)
  data_class = concrete_data_classes.detect{ |dc|
    dc.root_element_name == doc.root_name &&
    dc.fits_properties?( doc.property_element_contents )
  }
end
concrete_data_classes() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 83
def concrete_data_classes
  descendants.
    reject{ |d| d.name =~ /\bAbstract[A-Z]\w*$/ }.
    sort_by{ |d| -d.ancestors.length }
end
defining_property_value(options) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 89
def defining_property_value(options)
  attribute = options.fetch( :attribute )
  element   = options.fetch( :element   )
  value     = options.fetch( :value     )
  define_singleton_method( attribute ) { value }
  define_method( attribute ) { value }
  defining_property_mappings << DefiningPropertyMapping.new( attribute: attribute, element: element )
end
descendants() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 72
def descendants
  @@descendants ||= []
end
inherited(descendant) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 68
def inherited(descendant)
  descendants << descendant
end
populate_instance_from_properties(instance, variable_property_contents) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 128
def populate_instance_from_properties instance, variable_property_contents
  variable_property_contents.each do |name, content|
    mapping = attr_element_mappings.detect{ |mapping| mapping.element_name == name }
    next unless mapping
    instance.public_send "#{mapping.attribute_name}=", mapping.value_from_element_content( content)
  end
end
select_variable_property_contents(property_contents) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 123
def select_variable_property_contents(property_contents)
  defining_element_names = defining_property_mappings.map(&:element_name)
  property_contents.reject{ |name, content| defining_element_names.include?(name) }
end
unmatched_defining_properties(property_contents) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 114
def unmatched_defining_properties(property_contents)
  unmatched = []
  defining_property_mappings.each do |property_mapping|
    defining_value = public_send( property_mapping.attribute_name )
    unmatched << property_mapping unless property_contents[property_mapping.element_name] == defining_value
  end
  unmatched
end
verify_defining_properties(property_contents) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 104
def verify_defining_properties(property_contents)
  unmatched = unmatched_defining_properties( property_contents )
  return if unmatched.empty?
  message = unmatched.map{ |property_mapping|
    defining_mapping = public_send( property_mapping.attribute_name )
    "#{property_mapping.element_name} child element with \"#{defining_mapping}\" value not found."
  }.join( ' ' )
  raise InvalidXmlContentError, message
end

Public Instance Methods

xml_serialize() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 140
def xml_serialize
  doc = Data::XmlDocument.with_root_element_name( self.class.root_element_name )
  add_properties_to_xml_document doc
  doc.serialize
end

Private Instance Methods

add_properties_to_xml_document(doc) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 148
def add_properties_to_xml_document(doc)
  all_mappings.each do |mapping|
    content = property_attribute_value( mapping )
    next unless content
    doc_content = mapping.xml_doc_content_from_client_content( content )
    doc.add_property_content mapping.element_name, doc_content
  end
end
all_mappings() click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 157
def all_mappings
  self.class.defining_property_mappings + self.class.attr_element_mappings
end
property_attribute_value( property ) click to toggle source
# File lib/client_for_poslynx/data/abstract_data.rb, line 161
def property_attribute_value( property )
  public_send( property.attribute_name )
end