Class: Saxerator::Configuration
- Inherits:
-
Object
- Object
- Saxerator::Configuration
- Defined in:
- lib/saxerator/configuration.rb
Constant Summary collapse
- ADAPTER_TYPES =
%i[ox nokogiri rexml oga].freeze
Instance Attribute Summary collapse
- #hash_key_generator ⇒ Object
-
#output_type ⇒ Object
Returns the value of attribute output_type.
Instance Method Summary collapse
- #adapter ⇒ Object
- #adapter=(name) ⇒ Object
- #generate_key_for(val) ⇒ Object
- #hash_key_normalizer ⇒ Object
- #ignore_namespaces! ⇒ Object
- #ignore_namespaces? ⇒ Boolean
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #put_attributes_in_hash! ⇒ Object
- #put_attributes_in_hash? ⇒ Boolean
- #raise_error_if_using_put_attributes_in_hash_with_xml ⇒ Object
- #strip_namespaces!(*namespaces) ⇒ Object
- #symbolize_keys! ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
8 9 10 11 12 13 |
# File 'lib/saxerator/configuration.rb', line 8 def initialize @adapter = :rexml @output_type = :hash @put_attributes_in_hash = false @ignore_namespaces = false end |
Instance Attribute Details
#hash_key_generator ⇒ Object
45 46 47 |
# File 'lib/saxerator/configuration.rb', line 45 def hash_key_generator @hash_key_generator || hash_key_normalizer end |
#output_type ⇒ Object
Returns the value of attribute output_type.
4 5 6 |
# File 'lib/saxerator/configuration.rb', line 4 def output_type @output_type end |
Instance Method Details
#adapter ⇒ Object
22 23 24 25 |
# File 'lib/saxerator/configuration.rb', line 22 def adapter require "saxerator/adapters/#{@adapter}" Saxerator::Adapters.const_get(@adapter.to_s.capitalize, false) end |
#adapter=(name) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/saxerator/configuration.rb', line 15 def adapter=(name) unless ADAPTER_TYPES.include?(name) raise ArgumentError, "Unknown adapter '#{name.inspect}'" end @adapter = name end |
#generate_key_for(val) ⇒ Object
37 38 39 |
# File 'lib/saxerator/configuration.rb', line 37 def generate_key_for(val) hash_key_generator.call val end |
#hash_key_normalizer ⇒ Object
41 42 43 |
# File 'lib/saxerator/configuration.rb', line 41 def hash_key_normalizer @hash_key_normalizer ||= ->(x) { x.to_s } end |
#ignore_namespaces! ⇒ Object
66 67 68 |
# File 'lib/saxerator/configuration.rb', line 66 def ignore_namespaces! @ignore_namespaces = true end |
#ignore_namespaces? ⇒ Boolean
62 63 64 |
# File 'lib/saxerator/configuration.rb', line 62 def ignore_namespaces? @ignore_namespaces end |
#put_attributes_in_hash! ⇒ Object
70 71 72 73 |
# File 'lib/saxerator/configuration.rb', line 70 def put_attributes_in_hash! @put_attributes_in_hash = true raise_error_if_using_put_attributes_in_hash_with_xml end |
#put_attributes_in_hash? ⇒ Boolean
75 76 77 |
# File 'lib/saxerator/configuration.rb', line 75 def put_attributes_in_hash? @put_attributes_in_hash end |
#raise_error_if_using_put_attributes_in_hash_with_xml ⇒ Object
79 80 81 82 83 84 |
# File 'lib/saxerator/configuration.rb', line 79 def raise_error_if_using_put_attributes_in_hash_with_xml if @output_type != :hash && @put_attributes_in_hash raise ArgumentError, "put_attributes_in_hash! is only valid \ when using output_type = :hash (the default)'" end end |
#strip_namespaces!(*namespaces) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/saxerator/configuration.rb', line 53 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 end |
#symbolize_keys! ⇒ Object
49 50 51 |
# File 'lib/saxerator/configuration.rb', line 49 def symbolize_keys! @hash_key_generator = ->(x) { hash_key_normalizer.call(x).to_sym } end |