Class: Blog::Archive
- Inherits:
-
Object
- Object
- Blog::Archive
- Defined in:
- app/models/blog/archive.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #format(format = "%B %Y") ⇒ Object
-
#initialize(blog, year, month = nil) {|_self| ... } ⇒ Archive
constructor
A new instance of Archive.
- #invalid? ⇒ Boolean
- #path ⇒ Object
- #posts ⇒ Object
-
#to_ary ⇒ Object
Backwards-compatiblity with old-style archives.
- #to_date ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(blog, year, month = nil) {|_self| ... } ⇒ Archive
Returns a new instance of Archive.
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
#count ⇒ Object
Returns the value of attribute count.
3 4 5 |
# File 'app/models/blog/archive.rb', line 3 def count @count end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
2 3 4 |
# File 'app/models/blog/archive.rb', line 2 def month @month end |
#year ⇒ Object (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, ) [:group] ||= [:year, :month] group(blog.posts, :key => [: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 |
#path ⇒ Object
19 20 21 |
# File 'app/models/blog/archive.rb', line 19 def path @blog.archive_path(year, month) end |
#posts ⇒ Object
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_ary ⇒ Object
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_date ⇒ Object
35 36 37 |
# File 'app/models/blog/archive.rb', line 35 def to_date Date.new(year, month || 1, 1) end |
#to_s ⇒ Object
11 12 13 |
# File 'app/models/blog/archive.rb', line 11 def to_s format.to_s end |