Sha256: b725a0b4a2f61930f2bba585b60f08478e381c72d5c6d8d7346af0503c4e3bee

Contents?: true

Size: 968 Bytes

Versions: 1

Compression:

Stored size: 968 Bytes

Contents

module Microscope
  class InstanceMethod
    class DateInstanceMethod < InstanceMethod
      def apply
        cropped_field = field.name.gsub(/_on$/, '')
        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 <= Date.today
          end

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

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

          define_method "#{infinitive_verb}!" do
            send("#{field.name}=", Date.today)
            save!
          end
        RUBY
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
microscope-0.5.5 lib/microscope/instance_method/date_instance_method.rb