Class: Object

Inherits:
BasicObject
Defined in:
lib/base/helpers/support.xml.rb,
lib/base/helpers/support.rb

Overview

– Copyright © 2013 RightScale, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Constant Summary collapse

RIGHTXMLSUPPORT_XMLESCAPE =

:nodoc:

{'"' => '&quot;', '\'' =>'&apos;', '<' => '&lt;', '>' => '&gt;'}
RIGHTXMLSUPPORT_XMLUNESCAPE =
RIGHTXMLSUPPORT_XMLESCAPE.invert
RIGHTXMLSUPPORT_XMLINDENT =
""
RIGHTXMLSUPPORT_XMLLEVEL =
0
RIGHTXMLSUPPORT_XMLCRLF =
"\n"

Instance Method Summary collapse

Instance Method Details

#_arrayifyArray

Wraps the object into an array.

Examples:

nil._arrayify  #=> []
1._arrayify    #=> [1]
:sym._arrayify #=> [:sym]


148
149
150
# File 'lib/base/helpers/support.rb', line 148

def _arrayify
  Array(self)
end

#_blank?Boolean

Checks if the current object is blank or empty. “”, “ ”, nil, [] and {} are assumes as blank.



107
108
109
110
111
112
113
# File 'lib/base/helpers/support.rb', line 107

def _blank?
  case
  when respond_to?(:blank?) then blank?
  when respond_to?(:empty?) then empty?
  else                           !self
  end
end

#_extendedArray

Returns a list of modules an object is extended with.



127
128
129
# File 'lib/base/helpers/support.rb', line 127

def _extended
  (class << self; self; end).included_modules
end

#_extended?(_module) ⇒ Boolean

Checks whether an object was extended with a module.



135
136
137
# File 'lib/base/helpers/support.rb', line 135

def _extended?(_module)
  _extended.include?(_module)
end

#_present?Boolean

Checks if the object has any non-blank value (opposite to Object#_blank?)



119
120
121
# File 'lib/base/helpers/support.rb', line 119

def _present?
  !_blank?
end

#_to_xml(opts = {}) ⇒ String

Returns an XML-representation of the object.

Options Hash (opts):

  • :escape (Boolean)

    The flag.



93
94
95
# File 'lib/base/helpers/support.xml.rb', line 93

def _to_xml(opts={})
  _xml_conditional_escape(_xml_get_opts(opts))
end

#_to_xml!(*args) ⇒ String

Returns an XML-representation of the object starting with ‘<?xml version=“1.0” encoding=“UTF-8”?>’ string.



104
105
106
107
# File 'lib/base/helpers/support.xml.rb', line 104

def _to_xml!(*args)
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
  "#{_to_xml(*args)}"
end

#_xml_align(opts = {}) ⇒ Object

Returns an aligned piece of XML text.



79
80
81
82
83
# File 'lib/base/helpers/support.xml.rb', line 79

def _xml_align(opts={}) # :nodoc:
  return '' if self.to_s.empty?
  opts = _xml_get_opts(opts)
  "#{opts[:indent]*opts[:level]}#{self}#{opts[:crlf]}"
end

#_xml_conditional_escape(opts = {}) ⇒ String

Conditionally escapes non-XML symbols.

Options Hash (opts):

  • :escape (Boolean)

    The flag.



52
53
54
# File 'lib/base/helpers/support.xml.rb', line 52

def _xml_conditional_escape(opts={})
  opts[:escape] ? self._xml_escape : self.to_s
end

#_xml_escapeString

Escapes non-XML symbols.

Examples:

"Hello <'world'> & \"the Universe\""._xml_escape #=>
  "Hello &lt;&apos;world&apos;&gt; &amp; &quot;the Universe&quot;"


40
41
42
# File 'lib/base/helpers/support.xml.rb', line 40

def _xml_escape
  self.to_s.gsub('&', '&amp;').gsub(/#{RIGHTXMLSUPPORT_XMLESCAPE.keys.join('|')}/) { |match| RIGHTXMLSUPPORT_XMLESCAPE[match] }
end

#_xml_get_opts(opts = {}) ⇒ Object

Fixes the given set of options.



70
71
72
73
74
75
# File 'lib/base/helpers/support.xml.rb', line 70

def _xml_get_opts(opts={}) # :nodoc:
  opts[:level]  ||= RIGHTXMLSUPPORT_XMLLEVEL
  opts[:indent] ||= RIGHTXMLSUPPORT_XMLINDENT
  opts[:crlf]   ||= opts[:indent].empty? ? "" : RIGHTXMLSUPPORT_XMLCRLF
  opts
end

#_xml_unescapeString

Unescapes XML-escaped symbols.

Examples:

"Hello &lt;&apos;world&apos;&gt; &amp; &quot;the Universe&quot;"._xml_unescape #=>
  "Hello <'world'> & \"the Universe\"


64
65
66
# File 'lib/base/helpers/support.xml.rb', line 64

def _xml_unescape
  self.to_s.gsub(/#{RIGHTXMLSUPPORT_XMLUNESCAPE.keys.join('|')}/) { |match| RIGHTXMLSUPPORT_XMLUNESCAPE[match] }.gsub('&amp;','&')
end