Module: TimeOfDayAttr

Defined in:
lib/time_of_day_attr.rb,
lib/time_of_day_attr/seconds.rb,
lib/time_of_day_attr/version.rb,
lib/time_of_day_attr/time_of_day.rb,
lib/time_of_day_attr/form_builder_ext.rb,
lib/time_of_day_attr/active_record_ext.rb

Defined Under Namespace

Modules: ActiveRecordExt, FormBuilderExt Classes: Seconds, TimeOfDay

Constant Summary collapse

DEFAULT_FORMATS =
[:default, :hour].freeze
VERSION =
'2'.freeze

Class Method Summary collapse

Class Method Details

.delocalize(value, options = {}) ⇒ Object



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

def delocalize(value, options = {})
  time_of_day = TimeOfDay.new(value)

  formats = options[:formats] || DEFAULT_FORMATS

  delocalized_values = formats.map do |format|
    begin
      return time_of_day.to_seconds(time_format(format))
    rescue ArgumentError => e
      return nil if e.message.include?('out of range')
      next
    end
  end

  delocalized_values.compact.first
end

.localize(value, options = {}) ⇒ Object Also known as: l



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/time_of_day_attr.rb', line 30

def localize(value, options = {})
  return value unless value.respond_to?(:seconds)

  format = options[:format] || DEFAULT_FORMATS.first

  time_of_day = Seconds.new(value).to_time_of_day(time_format(format))

  omit_minutes = options[:omit_minutes_at_full_hour] && time_of_day.end_with?('00')

  omit_minutes ? time_of_day[0...-3] : time_of_day
end