Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/qbxml/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def self.xml_to_hash(node, hash = {}, opts = {})
node_hash = {CONTENT_ROOT => '', ATTR_ROOT => {}}
name = node.name
schema = opts[:schema]
opts[:typecast_cache] ||= {}

# Insert node hash into parent hash correctly.
case hash[name]
Expand Down Expand Up @@ -105,7 +106,7 @@ def self.xml_to_hash(node, hash = {}, opts = {})
node_hash.delete(ATTR_ROOT)
hash[name] = \
if schema
typecast(schema, node.path, node_hash[CONTENT_ROOT])
typecast(schema, node.path, node_hash[CONTENT_ROOT], opts[:typecast_cache])
else
node_hash[CONTENT_ROOT]
end
Expand All @@ -119,9 +120,10 @@ def self.xml_to_hash(node, hash = {}, opts = {})

private

def self.typecast(schema, xpath, value)
def self.typecast(schema, xpath, value, typecast_cache)
type_path = xpath.gsub(/\[\d+\]/,'')
type_proc = Qbxml::TYPE_MAP[schema.xpath(type_path).first.try(:text)]
# This is fairly expensive. Cache it for better performance when parsing lots of records of the same type.
type_proc = typecast_cache[type_path] ||= Qbxml::TYPE_MAP[schema.xpath(type_path).first.try(:text)]
raise "#{xpath} is not a valid type" unless type_proc
type_proc[value]
end
Expand Down