Class: Preamble
- Inherits:
-
Object
- Object
- Preamble
- 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
-
#content ⇒ Object
Returns the value of attribute content.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(metadata, content) ⇒ Preamble
constructor
A new instance of Preamble.
- #metadata_with_content ⇒ Object
- #save(path, options = {}) ⇒ Object
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
#content ⇒ Object
Returns the value of attribute content.
10 11 12 |
# File 'lib/preamble.rb', line 10 def content @content end |
#metadata ⇒ Object
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, = {}) preamble_lines = String.new content_lines = String.new = DEFAULTS.merge() state = :before_preamble open(path, "r:#{[: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 |
Instance Method Details
#metadata_with_content ⇒ Object
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, = {}) = DEFAULTS.merge() open(path, "w:#{[:external_encoding]}") do |f| f.write end end |