Module: TimeForABoolean

Defined in:
lib/time_for_a_boolean.rb,
lib/time_for_a_boolean/railtie.rb,
lib/time_for_a_boolean/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
'0.2.2'

Instance Method Summary collapse

Instance Method Details

#time_for_a_boolean(attribute, field = :"#{attribute}_at") ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/time_for_a_boolean.rb', line 9

def time_for_a_boolean(attribute, field=:"#{attribute}_at")
  define_method(attribute) do
    !send(field).nil? && send(field) <= -> { Time.current }.()
  end

  alias_method :"#{attribute}?", attribute

  setter_attribute = :"#{field}="
  define_method(:"#{attribute}=") do |value|
    if ActiveModel::Type::Boolean::FALSE_VALUES.include?(value)
      send(setter_attribute, nil)
    else
      send(setter_attribute, -> { Time.current }.())
    end
  end

  define_method(:"#{attribute}!") do
    send(:"#{attribute}=", true)
  end
end