Class: RubySlippers::Engine::Article

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



8
9
10
11
# File 'lib/ruby_slippers/article.rb', line 8

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 RubySlippers::Engine::Template

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
# File 'lib/ruby_slippers/article.rb', line 30

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

#authorObject



92
# File 'lib/ruby_slippers/article.rb', line 92

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

#bodyObject



54
55
56
# File 'lib/ruby_slippers/article.rb', line 54

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

#dateObject



91
# File 'lib/ruby_slippers/article.rb', line 91

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

#full_image_pathObject



76
77
78
79
# File 'lib/ruby_slippers/article.rb', line 76

def full_image_path
  return nil unless self[:image]
  self[:date].strftime("/img/articles/%Y/%B/#{self[:image]}").downcase
end

#has_more?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby_slippers/article.rb', line 81

def has_more?
  self[:body].length > self.summary.length
end

#image_srcObject



93
# File 'lib/ruby_slippers/article.rb', line 93

def image_src() full_image_path                             end

#loadObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_slippers/article.rb', line 13

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 ruby-slippers 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



58
59
60
# File 'lib/ruby_slippers/article.rb', line 58

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


85
86
87
88
# File 'lib/ruby_slippers/article.rb', line 85

def read_more_link()
  return "" unless has_more?
  "<div class=\"more-link\"><a href=\"#{article.path}\">read on &raquo;</a></div>"
end

#slugObject



35
36
37
# File 'lib/ruby_slippers/article.rb', line 35

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

#summary(length = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/ruby_slippers/article.rb', line 39

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-1 ? sum : sum.strip.sub(/\.\Z/, '&hellip;'))
end


69
70
71
72
73
74
# File 'lib/ruby_slippers/article.rb', line 69

def tag_links
  return [] unless self[:tags]
  self[:tags].split(',').collect do |tag|
    "<a href=\"/tagged/#{tag.strip.slugize}\">#{tag.strip.humanize.downcase}</a>"
  end.join(@config[:tag_separator])
end

#tagsObject



62
63
64
65
66
67
# File 'lib/ruby_slippers/article.rb', line 62

def tags
  return [] unless self[:tags]
  self[:tags].split(',').collect do |tag|
    "#{tag.strip.humanize.downcase}"
  end.join(@config[:tag_separator])
end

#titleObject



90
# File 'lib/ruby_slippers/article.rb', line 90

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

#to_htmlObject Also known as: to_s



94
# File 'lib/ruby_slippers/article.rb', line 94

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

#urlObject Also known as: permalink



49
50
51
# File 'lib/ruby_slippers/article.rb', line 49

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