Class: Jfm::Post
- Inherits:
-
Object
- Object
- Jfm::Post
- Defined in:
- lib/jfm.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
- #frontmatter ⇒ Object
- #frontmatter=(new_frontmatter) ⇒ Object
-
#initialize(filename) ⇒ Post
constructor
A new instance of Post.
- #match?(query) ⇒ Boolean
- #save ⇒ Object
Constructor Details
#initialize(filename) ⇒ Post
Returns a new instance of Post.
28 29 30 |
# File 'lib/jfm.rb', line 28 def initialize(filename) @filename = filename end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
26 27 28 |
# File 'lib/jfm.rb', line 26 def filename @filename end |
Instance Method Details
#frontmatter ⇒ Object
32 33 34 |
# File 'lib/jfm.rb', line 32 def frontmatter @frontmatter ||= YAML.load(raw_frontmatter) end |
#frontmatter=(new_frontmatter) ⇒ Object
36 37 38 |
# File 'lib/jfm.rb', line 36 def frontmatter=(new_frontmatter) @frontmatter = new_frontmatter end |
#match?(query) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/jfm.rb', line 40 def match?(query) case query when /(.+?):\s*~(.+)/ frontmatter[$1] != $2 when /(.+?):\s*(.+)/ frontmatter[$1] == $2 else frontmatter.has_key?(query) end end |
#save ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/jfm.rb', line 51 def save backmatter = content.lines.drop_while { |l| l.chomp == "---" }.drop_while { |l| l.chomp != "---" }.drop(1).join File.open(filename, "w") do |file| file.write YAML::dump(frontmatter) file.puts "---" file.write backmatter end end |