module Utils

Constants

UNDEFINED_PARAM
VAR_SUFIX

Public Class Methods

class_name() click to toggle source
# File lib/roundtrip_xml/utils.rb, line 48
def self.class_name
  @class_name || name_to_sym_helper(self.tag_name)
end
from_hash(runtime, hash) click to toggle source
# File lib/roundtrip_xml/utils.rb, line 63
def self.from_hash(runtime, hash)
  hash.delete '__class'
  obj = self.new
  hash.each do |k, val|
    if val.is_a? Hash
      val = runtime.fetch(val['__class']).from_hash(runtime, val)
    end
    obj.send "#{k}=", val
  end
  obj
end
included(base) click to toggle source
# File lib/roundtrip_xml/utils.rb, line 24
def self.included(base)
  unless base.const_defined?(:VAR_SUFIX)
    base.const_set :VAR_SUFIX, Utils::VAR_SUFIX
  end
end
subclass?() click to toggle source

by default a ROXML class isn't a sub class

# File lib/roundtrip_xml/utils.rb, line 41
def self.subclass?
  @is_subclass || false
end
unique_parent_accessors() click to toggle source
# File lib/roundtrip_xml/utils.rb, line 74
def self.unique_parent_accessors
  plain = Set.new(plain_accessors.map {|accessor| accessor.to_s.gsub(VAR_SUFIX, '').to_sym})
  parent_accessors = Set.new(roxml_attrs.map { |attr| attr.accessor })
  parent_accessors - plain
end

Public Instance Methods

_metadata() click to toggle source
# File lib/roundtrip_xml/utils.rb, line 34
def _metadata
  @_metadata || {}
end
attributes() click to toggle source
# File lib/roundtrip_xml/utils.rb, line 44
def attributes
  self.class.roxml_attrs
end
name_to_sym(name, lower_case = false) click to toggle source
# File lib/roundtrip_xml/utils.rb, line 84
def name_to_sym(name, lower_case = false)
  name_to_sym_helper(name, lower_case)
end
new_roxml_class(name, parent = Object, &block) click to toggle source
# File lib/roundtrip_xml/utils.rb, line 29
def new_roxml_class(name, parent = Object, &block)
  Class.new(parent) do
    include ROXML
    include PlainAccessors
    attr_writer :_metadata
    def _metadata
      @_metadata || {}
    end
    xml_convention :dasherize
    xml_name parent.respond_to?(:tag_name) ? parent.tag_name : name

    # by default a ROXML class isn't a sub class
    def self.subclass?
      @is_subclass || false
    end
    def attributes
      self.class.roxml_attrs
    end

    def self.class_name
      @class_name || name_to_sym_helper(self.tag_name)
    end

    def to_hash
      attributes.inject({}) do |hash, a|
        value = a.to_ref(self).to_xml(self)
        value = value.to_hash if value.respond_to? :to_hash

        hash[a.accessor] = value
        hash['__class'] = self.class.class_name
        hash
      end
    end

    def self.from_hash(runtime, hash)
      hash.delete '__class'
      obj = self.new
      hash.each do |k, val|
        if val.is_a? Hash
          val = runtime.fetch(val['__class']).from_hash(runtime, val)
        end
        obj.send "#{k}=", val
      end
      obj
    end
    def self.unique_parent_accessors
      plain = Set.new(plain_accessors.map {|accessor| accessor.to_s.gsub(VAR_SUFIX, '').to_sym})
      parent_accessors = Set.new(roxml_attrs.map { |attr| attr.accessor })
      parent_accessors - plain
    end
    class_eval &block if block_given?
  end

end
to_hash() click to toggle source
# File lib/roundtrip_xml/utils.rb, line 52
def to_hash
  attributes.inject({}) do |hash, a|
    value = a.to_ref(self).to_xml(self)
    value = value.to_hash if value.respond_to? :to_hash

    hash[a.accessor] = value
    hash['__class'] = self.class.class_name
    hash
  end
end