Class: Zillion::PlanFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/zillion/mapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counts) ⇒ PlanFinder

Returns a new instance of PlanFinder.



24
25
26
27
# File 'lib/zillion/mapper.rb', line 24

def initialize(counts)
  raise "Mapper not set!" if self.class.mapper.nil?
  @counts = counts
end

Class Method Details

.available_plans_for(counts) ⇒ Object



16
17
18
# File 'lib/zillion/mapper.rb', line 16

def self.available_plans_for(counts)
  mapper.all.select { |key, plan| plan.available_for?(counts) }
end

.init(mapper) ⇒ Object



7
8
9
10
# File 'lib/zillion/mapper.rb', line 7

def self.init(mapper)
  @mapper = mapper
  self
end

.mapperObject



12
13
14
# File 'lib/zillion/mapper.rb', line 12

def self.mapper
  @mapper
end

.unavailable_plans_for(counts) ⇒ Object



20
21
22
# File 'lib/zillion/mapper.rb', line 20

def self.unavailable_plans_for(counts)
  mapper.all.select { |key, plan| !plan.available_for?(counts) }
end

Instance Method Details

#available_plansObject



37
38
39
40
# File 'lib/zillion/mapper.rb', line 37

def available_plans
  # @available_plans ||=
  self.class.available_plans_for(@counts)
end

#best_planObject



63
64
65
66
# File 'lib/zillion/mapper.rb', line 63

def best_plan
  return nil if available_plans.empty?
  sorted_available_plans.first
end

#best_plan_excluding(*plan_list) ⇒ Object



68
69
70
71
72
# File 'lib/zillion/mapper.rb', line 68

def best_plan_excluding(*plan_list)
  return nil if available_plans.empty?
  list = sorted_available_plans.select { |plan| !plan_list.include?(plan.key.to_s) }
  list.first
end

#is_downgrade?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/zillion/mapper.rb', line 33

def is_downgrade?(from, to)
  compare(from, to, '>')
end

#is_upgrade?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/zillion/mapper.rb', line 29

def is_upgrade?(from, to)
  compare(from, to, '<')
end

#monthly_fee_for_best_planObject



74
75
76
# File 'lib/zillion/mapper.rb', line 74

def monthly_fee_for_best_plan
  best_plan.monthly_fee_for(@counts)
end

#next_plan_of(plan) ⇒ Object



49
50
51
52
53
# File 'lib/zillion/mapper.rb', line 49

def next_plan_of(plan)
  list = sorted_plans
  num  = list.index(mapper.get(plan)) or return
  list[num+1]
end

#previous_plan_of(plan) ⇒ Object



42
43
44
45
46
47
# File 'lib/zillion/mapper.rb', line 42

def previous_plan_of(plan)
  list = sorted_plans
  num  = list.index(mapper.get(plan))
  return if num.to_i <= 0
  list[num-1]
end

#sorted_available_plansObject



59
60
61
# File 'lib/zillion/mapper.rb', line 59

def sorted_available_plans
  sort_plans(available_plans)
end

#sorted_plansObject



55
56
57
# File 'lib/zillion/mapper.rb', line 55

def sorted_plans
  sort_plans(mapper.all)
end