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
- #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 |
# 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[: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_slug(title, date) ⇒ Object
5 6 7 8 9 |
# File 'lib/baron/models/article.rb', line 5 def self.create_slug 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
60 61 62 |
# File 'lib/baron/models/article.rb', line 60 def self[:author] || @config[:author] end |
#body ⇒ Object
30 31 32 |
# File 'lib/baron/models/article.rb', line 30 def body markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body] end |
#category ⇒ Object
64 65 66 |
# File 'lib/baron/models/article.rb', line 64 def category self[:category] end |
#date ⇒ Object
51 52 53 |
# File 'lib/baron/models/article.rb', line 51 def date @config[:date].call(self[:date]) end |
#date_iso8601 ⇒ Object
55 56 57 58 |
# File 'lib/baron/models/article.rb', line 55 def date_iso8601 # "2009-10-26T04:47:09Z" self[:date].strftime("%FT%T%:z") end |
#path(prefix = '', date_format = '') ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/baron/models/article.rb', line 34 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
68 69 70 |
# File 'lib/baron/models/article.rb', line 68 def permalink "http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}" end |
#slug ⇒ Object
72 73 74 |
# File 'lib/baron/models/article.rb', line 72 def slug self[:slug] end |
#summary(length = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/baron/models/article.rb', line 20 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
47 48 49 |
# File 'lib/baron/models/article.rb', line 47 def title self[:title] || 'Untitled' end |