Class: Saxerator::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/saxerator/configuration.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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_generatorObject



45
46
47
# File 'lib/saxerator/configuration.rb', line 45

def hash_key_generator
  @hash_key_generator || hash_key_normalizer
end

#output_typeObject

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

#adapterObject



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_normalizerObject



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

Returns:

  • (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

Returns:

  • (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_xmlObject



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