Class: Blog::Archive

Inherits:
Object
  • Object
show all
Defined in:
app/models/blog/archive.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blog, year, month = nil) {|_self| ... } ⇒ Archive

Returns a new instance of Archive.

Yields:

  • (_self)

Yield Parameters:

  • _self (Blog::Archive)

    the object that the method was called on



5
6
7
8
9
# File 'app/models/blog/archive.rb', line 5

def initialize(blog, year, month=nil)
  @blog, @year = blog, year.to_i
  @month = month.to_i if month
  yield self if block_given?
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



3
4
5
# File 'app/models/blog/archive.rb', line 3

def count
  @count
end

#monthObject (readonly)

Returns the value of attribute month.



2
3
4
# File 'app/models/blog/archive.rb', line 2

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



2
3
4
# File 'app/models/blog/archive.rb', line 2

def year
  @year
end

Class Method Details

.build(blog, options) ⇒ Object



49
50
51
52
53
54
55
# File 'app/models/blog/archive.rb', line 49

def self.build(blog, options)
  options[:group] ||= [:year, :month]
  
  group(blog.posts, :key => options[:group]).map { |hash|
    Blog::Archive.from_mongo(blog, hash)
  }.sort.reverse
end

.from_mongo(blog, hash) ⇒ Object



43
44
45
46
47
# File 'app/models/blog/archive.rb', line 43

def self.from_mongo(blog, hash)
  new(blog, hash["year"], hash["month"]) do |archive|
    archive.count = hash["count"].to_i
  end
end

Instance Method Details

#<=>(other) ⇒ Object



39
40
41
# File 'app/models/blog/archive.rb', line 39

def <=>(other)
  to_date <=> other.to_date
end

#format(format = "%B %Y") ⇒ Object



15
16
17
# File 'app/models/blog/archive.rb', line 15

def format(format="%B %Y")
  month ? to_date.strftime(format) : year
end

#invalid?Boolean



31
32
33
# File 'app/models/blog/archive.rb', line 31

def invalid?
  month.present? && !valid_month?
end

#pathObject



19
20
21
# File 'app/models/blog/archive.rb', line 19

def path
  @blog.archive_path(year, month)
end

#postsObject



23
24
25
26
27
28
29
# File 'app/models/blog/archive.rb', line 23

def posts
  @posts ||= begin
    posts = @blog.posts.where(:year => year)
    posts = posts.where(:month => month) if month
    posts
  end
end

#to_aryObject

Backwards-compatiblity with old-style archives



58
59
60
# File 'app/models/blog/archive.rb', line 58

def to_ary
  [to_date, path, count]
end

#to_dateObject



35
36
37
# File 'app/models/blog/archive.rb', line 35

def to_date
  Date.new(year, month || 1, 1)
end

#to_sObject



11
12
13
# File 'app/models/blog/archive.rb', line 11

def to_s
  format.to_s
end