Sha256: 20fba0f65399d472a4b3a2b98a232898e7fed1ef9392dbc2277c05c4025cbf73

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 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
        RUBY
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
microscope-0.5.6.1 lib/microscope/instance_method/datetime_instance_method.rb
microscope-0.5.6 lib/microscope/instance_method/datetime_instance_method.rb