Sha256: 685cec0f95b6eca81fbf5b02577c6bbab72bd42bccb037cf040e38eb7880cbd1

Contents?: true

Size: 806 Bytes

Versions: 1

Compression:

Stored size: 806 Bytes

Contents

require "active_support/core_ext/module/delegation"
require "active_support/core_ext/time/calculations"
require "active_model/type"
require "time_for_a_boolean/version"
require "time_for_a_boolean/railtie"

module TimeForABoolean
  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
time_for_a_boolean-0.2.1 lib/time_for_a_boolean.rb