Class: Toto::Article

Inherits:
Hash show all
Includes:
Template
Defined in:
lib/toto-haml.rb

Instance Method Summary collapse

Methods included from Template

included, #markdown, #method_missing

Constructor Details

#initialize(obj, config = {}) ⇒ Article

Returns a new instance of Article.



218
219
220
221
# File 'lib/toto-haml.rb', line 218

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

#[](key) ⇒ Object



238
239
240
241
# File 'lib/toto-haml.rb', line 238

def [] key
  self.load unless self.tainted?
  super
end

#authorObject



269
# File 'lib/toto-haml.rb', line 269

def author()  self[:author] || @config[:author]          end

#bodyObject



262
263
264
# File 'lib/toto-haml.rb', line 262

def body
  markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body]
end

#dateObject



267
# File 'lib/toto-haml.rb', line 267

def date()    @config[:date, self[:date]]                end

#loadObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/toto-haml.rb', line 223

def load
  data = if @obj.is_a? File
    meta, self[:body] = @obj.read.split(/\n\n/, 2)
    @obj.close
    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] = Time.parse(self[:date].gsub('/', '-')) rescue Time.now
  self
end

#pathObject



268
# File 'lib/toto-haml.rb', line 268

def path()    self[:date].strftime("/%Y/%m/%d/#{slug}/") end

#slugObject



243
244
245
# File 'lib/toto-haml.rb', line 243

def slug
  self[:slug] || self[:title].slugize
end

#summary(length = nil) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/toto-haml.rb', line 247

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

#titleObject



266
# File 'lib/toto-haml.rb', line 266

def title()   self[:title] || "an article"               end

#to_htmlObject Also known as: to_s



270
# File 'lib/toto-haml.rb', line 270

def to_html() self.load; super(:article)                 end

#urlObject Also known as: permalink



257
258
259
# File 'lib/toto-haml.rb', line 257

def url
  "http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}"
end