Class: Toto::Article

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

Instance Method Summary collapse

Methods included from Template

included

Constructor Details

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

Returns a new instance of Article.



161
162
163
164
# File 'lib/toto.rb', line 161

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

#method_missing(m, *args, &blk) ⇒ Object



208
209
210
# File 'lib/toto.rb', line 208

def method_missing m, *args, &blk
  self.keys.include?(m) ? self[m] : super
end

Instance Method Details

#[](key) ⇒ Object



180
181
182
183
# File 'lib/toto.rb', line 180

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

#authorObject



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

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

#bodyObject



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

def body()    markdown self[:body]                       end

#dateObject



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

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

#loadObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/toto.rb', line 166

def load
  data = if @obj.is_a? File
    meta, self[:body] = @obj.read.split(/\n\n/, 2)
    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



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

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

#slugObject



186
187
188
189
# File 'lib/toto.rb', line 186

def slug
  self[:slug] ||
  self[:title].downcase.gsub(/&/, 'and').gsub(/\s+/, '-').gsub(/[^a-z0-9-]/, '')
end

#summary(length = ) ⇒ Object



191
192
193
# File 'lib/toto.rb', line 191

def summary length = @config[:summary]
  markdown self[:body].match(/(.{1,#{length}}.*?)(\n|\Z)/m).to_s
end

#titleObject



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

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

#to_htmlObject Also known as: to_s



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

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

#urlObject



195
196
197
# File 'lib/toto.rb', line 195

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