Module: ZergXcode::Encoder

Defined in:
lib/zerg_xcode/file_format/encoder.rb

Overview

Writes flattened object graphs to .xcodeproj files.

Class Method Summary collapse

Class Method Details

.encode(project) ⇒ Object



13
14
15
# File 'lib/zerg_xcode/file_format/encoder.rb', line 13

def self.encode(project)
  "// !$*UTF8*$!\n" + encode_hash(project, 0) + "\n"
end

.encode_array(array, indentation) ⇒ Object



36
37
38
39
40
41
# File 'lib/zerg_xcode/file_format/encoder.rb', line 36

def self.encode_array(array, indentation)
  "(\n" + array.map { |value|
    encode_indentation(indentation + 1) +
    encode_object(value, indentation + 1) + ",\n"
  }.join + encode_indentation(indentation) + ")"
end

.encode_hash(hash, indentation) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/zerg_xcode/file_format/encoder.rb', line 17

def self.encode_hash(hash, indentation)
  "{\n" + hash.keys.sort.map { |key|
    encode_indentation(indentation + 1) + 
    encode_value(key, indentation + 1) + " = " +
    encode_object(hash[key], indentation + 1) + ";\n"
  }.join + encode_indentation(indentation) + "}"
end

.encode_indentation(indentation) ⇒ Object



48
49
50
# File 'lib/zerg_xcode/file_format/encoder.rb', line 48

def self.encode_indentation(indentation)
  "\t" * indentation
end

.encode_object(object, indentation) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/zerg_xcode/file_format/encoder.rb', line 25

def self.encode_object(object, indentation)
  case object
  when Hash
    encode_hash object, indentation
  when Array
    encode_array object, indentation
  when String
    encode_value object, indentation
  end
end

.encode_value(value, indentation) ⇒ Object



43
44
45
46
# File 'lib/zerg_xcode/file_format/encoder.rb', line 43

def self.encode_value(value, indentation)
  # This escapes what needs to be escaped.
  value.to_s.inspect
end