Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to use a class instead of lambdas #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/saxerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'saxerator/dsl'
require 'saxerator/full_document'
require 'saxerator/document_fragment'
require 'saxerator/hash_key_processor'
require 'saxerator/configuration'

require 'saxerator/builder'
Expand Down
5 changes: 3 additions & 2 deletions lib/saxerator/builder/hash_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class HashBuilder

def initialize(config, name, attributes)
@config = config
@name = config.generate_key_for(name)
@hash_key_processor = config.hash_key_processor
@name = @hash_key_processor.generate(name)
@attributes = normalize_attributes(attributes)
@children = []
@text = false
Expand Down Expand Up @@ -63,7 +64,7 @@ def normalize_attributes(attributes)
end

def generate_key(name)
@config.generate_key_for(name)
@hash_key_processor.generate(name)
end
end
end
Expand Down
25 changes: 4 additions & 21 deletions lib/saxerator/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Saxerator
class Configuration
attr_writer :hash_key_generator
attr_reader :output_type
attr_reader :output_type, :hash_key_processor

ADAPTER_TYPES = %i[ox nokogiri rexml oga].freeze

Expand All @@ -10,6 +9,7 @@ def initialize
@output_type = :hash
@put_attributes_in_hash = false
@ignore_namespaces = false
@hash_key_processor = Saxerator::HashKeyProcessor.new
end

def adapter=(name)
Expand All @@ -34,29 +34,12 @@ def output_type
@_output_type ||= Builder.to_class(@output_type)
end

def generate_key_for(val)
hash_key_generator.call val
end

def hash_key_normalizer
@hash_key_normalizer ||= ->(x) { x.to_s }
end

def hash_key_generator
@hash_key_generator || hash_key_normalizer
end

def symbolize_keys!
@hash_key_generator = ->(x) { hash_key_normalizer.call(x).to_sym }
@hash_key_processor.symbolize_keys = true
end

def strip_namespaces!(*namespaces)
if namespaces.any?
matching_group = namespaces.join('|')
@hash_key_normalizer = ->(x) { x.to_s.gsub(/(#{matching_group}):/, '') }
else
@hash_key_normalizer = ->(x) { x.to_s.gsub(/\w+:/, '') }
end
@hash_key_processor.strip_namespaces!(*namespaces)
end

def ignore_namespaces?
Expand Down
39 changes: 39 additions & 0 deletions lib/saxerator/hash_key_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Saxerator
class HashKeyProcessor
class DefultNormalizer
def run(key)
key.to_s
end
end

class StripNormalizer < DefultNormalizer
def initialize(*namespaces)
@namespaces_regexp = if namespaces.any?
/(#{namespaces.join('|')}):/
else
/\w+:/
end
end

def run(key)
super.gsub(@namespaces_regexp, '')
end
end

attr_accessor :symbolize_keys

def initialize
@normalizer = DefultNormalizer.new
@symbolize_keys = false
@strip_namespaces = false
end

def generate(key)
@symbolize_keys ? @normalizer.run(key).to_sym : @normalizer.run(key)
end

def strip_namespaces!(*namespaces)
@normalizer = StripNormalizer.new(*namespaces)
end
end
end