Module: Preamble

Defined in:
lib/preamble.rb,
lib/preamble/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object



6
7
8
9
10
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
45
46
47
48
49
50
51
52
53
# File 'lib/preamble.rb', line 6

def self.load(path)

  preamble_lines = []
  body_lines = []

  state = :before_preamble

  open(path).each do |line|

    stripped = line.strip

    case state

      when :before_preamble

        new_state = case stripped
          when "---" 
            :preamble
          when "" 
            :before_preamble
          else 
            raise "First line must begin with ---"
        end

      when :preamble

        new_state = case stripped
          when "---" 
            :after_preamble
          else 
            preamble_lines << line
            :preamble
        end

      when :after_preamble
        new_state = :after_preamble
        body_lines << line

      else raise "Invalid State: #{ state }"
    end

    state = new_state

  end

  return [YAML::load(preamble_lines.join), body_lines.join]

end

.load_multiple(*paths) ⇒ Object



55
56
57
# File 'lib/preamble.rb', line 55

def self.load_multiple(*paths)
  paths.map{ |path| Preamble.load(path) }
end