Class: Preamble

Inherits:
Object
  • Object
show all
Defined in:
lib/preamble.rb,
lib/preamble/version.rb

Constant Summary collapse

DEFAULTS =
{
  :external_encoding => Encoding.default_external
}
VERSION =
"0.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, content) ⇒ Preamble

Returns a new instance of Preamble.



12
13
14
15
# File 'lib/preamble.rb', line 12

def initialize(, content)
  @metadata = 
  @content = content
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/preamble.rb', line 10

def content
  @content
end

#metadataObject

Returns the value of attribute metadata.



10
11
12
# File 'lib/preamble.rb', line 10

def 
  @metadata
end

Class Method Details

.load(path, options = {}) ⇒ Object



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
76
77
78
# File 'lib/preamble.rb', line 29

def self.load(path, options = {})
  preamble_lines = String.new
  content_lines = String.new
  options = DEFAULTS.merge(options)
  
  state = :before_preamble
  
  open(path, "r:#{options[:external_encoding]}") do |f|
    f.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
          content_lines << line

        else
          raise "Invalid State: #{ state }"

      end

      state = new_state
    end
  end

  return new(YAML::load(preamble_lines), content_lines)
end

.load_multiple(*paths) ⇒ Object



80
81
82
83
# File 'lib/preamble.rb', line 80

def self.load_multiple(*paths)
  options = paths.last.is_a?(Hash) ? paths.pop : {}
  paths.map{ |path| Preamble.load(path, options) }
end

Instance Method Details

#metadata_with_contentObject



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

def 
  @metadata.to_yaml + "---\n" + @content
end

#save(path, options = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/preamble.rb', line 21

def save(path, options = {})
  options = DEFAULTS.merge(options)

  open(path, "w:#{options[:external_encoding]}") do |f|
    f.write 
  end
end