Sha256: 9607ea994e8d846cdb14344d39a5d01d3aa744261355d0c5ff42f30fd53710c7
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
module Microscope class Scope class DatetimeScope < Scope def initialize(*args) super @now = 'Time.now' @now_suffix = '_now' @specific_suffix = '_at' @cropped_field_regex = /_at$/ @formatted_time = 'time' end def apply model.class_eval(apply_scopes) if @field.name =~ @cropped_field_regex end private def apply_scopes <<-RUBY scope "#{cropped_field}_before", lambda { |time| where('#{quoted_field} < ?', #{@formatted_time}) } scope "#{cropped_field}_before_or#{@specific_suffix}", lambda { |time| where('#{quoted_field} <= ?', #{@formatted_time}) } scope "#{cropped_field}_before#{@now_suffix}", lambda { where('#{quoted_field} < ?', #{@now}) } scope "#{cropped_field}_before_or#{@now_suffix}", lambda { where('#{quoted_field} <= ?', #{@now}) } scope "#{cropped_field}_after", lambda { |time| where('#{quoted_field} > ?', #{@formatted_time}) } scope "#{cropped_field}_after_or#{@specific_suffix}", lambda { |time| where('#{quoted_field} >= ?', #{@formatted_time}) } scope "#{cropped_field}_after#{@now_suffix}", lambda { where('#{quoted_field} > ?', #{@now}) } scope "#{cropped_field}_after_or#{@now_suffix}", lambda { where('#{quoted_field} >= ?', #{@now}) } scope "#{cropped_field}_between", lambda { |range| where("#{@field.name}" => range) } scope "#{cropped_field}", lambda { where('#{quoted_field} IS NOT NULL AND #{quoted_field} <= ?', #{@now}) } scope "not_#{cropped_field}", lambda { where('#{quoted_field} IS NULL OR #{quoted_field} > ?', #{@now}) } scope "un#{cropped_field}", lambda { not_#{cropped_field} } RUBY end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
microscope-1.0.1 | lib/microscope/scope/datetime_scope.rb |