Sha256: 0da623ee43839e1aa063c69006c09632fd4edc613b4c1924168b2d8fd8efe831

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

module Microscope
  class InstanceMethod
    class DatetimeInstanceMethod < InstanceMethod
      def initialize(*args)
        super

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

      def apply
        return unless @field_name =~ @cropped_field_regex

        cropped_field = field.name.gsub(@cropped_field_regex, '')
        infinitive_verb = self.class.past_participle_to_infinitive(cropped_field)

        model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          define_method "#{cropped_field}?" do
            value = send("#{field.name}")
            !value.nil? && value <= #{@now}
          end

          define_method "#{cropped_field}=" do |value|
            if Microscope::InstanceMethod.value_to_boolean(value)
              self.#{field.name} ||= #{@now}
            else
              self.#{field.name} = nil
            end
          end

          define_method "not_#{cropped_field}?" do
            !#{cropped_field}?
          end

          define_method "#{infinitive_verb}!" do
            send("#{field.name}=", #{@now})
            save!
          end

          define_method "not_#{infinitive_verb}!" do
            send("#{field.name}=", nil)
            save!
          end
          alias_method 'un#{infinitive_verb}!', 'not_#{infinitive_verb}!'
        RUBY
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
microscope-0.5.8 lib/microscope/instance_method/datetime_instance_method.rb
microscope-0.5.7 lib/microscope/instance_method/datetime_instance_method.rb