Module: Manifest

Defined in:
lib/manifest.rb

Constant Summary collapse

MANIFEST_LINE_SEP =
/\r\n|\n|\r[^\n]/
MANIFEST_SECTION_SEP =
/(#{MANIFEST_LINE_SEP}){2}/

Class Method Summary collapse

Class Method Details

.read(text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
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
# File 'lib/manifest.rb', line 23

def read(text)

    text.split(MANIFEST_SECTION_SEP).reject { |s| s.chomp == "" }.map do |section|
section.split(MANIFEST_LINE_SEP).each { |line| line.length < 72 }.inject([]) { |merged, line|
  if line[0] == ' '
    merged.last << line[1..-1]
  else
    merged << line
  end
  merged
  }.map { |line| line.split(/: /) }.inject({}) { |map, (name, values)|
    if (values.nil?)
      map.merge!(name=>nil)
    else
      valuesAsHash = {}
      
      nestedList = false
      values.split(/,/).inject([]) { |array, att1| 
        # split and then recompose. Not optimal at all but the manifest format is such a pain...
        if nestedList
          last = array.pop
          array << "#{last},#{att1}"
        else
          array << att1
        end

        index = 0
        if (/.*"$/.match(att1) || /.*";/.match(att1))
           nestedList = false
           index =  $~[0].size
        end
        if att1[index, att1.size].match(/"/)
           # if a " is in the value, it means we entered a subentry. And since it is not at the
           # end of the line, we can conclude we are in a nested list.
           nestedList = true
        end
        
        array
        }.each { |attribute|
          optionalAttributes = {}
          values = attribute.split(/;/)
          value = values.shift
          values.each {|attribute| 
            array = attribute.split(/:?=/) 
            optionalAttributes.merge!(array.first.strip=>array.last.strip)
          }
          valuesAsHash.merge!(value=>optionalAttributes)
        }
        map.merge!(name=>valuesAsHash) 
      end
    }
  end
end