Class: Toto::Article
Instance Method Summary
collapse
Methods included from Template
included, #markdown, #method_missing
Constructor Details
#initialize(obj, config = {}) ⇒ Article
Returns a new instance of Article.
224
225
226
227
|
# File 'lib/toto.rb', line 224
def initialize obj, config = {}
@obj, @config = obj, config
self.load if obj.is_a? Hash
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Toto::Template
Instance Method Details
246
247
248
249
|
# File 'lib/toto.rb', line 246
def [] key
self.load unless self.tainted?
super
end
|
280
|
# File 'lib/toto.rb', line 280
def author() self[:author] || @config[:author] end
|
270
271
272
|
# File 'lib/toto.rb', line 270
def body
markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body]
end
|
279
|
# File 'lib/toto.rb', line 279
def date() @config[:date].call(self[:date]) end
|
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/toto.rb', line 229
def load
data = if @obj.is_a? String
meta, self[:body] = File.read(@obj).split(/\n\n/, 2)
@obj =~ /\/(\d{4}-\d{2}-\d{2})[^\/]*$/
($1 ? {:date => $1} : {}).merge(YAML.load(meta))
elsif @obj.is_a? Hash
@obj
end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) }
self.taint
self.update data
self[:date] = Date.parse(self[:date].gsub('/', '-')) rescue Date.today
self
end
|
274
275
276
|
# File 'lib/toto.rb', line 274
def path
"/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/')
end
|
251
252
253
|
# File 'lib/toto.rb', line 251
def slug
self[:slug] || self[:title].slugize
end
|
#summary(length = nil) ⇒ Object
255
256
257
258
259
260
261
262
263
|
# File 'lib/toto.rb', line 255
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/, '…'))
end
|
278
|
# File 'lib/toto.rb', line 278
def title() self[:title] || "an article" end
|
#to_html ⇒ Object
Also known as:
to_s
281
|
# File 'lib/toto.rb', line 281
def to_html() self.load; super(:article, @config) end
|
#url ⇒ Object
Also known as:
permalink
265
266
267
|
# File 'lib/toto.rb', line 265
def url
"http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}"
end
|