Class: Qbxml::Hash

Inherits:
Hash
  • Object
show all
Includes:
Types
Defined in:
lib/qbxml/hash.rb

Overview

Constant Summary collapse

CONTENT_ROOT =
'__content__'.freeze
ATTR_ROOT =
'xml_attributes'.freeze
IGNORED_KEYS =
[ATTR_ROOT]

Constants included from Types

Types::ACRONYMS, Types::ACRONYM_REGEXP, Types::BIGDECIMAL_CAST, Types::BOOL_CAST, Types::DATE_CAST, Types::FLOAT_CAST, Types::INT_CAST, Types::STR_CAST, Types::TIME_CAST, Types::TYPE_MAP, Types::XML_DIRECTIVES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(hash, opts = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qbxml/hash.rb', line 15

def self.from_hash(hash, opts = {}, &block)
  key_proc = \
    if opts[:camelize]
      lambda { |k|
        # QB wants things like ListID, not ListId. Adding inflections then using camelize can accomplish
        # the same thing, but then the inflections will apply to everything the user does everywhere.
        k.camelize.gsub(Qbxml::Types::ACRONYM_REGEXP) { "#{$1}#{$2.upcase}#{$3}" }
      }
    elsif opts[:underscore]
      lambda { |k| k.underscore } 
    end

  deep_convert(hash, opts, &key_proc)
end

.from_xml(xml, opts = {}) ⇒ Object



40
41
42
43
# File 'lib/qbxml/hash.rb', line 40

def self.from_xml(xml, opts = {})
  from_hash(
    xml_to_hash(Nokogiri::XML(xml).root, {}, opts), opts)
end

.to_xml(hash, opts = {}) ⇒ Object



34
35
36
37
38
# File 'lib/qbxml/hash.rb', line 34

def self.to_xml(hash, opts = {})
  opts[:root], hash = hash.first
  opts[:attributes] = hash.delete(ATTR_ROOT)
  hash_to_xml(hash, opts)
end

Instance Method Details

#to_xml(opts = {}) ⇒ Object



30
31
32
# File 'lib/qbxml/hash.rb', line 30

def to_xml(opts = {})
  hash = self.class.to_xml(self, opts)
end