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



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

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



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

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

#full_image_pathObject



83
84
85
86
# File 'lib/ruby_slippers/article.rb', line 83

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

#has_more_then_summary?Boolean

Returns:

  • (Boolean)


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

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

#image_srcObject



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

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


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

def related_articles
  Site.send(:articles, :txt).each do |article|
    raise article.to_yaml
    Article.new article, @config
  end
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/, '…'))
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



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

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

#to_htmlObject Also known as: to_s



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

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