Class: SOML::Document

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/soml/document.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = []) ⇒ Document

Returns a new instance of Document.



5
6
7
# File 'lib/soml/document.rb', line 5

def initialize(fields = [])
    @fields = fields
end

Class Method Details

.parse(soml) ⇒ Object



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
# File 'lib/soml/document.rb', line 25

def self.parse(soml)
    in_field = false
    state = Struct.new(:key, :value, :fields, :delim).new(nil, [], [], nil)

    soml.lines.map(&:chomp).each do |line|
        if in_field && line == state.delim
            state.fields << Field.new(state.key, Util.dedent(state.value.join("\n")))
            in_field = false
        elsif in_field
            state.value << line
        else
            next if line.strip[0] == '#'

            state.key, value = line.strip.split(' ', 2)
            if value =~ /<<(.*)/
                in_field = true
                state.value = []
                state.delim = $1
            else
                state.fields << Field.new(state.key, value)
            end
        end
    end

    Document.new(state.fields)
end

Instance Method Details

#[](i) ⇒ Object



13
14
15
# File 'lib/soml/document.rb', line 13

def [](i)
    @fields[i]
end

#add(name, value) ⇒ Object



21
22
23
# File 'lib/soml/document.rb', line 21

def add(name, value)
    @fields << Field.new(name, value)
end

#each(&block) ⇒ Object



9
10
11
# File 'lib/soml/document.rb', line 9

def each(&block)
    @fields.each(&block)
end

#to_sObject



17
18
19
# File 'lib/soml/document.rb', line 17

def to_s
    @fields.join("\n")
end