Sha256: b9431ae44bc9d6e15af41b10c949ae78c0e47bde86086e05b255f6370bc50fa9

Contents?: true

Size: 921 Bytes

Versions: 3

Compression:

Stored size: 921 Bytes

Contents

module ActAsTimeAsBoolean

  def self.included(base)
    base.define_singleton_method(:time_as_boolean) do |field, options={}|
      field = field.to_sym

      self.send :define_method, field do
        !send(:"#{field}_at").nil?
      end

      self.send :alias_method, :"#{field}?", :"#{field}"

      self.send :define_method, :"#{field}=" do |value|
        if (value && value != 'false' && value != '0' && !self.send(field)) || (!value && self.send(field))
          if value && value != 'false' && value != '0'
            send :"#{field}_at=", Time.now
            true
          else
            send :"#{field}_at=", nil
          end
        end
      end

      if options[:opposite]
        self.send :define_method, :"#{options[:opposite]}" do
          send(:"#{field}_at").nil?
        end

        self.send :alias_method, :"#{options[:opposite]}?", :"#{options[:opposite]}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
act_as_time_as_boolean-0.1.2 lib/act_as_time_as_boolean/base.rb
act_as_time_as_boolean-0.1.1 lib/act_as_time_as_boolean/base.rb
act_as_time_as_boolean-0.1.0 lib/act_as_time_as_boolean/base.rb