Class: PropertyList::AsciiGenerator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(indent_unit: "\t", initial_indent: '', sort_keys: true, gnu_extension: true) ⇒ AsciiGenerator

Returns a new instance of AsciiGenerator.



22
23
24
25
26
27
28
29
30
# File 'lib/property-list/ascii_generator.rb', line 22

def initialize indent_unit: "\t", initial_indent: '', sort_keys: true, gnu_extension: true
  @indent_unit = indent_unit
  @indent_level = 0
  @initial_indent = initial_indent
  @indent = @initial_indent + @indent_unit * @indent_level
  @sort_keys = sort_keys
  @gnu_extension = gnu_extension
  @output = []
end

Instance Attribute Details

#outputObject (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



136
137
138
139
140
141
142
143
144
145
# File 'lib/property-list/ascii_generator.rb', line 136

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



162
163
164
165
166
# File 'lib/property-list/ascii_generator.rb', line 162

def ascii_data content
  hex, _ = content.unpack 'H*'
  hex.gsub! /(.{2})/, "\\1 "
  @output << "< #{hex}>"
end

#ascii_hash_content(object) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/property-list/ascii_generator.rb', line 147

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/property-list/ascii_generator.rb', line 113

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



100
101
102
103
# File 'lib/property-list/ascii_generator.rb', line 100

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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
    if @gnu_extension
      ascii_value "<*BY>"
    else
      raise UnsupportedTypeError, 'TrueClass'
    end
  when false
    if @gnu_extension
      ascii_value "<*BN>"
    else
      raise UnsupportedTypeError, 'FalseClass'
    end
  when Float
    if @gnu_extension
      if object.to_i == object
        object = object.to_i
      end
      ascii_value "<*R#{object}>"
    else
      raise UnsupportedTypeError, 'Float'
    end
  when Integer
    if @gnu_extension
      ascii_value "<*I#{object}>"
    else
      raise UnsupportedTypeError, 'Integer'
    end
  when Time, Date # also covers DateTime
    if @gnu_extension
      ascii_value "<*D#{object.strftime '%Y-%m-%d %H:%M:%S %z'}>"
    else
      raise UnsupportedTypeError, object.class.to_s
    end
  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 UnsupportedTypeError, object.class.to_s
  end
end