Module: TimestampAsBoolean::ClassMethods

Defined in:
lib/timestamp_as_boolean.rb

Constant Summary collapse

POSSIBLE_TRUTHY_VALUES =
[true, '1', 't', 'true'].freeze

Instance Method Summary collapse

Instance Method Details

#timestamp_as_boolean(method, attrx = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/timestamp_as_boolean.rb', line 13

def timestamp_as_boolean(method, attrx = nil)
  attrx ||= "#{method}_at"

  @@timestamped_to_bool_list ||= []
  @@timestamped_to_bool_list << method.to_sym

  define_method method do
    eval("#{attrx}.present?")
  end

  define_method "#{method}=" do |val|
    if val.present? && POSSIBLE_TRUTHY_VALUES.include?(val)
      send("#{attrx}=", Time.current) if send(attrx).nil? # Use the setter to preserve AR dirty tracking.
    else
      send("#{attrx}=", nil)
    end
  end
end

#timestamped_to_bool_listObject



32
33
34
# File 'lib/timestamp_as_boolean.rb', line 32

def timestamped_to_bool_list
  @@timestamped_to_bool_list
end