Class: Moxml::XmlUtils::Encoder

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

Constant Summary collapse

MAPPINGS =
{
  none: {},
  basic: {
    "<" => "&lt;",
    ">" => "&gt;",
    "&" => "&amp;",
  },
  quotes: {
    "'" => "&apos;",
    '"' => "&quot;",
  },
  full: {
    "<" => "&lt;",
    ">" => "&gt;",
    "'" => "&apos;",
    '"' => "&quot;",
    "&" => "&amp;",
  },
}.freeze
MODES =
MAPPINGS.keys.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, mode = nil) ⇒ Encoder

Returns a new instance of Encoder.



29
30
31
32
# File 'lib/moxml/xml_utils/encoder.rb', line 29

def initialize(text, mode = nil)
  @text = text
  @mode = valid_mode(mode)
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



6
7
8
# File 'lib/moxml/xml_utils/encoder.rb', line 6

def mode
  @mode
end

Instance Method Details

#callObject



34
35
36
37
38
39
40
# File 'lib/moxml/xml_utils/encoder.rb', line 34

def call
  return @text if mode == :none

  @text.to_s.gsub(/[#{mapping.keys.join}]/) do |match|
    mapping[match]
  end
end