Module: Utils

Included in:
DslRuntime, Extractor, RootCleanroom, RoxmlBuilder
Defined in:
lib/roundtrip_xml/utils.rb

Constant Summary collapse

VAR_SUFIX =
'_v'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
# File 'lib/roundtrip_xml/utils.rb', line 12

def self.included(base)
  unless base.const_defined?(:VAR_SUFIX)
    base.const_set :VAR_SUFIX, Utils::VAR_SUFIX
  end
end

Instance Method Details

#name_to_sym(name, lower_case = false) ⇒ Object



50
51
52
# File 'lib/roundtrip_xml/utils.rb', line 50

def name_to_sym(name, lower_case = false)
  name_to_sym_helper(name, lower_case)
end

#new_roxml_class(name, parent = Object, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/roundtrip_xml/utils.rb', line 17

def new_roxml_class(name, parent = Object, &block)
  Class.new(parent) do
    include ROXML
    include PlainAccessors
    xml_convention :dasherize
    xml_name name
    def attributes
      self.class.roxml_attrs
    end

    def self.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
      end
    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