Module: Duranged::Formatters

Included in:
Base
Defined in:
lib/duranged/formatters.rb

Constant Summary collapse

PERIODS =
[:years, :months, :weeks, :days_after_weeks, :days, :hours, :minutes, :seconds]
FORMATTERS =
{ 's' => -> (pad,with) { pad_value seconds, pad, with },
'm' => -> (pad,with) { pad_value minutes, pad, with },
'h' => -> (pad,with) { pad_value hours, pad, with },
'd' => -> (pad,with) { pad_value days, pad, with },
'D' => -> (pad,with) { pad_value days_after_weeks, pad, with },
'w' => -> (pad,with) { pad_value weeks, pad, with },
'M' => -> (pad,with) { pad_value months, pad, with },
'y' => -> (pad,with) { pad_value years, pad, with } }

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object



13
14
15
# File 'lib/duranged/formatters.rb', line 13

def as_json(options=nil)
  value
end

#strfdur(format) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/duranged/formatters.rb', line 27

def strfdur(format)
  str = format.to_s

  # :years(%Y years, ):months(%N months, )%D :days
  str = strfparts(str)

  # %y %M %w %d %m %s
  str = strfformatters(str)

  str
end

#to_hObject



17
18
19
20
21
# File 'lib/duranged/formatters.rb', line 17

def to_h
  Hash[PERIODS.map do |part|
    [part, send(part)]
  end]
end

#to_iObject



39
40
41
# File 'lib/duranged/formatters.rb', line 39

def to_i
  value
end

#to_sObject



23
24
25
# File 'lib/duranged/formatters.rb', line 23

def to_s
  ChronicDuration.output(value, format: :long, joiner: ', ').to_s
end