class Contraption::Catalog

Attributes

items[R]

Public Class Methods

new(items) click to toggle source
# File lib/contraption/catalog.rb, line 9
def initialize items
  @items = Array items
end

Public Instance Methods

<<(new_item) click to toggle source
# File lib/contraption/catalog.rb, line 13
def << new_item
  @items.concat(Array new_item)
end
by_day() click to toggle source
# File lib/contraption/catalog.rb, line 25
def by_day
  by_date { |date| Day.new date.year, date.month, date.day }
end
by_month() click to toggle source
# File lib/contraption/catalog.rb, line 21
def by_month
  by_date { |date| Month.new date.year, date.month }
end
by_tag() click to toggle source
# File lib/contraption/catalog.rb, line 29
def by_tag
  tags = all(:tags).uniq{|t| t.to_url}
  items.each_with_object(Hash.new {|h,k| h[k] = []}) do |item, result|
    item.tags.each{|t| result[t.to_url] << item}
  end
end
by_year() click to toggle source
# File lib/contraption/catalog.rb, line 17
def by_year
  by_date { |date| Year.new date.year }
end
each() click to toggle source
# File lib/contraption/catalog.rb, line 36
def each
  items.each
end
most_recent(n=1) click to toggle source
# File lib/contraption/catalog.rb, line 40
def most_recent n=1
  n = items.length if n < 0
  items.sort_by{ |item| item.publication_date }
       .reverse
       .take n
end

Private Instance Methods

all(attribute) click to toggle source
# File lib/contraption/catalog.rb, line 58
def all attribute
  items.map{|item| item.public_send(attribute.to_sym)}
       .flatten
       .uniq
end
by() { |item| ... } click to toggle source
# File lib/contraption/catalog.rb, line 48
def by &block
  items.group_by do |item|
    yield item
  end
end
by_date() { |call| ... } click to toggle source
# File lib/contraption/catalog.rb, line 54
def by_date &block
  by { |i| yield block.call(i.publication_date) }
end