Class: Jfm::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/jfm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#filenameObject (readonly)

Returns the value of attribute filename.



26
27
28
# File 'lib/jfm.rb', line 26

def filename
  @filename
end

Instance Method Details

#frontmatterObject



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

Returns:

  • (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

#saveObject



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