Class: TimePricing::Calculation
- Inherits:
-
Object
- Object
- TimePricing::Calculation
- Defined in:
- lib/time_pricing/calculation.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#cost ⇒ Object
readonly
Returns the value of attribute cost.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#plans_used ⇒ Object
readonly
Returns the value of attribute plans_used.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
- #breakdown ⇒ Object
- #extra_duration ⇒ Object
- #for_duration(duration) ⇒ Object
- #for_time(start_time, end_time) ⇒ Object
-
#initialize(config) ⇒ Calculation
constructor
A new instance of Calculation.
- #total_duration ⇒ Object
Constructor Details
#initialize(config) ⇒ Calculation
Returns a new instance of Calculation.
5 6 7 8 |
# File 'lib/time_pricing/calculation.rb', line 5 def initialize(config) @config = config @start_time = nil end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/time_pricing/calculation.rb', line 3 def config @config end |
#cost ⇒ Object (readonly)
Returns the value of attribute cost.
3 4 5 |
# File 'lib/time_pricing/calculation.rb', line 3 def cost @cost end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
3 4 5 |
# File 'lib/time_pricing/calculation.rb', line 3 def duration @duration end |
#end_time ⇒ Object (readonly)
Returns the value of attribute end_time.
3 4 5 |
# File 'lib/time_pricing/calculation.rb', line 3 def end_time @end_time end |
#plans_used ⇒ Object (readonly)
Returns the value of attribute plans_used.
3 4 5 |
# File 'lib/time_pricing/calculation.rb', line 3 def plans_used @plans_used end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
3 4 5 |
# File 'lib/time_pricing/calculation.rb', line 3 def start_time @start_time end |
Instance Method Details
#breakdown ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/time_pricing/calculation.rb', line 34 def breakdown result = [] current_start_time = @start_time @plans_used.each do |plan_name| plan = @config.plans[plan_name] plan_json = plan.to_json if @start_time end_time = @start_time + plan.duration plan_json.merge!({ start_time: current_start_time, end_time: end_time }) current_start_time = end_time + 1 end result << plan_json end result end |
#extra_duration ⇒ Object
70 71 72 |
# File 'lib/time_pricing/calculation.rb', line 70 def extra_duration total_duration - @duration end |
#for_duration(duration) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/time_pricing/calculation.rb', line 19 def for_duration(duration) @duration = duration if @config.combine_plans? result = best_cost_with_combos(duration) else result = best_cost_without_combos(duration) end @cost = result[:cost] @plans_used = result[:plans_used] self end |
#for_time(start_time, end_time) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/time_pricing/calculation.rb', line 10 def for_time(start_time, end_time) @start_time = start_time @end_time = end_time # find duration from start_time & end_time duration = (end_time - start_time).to_i for_duration(duration) end |
#total_duration ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/time_pricing/calculation.rb', line 59 def total_duration duration = 0 @plans_used.each do |plan_name| plan = @config.plans[plan_name] duration += plan.duration end duration end |