Class: TinMan::Article
Instance Method Summary
collapse
Methods included from Template
included, #method_missing, #redcloth
Constructor Details
#initialize(obj, config = {}) ⇒ Article
Returns a new instance of Article.
217
218
219
220
|
# File 'lib/tinman.rb', line 217
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 TinMan::Template
Instance Method Details
239
240
241
242
|
# File 'lib/tinman.rb', line 239
def [] key
self.load unless self.tainted?
super
end
|
273
|
# File 'lib/tinman.rb', line 273
def author() self[:author] || @config[:author] end
|
263
264
265
|
# File 'lib/tinman.rb', line 263
def body
redcloth self[:body].sub(@config[:summary][:delim], '') rescue redcloth self[:body]
end
|
272
|
# File 'lib/tinman.rb', line 272
def date() @config[:date].call(self[:date]) end
|
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/tinman.rb', line 222
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
|
267
268
269
|
# File 'lib/tinman.rb', line 267
def path
@config[:prefix] + self[:date].strftime("/%Y/%m/%d/#{slug}/")
end
|
244
245
246
|
# File 'lib/tinman.rb', line 244
def slug
self[:slug] || self[:title].slugize
end
|
#summary(length = nil) ⇒ Object
248
249
250
251
252
253
254
255
256
|
# File 'lib/tinman.rb', line 248
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
redcloth(sum.length == self[:body].length ? sum : sum.strip.sub(/\.\Z/, '…'))
end
|
271
|
# File 'lib/tinman.rb', line 271
def title() self[:title] || "an article" end
|
#to_html ⇒ Object
Also known as:
to_s
274
|
# File 'lib/tinman.rb', line 274
def to_html() self.load; super(:article, @config) end
|
#url ⇒ Object
Also known as:
permalink
258
259
260
|
# File 'lib/tinman.rb', line 258
def url
"http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}"
end
|