Class: Glinda::Article

Inherits:
Hash show all
Includes:
Template, NewRelic::Agent::MethodTracer
Defined in:
lib/glinda.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.



301
302
303
304
# File 'lib/glinda.rb', line 301

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 Glinda::Template

Instance Method Details

#[](key) ⇒ Object



323
324
325
326
# File 'lib/glinda.rb', line 323

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

#authorObject



365
# File 'lib/glinda.rb', line 365

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

#bodyObject



347
348
349
# File 'lib/glinda.rb', line 347

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

#dateObject



364
# File 'lib/glinda.rb', line 364

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

#loadObject



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/glinda.rb', line 306

def load
  data = if @obj.is_a? String
    meta, self[:body] = File.read(@obj).split(/\n\n/, 2)

    # use the date from the filename, or else tinman won't find the article
    @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

#pathObject



351
352
353
# File 'lib/glinda.rb', line 351

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

#slugObject



328
329
330
# File 'lib/glinda.rb', line 328

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

#summary(length = nil) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/glinda.rb', line 332

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

#tagsObject



355
356
357
358
359
360
361
# File 'lib/glinda.rb', line 355

def tags
  if self[:tags]
    self[:tags].split(',').map(&:strip)
  else
    []
  end
end

#titleObject



363
# File 'lib/glinda.rb', line 363

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

#to_htmlObject Also known as: to_s



366
# File 'lib/glinda.rb', line 366

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

#urlObject Also known as: permalink



342
343
344
# File 'lib/glinda.rb', line 342

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