Sha256: e234b0265c2845e17a483b760c694245adc7c626feafc25c54ba3d4f971ebda2
Contents?: true
Size: 1.39 KB
Versions: 3
Compression:
Stored size: 1.39 KB
Contents
module Microscope class InstanceMethod class DateInstanceMethod < InstanceMethod def initialize(*args) super @now = 'Date.today' @cropped_field_regex = /_on$/ 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 alias_method 'un#{cropped_field}?', 'not_#{cropped_field}?' 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
3 entries across 3 versions & 1 rubygems