Class: TimePricing::Calculation

Inherits:
Object
  • Object
show all
Defined in:
lib/time_pricing/calculation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/time_pricing/calculation.rb', line 3

def config
  @config
end

#costObject (readonly)

Returns the value of attribute cost.



3
4
5
# File 'lib/time_pricing/calculation.rb', line 3

def cost
  @cost
end

#durationObject (readonly)

Returns the value of attribute duration.



3
4
5
# File 'lib/time_pricing/calculation.rb', line 3

def duration
  @duration
end

#end_timeObject (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_usedObject (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_timeObject (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

#breakdownObject



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_durationObject



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_durationObject



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