Class: Baron::Article
- Inherits:
-
Hash
- Object
- Hash
- Baron::Article
- Defined in:
- lib/baron/models/article.rb
Class Method Summary collapse
Instance Method Summary collapse
- #author ⇒ Object
- #body ⇒ Object
- #category ⇒ Object
- #date ⇒ Object
- #date_iso8601 ⇒ Object
-
#initialize(file_parts, config = {}) ⇒ Article
constructor
A new instance of Article.
- #path(prefix = '', date_format = '') ⇒ Object
- #permalink ⇒ Object
- #slug ⇒ Object
- #slug_with_date ⇒ Object
- #summary(length = nil) ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(file_parts, config = {}) ⇒ Article
Returns a new instance of Article.
11 12 13 14 15 16 17 18 19 |
# File 'lib/baron/models/article.rb', line 11 def initialize file_parts, config = {} @config = config self[:filename_and_path] = file_parts[:filename_and_path] self[:filename_without_extension] = file_parts[:filename_without_extension] self[:slug] = file_parts[:filename] self[:category] = file_parts[:category].empty? ? '' : file_parts[:category] self[:date] = Date.parse(file_parts[:date].gsub('/', '-')) rescue Date.today load_article(file_parts[:filename_and_path]) end |
Class Method Details
.create_filename_without_extension(title, date) ⇒ Object
5 6 7 8 9 |
# File 'lib/baron/models/article.rb', line 5 def self.create_filename_without_extension title, date slug = title.empty? ? "Untitled" : title.strip.downcase slug_encoded = slug.gsub(/[^a-z0-9]/, '-') "#{date.strftime("%Y-%m-%d-") + slug_encoded}" end |
Instance Method Details
#author ⇒ Object
61 62 63 |
# File 'lib/baron/models/article.rb', line 61 def self[:author] || @config[:author] end |
#body ⇒ Object
31 32 33 |
# File 'lib/baron/models/article.rb', line 31 def body markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body] end |
#category ⇒ Object
65 66 67 |
# File 'lib/baron/models/article.rb', line 65 def category self[:category] end |
#date ⇒ Object
52 53 54 |
# File 'lib/baron/models/article.rb', line 52 def date @config[:date].call(self[:date]) end |
#date_iso8601 ⇒ Object
56 57 58 59 |
# File 'lib/baron/models/article.rb', line 56 def date_iso8601 # "2009-10-26T04:47:09Z" self[:date].strftime("%FT%T%:z") end |
#path(prefix = '', date_format = '') ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/baron/models/article.rb', line 35 def path prefix = '', date_format = '' permalink_prefix = prefix.empty? ? @config[:permalink_prefix] : prefix permalink_date_format = date_format.empty? ? @config[:permalink_date_format] : date_format date_path = case permalink_date_format when :year_date; self[:date].strftime("/%Y") when :year_month_date; self[:date].strftime("/%Y/%m") when :year_month_day_date; self[:date].strftime("/%Y/%m/%d") else '' end "/#{permalink_prefix}/#{self[:category]}#{date_path}/#{slug}/".squeeze('/') end |
#permalink ⇒ Object
69 70 71 |
# File 'lib/baron/models/article.rb', line 69 def permalink "http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}" end |
#slug ⇒ Object
73 74 75 |
# File 'lib/baron/models/article.rb', line 73 def slug self[:slug] end |
#slug_with_date ⇒ Object
77 78 79 |
# File 'lib/baron/models/article.rb', line 77 def slug_with_date end |
#summary(length = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/baron/models/article.rb', line 21 def summary length = nil config = @config[:summary] sum = if self[:body] =~ config[:delim] self[:body].split(config[:delim]).first else self[:body].match(/(.{1,#{length || config[:length] || config[:max]}}.*?)(\n|\Z)/m).to_s end markdown(sum.length == self[:body].length ? sum : sum.strip.sub(/\.\Z/, @config[:truncation_marker])) end |
#title ⇒ Object
48 49 50 |
# File 'lib/baron/models/article.rb', line 48 def title self[:title] || 'Untitled' end |