Sha256: 72f185cf45b5ef2a7d1ef7a5da88a7720b3623d45824f7e3cbefb877b47c4acc

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Microscope
  class Scope
    class DatetimeScope < Scope
      def initialize(*args)
        super

        @now = 'Time.now'
        @now_suffix = '_now'
        @cropped_field_regex = /_at$/
      end

      def apply
        @cropped_field = field.name.gsub(@cropped_field_regex, '')

        model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          #{before_scopes}
          #{after_scopes}
          #{between_scopes}
          #{boolean_scopes}
        RUBY
      end

    private

      def before_scopes
        <<-RUBY
          scope "#{@cropped_field}_before", lambda { |time| where("#{field.name} < ?", time) }
          scope "#{@cropped_field}_before_or_at", lambda { |time| where("#{field.name} <= ?", time) }
          scope "#{@cropped_field}_before#{@now_suffix}", lambda { where("#{field.name} < ?", #{@now}) }
        RUBY
      end

      def after_scopes
        <<-RUBY
          scope "#{@cropped_field}_after", lambda { |time| where("#{field.name} > ?", time) }
          scope "#{@cropped_field}_after_or_at", lambda { |time| where("#{field.name} >= ?", time) }
          scope "#{@cropped_field}_after#{@now_suffix}", lambda { where("#{field.name} > ?", #{@now}) }
        RUBY
      end

      def between_scopes
        <<-RUBY
          scope "#{@cropped_field}_between", lambda { |range| where("#{field.name}" => range) }
        RUBY
      end

      def boolean_scopes
        <<-RUBY
          scope "#{@cropped_field}", lambda { where("#{field.name} IS NOT NULL AND #{field.name} <= ?", #{@now}) }
          scope "not_#{@cropped_field}", lambda { where("#{field.name} IS NULL OR #{field.name} > ?", #{@now}) }
        RUBY
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
microscope-0.4 lib/microscope/scope/datetime_scope.rb