Class: Rollin::Article
- Inherits:
-
Object
- Object
- Rollin::Article
- Defined in:
- lib/rollin/article.rb
Instance Attribute Summary collapse
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Instance Method Summary collapse
- #body ⇒ Object
- #date ⇒ Object
-
#initialize(source_file) ⇒ Article
constructor
A new instance of Article.
- #matches?(search) ⇒ Boolean
- #metatags ⇒ Object
- #published? ⇒ Boolean
- #title ⇒ Object
Constructor Details
#initialize(source_file) ⇒ Article
Returns a new instance of Article.
4 5 6 7 8 9 10 11 12 |
# File 'lib/rollin/article.rb', line 4 def initialize(source_file) @source_file = source_file filename = File.basename(@source_file) @id = filename[0, filename.length - 3] @title_from_filename = filename[11, filename.length - 11 - 3].gsub('_', ' ') @year = filename[0, 4].to_i @month = filename[5, 2].to_i @day = filename[8, 2].to_i end |
Instance Attribute Details
#day ⇒ Object (readonly)
Returns the value of attribute day.
2 3 4 |
# File 'lib/rollin/article.rb', line 2 def day @day end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
2 3 4 |
# File 'lib/rollin/article.rb', line 2 def id @id end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
2 3 4 |
# File 'lib/rollin/article.rb', line 2 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
2 3 4 |
# File 'lib/rollin/article.rb', line 2 def year @year end |
Instance Method Details
#body ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rollin/article.rb', line 66 def body = { hard_wrap: false } = { autolink: true, space_after_headers: true, fenced_code_blocks: true, disable_indented_code_blocks: true } render = Redcarpet::Render::HTML.new() redcarpet = Redcarpet::Markdown.new(render, ) redcarpet.render(read_from_file.content) end |
#date ⇒ Object
62 63 64 |
# File 'lib/rollin/article.rb', line 62 def date Date.new(@year, @month, @day) end |
#matches?(search) ⇒ Boolean
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 |
# File 'lib/rollin/article.rb', line 22 def matches?(search) return @id == search if search.is_a? String search = search.inject({}) { |hash, (k,v)| hash[k.to_s] = v; hash } return false unless published? || show_unpublished?(search) if search.has_key?('year') return false if search.delete('year') != @year if search.has_key?('month') return false if search.delete('month') != @month if search.has_key?('day') return false if search.delete('day') != @day end end end if search.keys.empty? return true else search.each do |key, value| return true if [key] != nil && ([key] == value || [key].include?(value)) end return false end end |
#metatags ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rollin/article.rb', line 49 def file = read_from_file return {} unless file.has_front_matter? begin return YAML.safe_load(file.yaml_front_matter) rescue SyntaxError => e puts "YAML Exception reading #{File.join(@source_file)}: #{e.}" rescue Exception => e puts "Error reading file #{File.join(@source_file)}: #{e.}" end return {} end |
#published? ⇒ Boolean
14 15 16 |
# File 'lib/rollin/article.rb', line 14 def published? !.has_key?('published') || ['published'] == 'true' end |
#title ⇒ Object
18 19 20 |
# File 'lib/rollin/article.rb', line 18 def title ['title'] || @title_from_filename end |