Sha256: 01b1c0a197e8cca2e1c9fbcbd9f8da1349f66b7e228cf99c39e9b7b68630b6ce

Contents?: true

Size: 1.42 KB

Versions: 24

Compression:

Stored size: 1.42 KB

Contents

class ArchiveFinder
  
  def initialize(&block)
    @block = block
  end
  
  def find(method, options = {})
    @block.call(method, options)
  end
  
  class << self
    def year_finder(finder, year)
      new do |method, options|
        start = Time.local(year)
        finish = start.next_year
        add_condition(options, "published_at >= ? and published_at < ?", start, finish)
        finder.find(method, options)
      end
    end
    
    def month_finder(finder, year, month)
      new do |method, options|
        start = Time.local(year, month)
        finish = start.next_month
        add_condition(options, "published_at >= ? and published_at < ?", start, finish)
        finder.find(method, options)
      end
    end
    
    def day_finder(finder, year, month, day)
      new do |method, options|
        start = Time.local(year, month, day)
        finish = start.tomorrow
        add_condition(options, "published_at >= ? and published_at < ?", start, finish)
        finder.find(method, options)
      end
    end
    
    private
      
      def concat_conditions(a, b)
        sql = "(#{ [a.shift, b.shift].compact.join(") AND (") })"
        params = a + b
        [sql, *params]
      end
      
      def add_condition(options, *condition)
        old = options[:conditions] || []
        conditions = concat_conditions(old, condition)
        options[:conditions] = conditions
        options
      end
      
  end
end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
radiantcms-couchrest_model-0.2.4 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.2.2 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.2.1 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.2 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.1.9 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.1.8 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.1.7 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.1.6 vendor/extensions/archive/app/models/archive_finder.rb
radiantcms-couchrest_model-0.1.5 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.7.2 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.8.2 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.9.0.rc2 vendor/extensions/archive/app/models/archive_finder.rb
radiant-rc-0.9.0 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.8.1 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.6.4 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.6.5.1 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.6.5 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.6.6 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.6.7 vendor/extensions/archive/app/models/archive_finder.rb
radiant-0.6.9 vendor/extensions/archive/app/models/archive_finder.rb