Module: TruthyDate

Defined in:
lib/truthy_date.rb

Instance Method Summary collapse

Instance Method Details

#truthy_date(attribute, options) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/truthy_date.rb', line 2

def truthy_date(attribute, options)
  raise ArgumentError, "missing :as for truthy_date" unless options[:as]

  define_method("#{options[:as]}=") do |value|
    self.send("#{attribute}=", ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value) ? Time.now : nil)
  end

  define_method("#{options[:as]}?") do
    self.send(attribute).present?
  end

  define_method("#{options[:as]}") do
    self.send("#{options[:as]}?")
  end
end