Class: EimXML::PCString

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

Constant Summary collapse

ENCODING_MAP =
{
  '&' => '&',
  '"' => '"',
  "'" => ''',
  '<' => '&lt;',
  '>' => '&gt;'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, encoded = false) ⇒ PCString

rubocop:disable Style/OptionalBooleanParameter



32
33
34
35
# File 'lib/eim_xml.rb', line 32

def initialize(src, encoded = false) # rubocop:disable Style/OptionalBooleanParameter
  @src = src
  @encoded_string = encoded ? src : PCString.encode(src)
end

Instance Attribute Details

#encoded_stringObject (readonly) Also known as: to_s

Returns the value of attribute encoded_string.



11
12
13
# File 'lib/eim_xml.rb', line 11

def encoded_string
  @encoded_string
end

#srcObject (readonly)

Returns the value of attribute src.



11
12
13
# File 'lib/eim_xml.rb', line 11

def src
  @src
end

Class Method Details

.[](obj) ⇒ Object



28
29
30
# File 'lib/eim_xml.rb', line 28

def self.[](obj)
  obj.is_a?(PCString) ? obj : PCString.new(obj)
end

.encode(src) ⇒ Object



22
23
24
25
26
# File 'lib/eim_xml.rb', line 22

def self.encode(src)
  src.to_s.gsub(/[&"'<>]/) do |m|
    ENCODING_MAP[m]
  end
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
# File 'lib/eim_xml.rb', line 37

def ==(other)
  other.is_a?(PCString) ? @encoded_string == other.encoded_string : self == PCString.new(other)
end

#write_to(out = '') ⇒ Object



41
42
43
# File 'lib/eim_xml.rb', line 41

def write_to(out = '')
  out << encoded_string
end