Class: Moxml::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/config.rb

Constant Summary collapse

VALID_ADAPTERS =
%i[nokogiri oga rexml ox headed_ox libxml].freeze
DEFAULT_ADAPTER =
VALID_ADAPTERS.first
ENTITY_LOAD_MODES =

Entity loading modes:

  • :required - Must load entities, raise error if unavailable (default)

  • :optional - Try to load, continue silently if unavailable

  • :disabled - Don’t load entities, use empty registry

  • :custom - Use custom entity provider via entity_provider callback

%i[required optional disabled custom].freeze
NAMESPACE_URI_MODES =
%i[strict lenient].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_name = nil, strict_parsing = nil, default_encoding = nil) ⇒ Config

Returns a new instance of Config.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/moxml/config.rb', line 40

def initialize(adapter_name = nil, strict_parsing = nil,
               default_encoding = nil)
  self.adapter = adapter_name || Config.default.adapter_name
  @strict_parsing = strict_parsing || Config.default.strict_parsing
  @default_encoding = default_encoding || Config.default.default_encoding
  # reserved for future use
  @default_indent = 2
  @entity_encoding = :basic
  @restore_entities = false
  @preload_entity_sets = []
  @entity_load_mode = :required
  @entity_provider = nil
  @namespace_uri_mode = :strict
end

Class Attribute Details

.default_adapterObject



22
23
24
# File 'lib/moxml/config.rb', line 22

def default_adapter
  @default_adapter ||= DEFAULT_ADAPTER
end

Instance Attribute Details

#adapter_nameObject (readonly)

Returns the value of attribute adapter_name.



29
30
31
# File 'lib/moxml/config.rb', line 29

def adapter_name
  @adapter_name
end

#default_encodingObject

Returns the value of attribute default_encoding.



30
31
32
# File 'lib/moxml/config.rb', line 30

def default_encoding
  @default_encoding
end

#default_indentObject

Returns the value of attribute default_indent.



30
31
32
# File 'lib/moxml/config.rb', line 30

def default_indent
  @default_indent
end

#entity_encodingObject

Returns the value of attribute entity_encoding.



30
31
32
# File 'lib/moxml/config.rb', line 30

def entity_encoding
  @entity_encoding
end

#entity_load_modeObject

Returns the value of attribute entity_load_mode.



30
31
32
# File 'lib/moxml/config.rb', line 30

def entity_load_mode
  @entity_load_mode
end

#entity_providerObject

Returns the value of attribute entity_provider.



30
31
32
# File 'lib/moxml/config.rb', line 30

def entity_provider
  @entity_provider
end

#namespace_uri_modeObject

Returns the value of attribute namespace_uri_mode.



30
31
32
# File 'lib/moxml/config.rb', line 30

def namespace_uri_mode
  @namespace_uri_mode
end

#preload_entity_setsObject

Returns the value of attribute preload_entity_sets.



30
31
32
# File 'lib/moxml/config.rb', line 30

def preload_entity_sets
  @preload_entity_sets
end

#restore_entitiesObject

Returns the value of attribute restore_entities.



30
31
32
# File 'lib/moxml/config.rb', line 30

def restore_entities
  @restore_entities
end

#strict_parsingObject

Returns the value of attribute strict_parsing.



30
31
32
# File 'lib/moxml/config.rb', line 30

def strict_parsing
  @strict_parsing
end

Class Method Details

.defaultObject



18
19
20
# File 'lib/moxml/config.rb', line 18

def default
  @default ||= new(default_adapter, true, "UTF-8")
end

Instance Method Details

#adapterObject



76
77
78
# File 'lib/moxml/config.rb', line 76

def adapter
  @adapter ||= Adapter.load(@adapter_name)
end

#adapter=(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/moxml/config.rb', line 55

def adapter=(name)
  name = name.to_sym
  @adapter = nil

  unless VALID_ADAPTERS.include?(name)
    raise Moxml::AdapterError.new(
      "Invalid adapter: #{name}",
      adapter: name,
      operation: "set_adapter",
    )
  end

  @adapter_name = name
  adapter
end

#default_adapter=(name) ⇒ Object



71
72
73
74
# File 'lib/moxml/config.rb', line 71

def default_adapter=(name)
  self.adapter = name
  self.class.default_adapter = name
end

#load_external_entitiesObject



108
109
110
# File 'lib/moxml/config.rb', line 108

def load_external_entities
  @entity_load_mode == :required
end

#load_external_entities=(value) ⇒ Object

Backward compatibility: convert old boolean to new symbol



100
101
102
103
104
105
106
# File 'lib/moxml/config.rb', line 100

def load_external_entities=(value)
  @entity_load_mode = case value
                      when true then :required
                      when false then :disabled
                      else value
                      end
end