Class: NokogiriPList::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri-plist/generator.rb

Class Method Summary collapse

Class Method Details

.array_or_hash?(item) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/nokogiri-plist/generator.rb', line 50

def self.array_or_hash?(item)
  item.is_a?(Hash) or item.is_a?(Array)
end

.indent(number) ⇒ Object



46
47
48
# File 'lib/nokogiri-plist/generator.rb', line 46

def self.indent(number)
  " " * (number * indent_size)
end

.indent_sizeObject



7
8
9
# File 'lib/nokogiri-plist/generator.rb', line 7

def self.indent_size
  2
end

.tag(name, content = nil, current_indent = 0, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nokogiri-plist/generator.rb', line 54

def self.tag(name, content=nil, current_indent=0, &block)
  if content or block_given?
    new_line = (["array", "dict"].include? name) ? "\n" : ""
    closing_tag_indent = (new_line != "\n") ? 0 : current_indent
    indent(current_indent) + "<#{name}>" + new_line +
    (block_given? ? yield : content).to_s +
    indent(closing_tag_indent) + "</#{name}>\n"
  else
    indent(current_indent) + "<#{name}/>\n"
  end
end

.to_s(value, current_indent = 0) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nokogiri-plist/generator.rb', line 11

def self.to_s(value, current_indent = 0)
# Todo: make these procs and allow custom procs (for data for example)
case value
when Array
    tag("array", nil, current_indent) do
      value.inject("") do |result, item|
        result + item.to_plist_xml_unchomped(current_indent + 1)
      end
    end
  when Hash
    tag("dict", nil, current_indent) do
      value.inject("") do |result, (dict_key, dict_value)|
        result + tag("key", dict_key, current_indent + 1).chomp +
        dict_value.to_plist_xml_unchomped
      end
    end
  when TrueClass
    tag("true", nil, current_indent)
  when FalseClass
    tag("false", nil, current_indent)
  when Time
    tag("date", value.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), current_indent)
  when Date # also catches DateTime
    tag("date", value.strftime('%Y-%m-%dT%H:%M:%SZ'), current_indent)
  when String, Symbol
    tag("string", value, current_indent)
  when Fixnum, Bignum, Integer
    tag("integer", value, current_indent)
  when Float
    tag("real", value, current_indent)
  when BigDecimal
    tag("real", value.to_s('F'), current_indent)
  end
end