Class: AS::Duration::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/as/duration.rb

Instance Method Summary collapse

Constructor Details

#initialize(parts) ⇒ Calculator

Returns a new instance of Calculator.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/as/duration.rb', line 70

def initialize(parts)
  options = parts.inject({}) do |options, (type, number)|
    options.update(type => number) { |key, old, new| old + new }
  end

  # Remove partial weeks and days for accurate date behaviour
  if Float === options[:weeks]
    options[:weeks], partial_weeks = options[:weeks].divmod(1)
    options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
  end
  if Float === options[:days]
    options[:days], partial_days = options[:days].divmod(1)
    options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
  end

  @options = options
end

Instance Method Details

#advance(time) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/as/duration.rb', line 88

def advance(time)
  case time
  when Time then advance_time(time)
  when Date then advance_date(time)
  else
    raise ArgumentError, "expected Time or Date, got #{time.inspect}"
  end
end