Skip to content

Commit

Permalink
Merge pull request #44 from soulcutter/preformance-updat-part-1
Browse files Browse the repository at this point in the history
Performance update part 1
  • Loading branch information
soulcutter committed Aug 16, 2016
2 parents a7159aa + cc60e20 commit 85be898
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/saxerator/builder/array_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Builder
class ArrayElement < DelegateClass(Array)
attr_accessor :name

def initialize(arr = [])
def initialize(arr = [], name = nil)
@name = name
super(arr)
end

Expand Down
4 changes: 1 addition & 3 deletions lib/saxerator/builder/empty_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def to_h
end

def to_a
array = ArrayElement.new
array.name = name
array
ArrayElement.new([], name)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/saxerator/builder/hash_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def add_to_hash_element(hash, name, element)
name = generate_key(name)
if hash.key? name
unless hash[name].is_a?(ArrayElement)
hash[name] = ArrayElement.new([hash[name]])
hash[name].name = name
hash[name] = ArrayElement.new([hash[name]], name)
end
hash[name] << element
else
Expand Down
9 changes: 3 additions & 6 deletions lib/saxerator/builder/hash_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ class HashElement < DelegateClass(Hash)
attr_accessor :name

def initialize(name = nil, attributes = nil)
self.name = name
self.attributes = attributes
@name = name
@attributes = attributes
super({})
end

def to_a
array = ArrayElement.new
array.name = name
array.concat super
array
ArrayElement.new(super, name)
end

def to_h; self end
Expand Down
9 changes: 3 additions & 6 deletions lib/saxerator/builder/string_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ class StringElement < DelegateClass(String)
attr_accessor :name

def initialize(str, name, attributes)
self.name = name
self.attributes = attributes
@name = name
@attributes = attributes
super(str)
end

def to_a
array = ArrayElement.new
array << self
array.name = name
array
ArrayElement.new(self, name)
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/saxerator/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Saxerator
class Configuration
attr_writer :hash_key_generator
attr_reader :output_type
attr_writer :hash_key_generator, :adapter

ALLOWED_OUTPUT_TYPES = {
nokogiri: [:hash, :xml],
Expand All @@ -15,6 +15,11 @@ def initialize
@ignore_namespaces = false
end

def adapter=(name)
raise ArgumentError.new("Unknown adapter '#{name.inspect}'") unless ALLOWED_OUTPUT_TYPES.has_key?(name)
@adapter = name
end

def adapter
require "saxerator/adapters/#{@adapter}"
Saxerator::Adapters.const_get(@adapter.to_s.capitalize, false)
Expand All @@ -30,6 +35,10 @@ def output_type=(val)
raise_error_if_using_put_attributes_in_hash_with_xml
end

def output_type
@_output_type ||= Builder.to_class(@output_type)
end

def generate_key_for(val)
hash_key_generator.call val
end
Expand Down
4 changes: 2 additions & 2 deletions lib/saxerator/latches/child_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def start_element(name, _)
def end_element(_)
return unless depth_within_element > 0
increment_depth(-1)
@depths.pop if @depths.last == 0
@depths.pop if @depths[-1] == 0
end

def open?
Expand All @@ -26,7 +26,7 @@ def increment_depth(amount)
end

def depth_within_element
!@depths.empty? ? @depths.last : 0
!@depths.empty? ? @depths[-1] : 0
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/saxerator/parser/accumulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ def initialize(config, block)
@stack = []
@config = config
@block = block
@builder = Builder.to_class(@config.output_type)
end

def start_element(name, attrs = [])
@stack.push @builder.new(@config, name, Hash[*attrs.flatten])
@stack.push @config.output_type.new(@config, name, Hash[attrs])
end

def end_element(_)
if @stack.size > 1
last = @stack.pop
@stack.last.add_node last
@stack[-1].add_node last
else
@block.call(@stack.pop.block_variable)
end
end

def characters(string)
@stack.last.add_node(string) unless string.strip.empty?
@stack[-1].add_node(string) unless string.strip.length == 0
end

def accumulating?
Expand Down

0 comments on commit 85be898

Please sign in to comment.