Class: Toto::Article

Inherits:
Hash show all
Includes:
Template
Defined in:
lib/toto.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.



236
237
238
239
# File 'lib/toto.rb', line 236

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



255
256
257
258
# File 'lib/toto.rb', line 255

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

#authorObject



286
# File 'lib/toto.rb', line 286

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

#bodyObject



279
280
281
# File 'lib/toto.rb', line 279

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

#dateObject



284
# File 'lib/toto.rb', line 284

def date()    @config[:date].call(self[:date])           end

#loadObject



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/toto.rb', line 241

def load
  data = if @obj.is_a? String
    meta, self[:body] = File.read(@obj).split(/\n\n/, 2)
    YAML.load(meta).merge(:title => File.basename(@obj).chomp('.txt').humanize)
  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



285
# File 'lib/toto.rb', line 285

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

#slugObject



260
261
262
# File 'lib/toto.rb', line 260

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

#summary(length = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
# File 'lib/toto.rb', line 264

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



283
# File 'lib/toto.rb', line 283

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

#to_htmlObject Also known as: to_s



287
# File 'lib/toto.rb', line 287

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

#urlObject Also known as: permalink



274
275
276
# File 'lib/toto.rb', line 274

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