Files
dunlop-core/app/services/day_time_base.rb
T
2018-01-20 13:02:44 -03:00

60 lines
1.5 KiB
Ruby

module DayTimeBase
extend ActiveSupport::Concern
attr_reader :number
delegate :blank?, :to_i, :to_f, to: :number
def initialize(number_or_time)
if number_or_time.present?
@number = number_or_time.is_a?(String) ? self.class.string_to_number(number_or_time) : number_or_time
else
@number = nil
end
end
def inspect
to_s
end
def coerce(value)
[value, number]
end
# ActiveRecord does a check on the existenc of the method quoted_id to quote for sql. Since this is an integer representation,
# the number is used directly.
# active_record/connection_adapters/abstract/quoting.rb|8 col 7| def quote(value, column = nil)
def quoted_id
number || 'NULL'
end
[:==, :>, :<, :!=, :>=, :<=, :equal?, :<=>].each do |compare_method|
define_method compare_method do |compare_value|
compare_operation compare_method, compare_value
end
end
def compare_operation(operation, number_or_time)
case number_or_time
when String
number.public_send(operation, self.class.string_to_number(number_or_time))
when DayTimeBase
seconds.public_send(operation, number_or_time.seconds)
else
number.public_send(operation, number_or_time)
end
end
module ClassMethods
def load(value)
new(value)
end
def dump(number_or_time)
case number_or_time
when DayTimeBase then number_or_time.number
when String then string_to_number(number_or_time)
else number_or_time
end
end
end
end