Class: PropertyList::AsciiGenerator
- Inherits:
-
Object
- Object
- PropertyList::AsciiGenerator
- Defined in:
- lib/property-list/ascii_generator.rb
Overview
:nodoc:
Constant Summary collapse
- TABLE_FOR_ASCII_STRING_ESCAPE =
{ "\\".ord => "\\\\", '"'.ord => '\\"', "\b".ord => '\b', "\n".ord => "\\n", "\r".ord => "\\r", "\t".ord => "\\t" }.freeze
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #ascii_collection(start_delim, end_delim) ⇒ Object
- #ascii_data(content) ⇒ Object
- #ascii_hash_content(object) ⇒ Object
- #ascii_string(s) ⇒ Object
- #ascii_value(v) ⇒ Object
- #generate(object, wrap = true) ⇒ Object
-
#initialize(indent_unit: "\t", initial_indent: '', sort_keys: true) ⇒ AsciiGenerator
constructor
A new instance of AsciiGenerator.
Constructor Details
#initialize(indent_unit: "\t", initial_indent: '', sort_keys: true) ⇒ AsciiGenerator
Returns a new instance of AsciiGenerator.
23 24 25 26 27 28 29 30 |
# File 'lib/property-list/ascii_generator.rb', line 23 def initialize indent_unit: "\t", initial_indent: '', sort_keys: true @indent_unit = indent_unit @indent_level = 0 @initial_indent = initial_indent @indent = @initial_indent + @indent_unit * @indent_level @sort_keys = sort_keys @output = [] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
31 32 33 |
# File 'lib/property-list/ascii_generator.rb', line 31 def output @output end |
Instance Method Details
#ascii_collection(start_delim, end_delim) ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/property-list/ascii_generator.rb', line 116 def ascii_collection start_delim, end_delim @output << @indent @output << start_delim @output << "\n" @indent = @initial_indent + @indent_unit * (@indent_level += 1) yield @indent = @initial_indent + @indent_unit * (@indent_level -= 1) @output << @indent @output << end_delim end |
#ascii_data(content) ⇒ Object
142 143 144 145 146 |
# File 'lib/property-list/ascii_generator.rb', line 142 def ascii_data content hex, _ = content.unpack 'H*' hex.gsub! /(.{2})/, "\\1 " @output << "< #{hex}>" end |
#ascii_hash_content(object) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/property-list/ascii_generator.rb', line 127 def ascii_hash_content object keys = object.keys keys.sort_by! &:to_s if @sort_keys keys.each do |k| v = object[k] reset_indent = @indent ascii_string k.to_s @output << ' = ' @indent = '' # ignores the indent for first line generate v @output << ";\n" @indent = reset_indent end end |
#ascii_string(s) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/property-list/ascii_generator.rb', line 93 def ascii_string s @output << @indent if s =~ /\A[\w\.\/]+\z/ @output << s return end # there is also single quote string, in which we can use new lines and '' for single quote @output << '"' s.unpack('U*').each do |c| if c > 127 # there is also a \OOO format, but we only generate the \U format @output << "\\U#{c.to_s(16).rjust 4, '0'}" elsif (escaped_c = TABLE_FOR_ASCII_STRING_ESCAPE[c]) @output << escaped_c else @output << c.chr end end @output << '"' end |
#ascii_value(v) ⇒ Object
80 81 82 83 |
# File 'lib/property-list/ascii_generator.rb', line 80 def ascii_value v @output << @indent @output << v end |
#generate(object, wrap = true) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/property-list/ascii_generator.rb', line 33 def generate object, wrap=true # See also # http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSPropertyList.html # The <...> extensions are from GNUStep case object when Array ascii_collection '(', ')' do object.each do |e| generate e @output << ",\n" end end when Hash if wrap ascii_collection '{', '}' do ascii_hash_content object end else ascii_hash_content object end when true ascii_value "<*BY>" when false ascii_value "<*BN>" when Float if object.to_i == object object = object.to_i end ascii_value "<*R#{object}>" when Integer ascii_value "<*I#{object}>" when Time, Date # also covers DateTime ascii_value "<*D#{object.strftime '%Y-%m-%d %H:%M:%S %z'}>" when String ascii_string object when Symbol ascii_string object.to_s when IO, StringIO object.rewind contents = object.read ascii_data contents else raise "Generating of this class is not supported" end end |