zillion 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7c07c7fc1c9a9d1db83c48d8bd87d27fa6b0fed
4
- data.tar.gz: e79f1bbd30dd52bf2f166200dfdefa398a30cb5b
3
+ metadata.gz: a03fdebc806dd28abd1d3813483727407848d7f1
4
+ data.tar.gz: 1e1a6a57c2e52d97f5f4b23978b98bb5ea404476
5
5
  SHA512:
6
- metadata.gz: 9ddf4c476a574cf6e5d74850b3174ce60b03bb225b89a1ce7e31953de3c3176c566ecc2f2891c0ced74dd34c323f79940b88c5d75fa2cdfe84c8df06355c46b1
7
- data.tar.gz: 62d318109781177ceb663f6e6ebb664e9a19d14982815b8c43a23aca52791ea1931661588738508990b8dcd7b8e29eef4e8e955c84fb8ee39c29abadb1996257
6
+ metadata.gz: d46e751cb62241643c8602ed271dff1d071d91d65cd83ce79c7cde2f292d852eb05e751c2a29ca3f1c340f4530f99f00c8e67c2fd28fcd27ba1a96f1534704f8
7
+ data.tar.gz: 530442ee7467e56ca985051a0a21f1c6cb76d26f8e65e40f0bd7ebf4e38c87aacb2a5d609d0a55707d0b640a4f132bc37176278b04d290b3b52b8a05a4bab1ee
@@ -39,12 +39,25 @@ class PlanFinder
39
39
  self.class.available_plans_for(@counts)
40
40
  end
41
41
 
42
- def sorted_available_plans
43
- sorted_keys = available_plans.keys.sort do |a,b|
44
- mapper.get(a).monthly_fee_for(@counts) <=> mapper.get(b).monthly_fee_for(@counts)
45
- end
42
+ def previous_plan_of(plan)
43
+ list = sorted_plans
44
+ num = list.index(mapper.get(plan))
45
+ return if num.to_i <= 0
46
+ list[num-1]
47
+ end
46
48
 
47
- sorted_keys.map { |key| mapper.get(key) }
49
+ def next_plan_of(plan)
50
+ list = sorted_plans
51
+ num = list.index(mapper.get(plan)) or return
52
+ list[num+1]
53
+ end
54
+
55
+ def sorted_plans
56
+ sort_plans(mapper.all)
57
+ end
58
+
59
+ def sorted_available_plans
60
+ sort_plans(available_plans)
48
61
  end
49
62
 
50
63
  def best_plan
@@ -58,14 +71,27 @@ class PlanFinder
58
71
 
59
72
  private
60
73
 
74
+ def sort_plans(list)
75
+ sorted_keys = list.keys.sort do |a, b|
76
+ fee_for(a) <=> fee_for(b)
77
+ end
78
+
79
+ sorted_keys.map { |key| mapper.get(key) }
80
+ end
81
+
61
82
  def mapper
62
83
  self.class.mapper
63
84
  end
64
85
 
65
86
  def compare(one, two, direction)
66
- one = PlanMapper.get(one) unless one.is_a?(Plan)
67
- two = PlanMapper.get(two) unless two.is_a?(Plan)
68
- one.monthly_fee_for(@counts).send(direction, two.monthly_fee_for(@counts))
87
+ fee_for(one).send(direction, fee_for(two))
88
+ end
89
+
90
+ # returns fee for counts, if within limits, otherwise zero
91
+ def fee_for(plan_key)
92
+ mapper.get(plan_key).monthly_fee_for(@counts)
93
+ rescue Plan::OverLimitsError
94
+ 0
69
95
  end
70
96
 
71
97
  end
@@ -101,6 +127,7 @@ class PlanMapper
101
127
 
102
128
  def get(key)
103
129
  raise "Please load first. No need to be an ass." if plans.empty?
130
+ return key if key.is_a?(Plan)
104
131
  plans[key.to_sym]
105
132
  end
106
133
 
data/spec/mapper_spec.rb CHANGED
@@ -23,8 +23,9 @@ describe 'PlanFinder' do
23
23
 
24
24
  let (:mapper) {
25
25
  PlanMapper.new({
26
- small: { monthly_fee: 0, limits: { products: 100 } },
27
- large: { monthly_fee: 10, limits: { products: 200 } }
26
+ small: { monthly_fee: 0, limits: { products: 100 } },
27
+ medium: { monthly_fee: 5, limits: { products: 150 } },
28
+ large: { monthly_fee: 10, limits: { products: 200 } }
28
29
  })
29
30
  }
30
31
 
@@ -32,6 +33,15 @@ describe 'PlanFinder' do
32
33
  expect { PlanFinder.new({}) }.to raise_error # MissingCountError
33
34
  end
34
35
 
36
+ describe '#sorted_plans' do
37
+
38
+ it 'returns all plans, including unavailable ones' do
39
+ finder = PlanFinder.init(mapper).new({ products: 120 })
40
+ expect(finder.sorted_plans.map(&:name)).to eql([:small, :medium, :large])
41
+ end
42
+
43
+ end
44
+
35
45
  describe '#available_plans' do
36
46
 
37
47
  it 'explodes if no limits passed' do
@@ -51,7 +61,7 @@ describe 'PlanFinder' do
51
61
 
52
62
  it 'returns array with higher plan if over within both of them' do
53
63
  finder = PlanFinder.init(mapper).new({ products: 150 })
54
- expect(finder.available_plans).to eql({ large: mapper.get(:large) })
64
+ expect(finder.available_plans).to eql({ medium: mapper.get(:medium), large: mapper.get(:large) })
55
65
  end
56
66
 
57
67
  it 'returns array with both plans if under the lowest limit' do
@@ -61,6 +71,44 @@ describe 'PlanFinder' do
61
71
 
62
72
  end
63
73
 
74
+ describe '#previous_plan_of(plan)' do
75
+
76
+ it 'returs nil if lowest plan' do
77
+ finder = PlanFinder.init(mapper).new({ products: 50 })
78
+ expect(finder.previous_plan_of(mapper.get(:small))).to be_nil
79
+ end
80
+
81
+ it 'returs small if medium plan' do
82
+ finder = PlanFinder.init(mapper).new({ products: 50 })
83
+ expect(finder.previous_plan_of(:medium)).to be(mapper.get(:small))
84
+ end
85
+
86
+ it 'returs medium if large plan' do
87
+ finder = PlanFinder.init(mapper).new({ products: 50 })
88
+ expect(finder.previous_plan_of(:large)).to be(mapper.get(:medium))
89
+ end
90
+
91
+ end
92
+
93
+ describe '#next_plan_of(plan)' do
94
+
95
+ it 'returs nil if highest plan' do
96
+ finder = PlanFinder.init(mapper).new({ products: 50 })
97
+ expect(finder.next_plan_of(mapper.get(:large))).to be_nil
98
+ end
99
+
100
+ it 'returs large if medium plan' do
101
+ finder = PlanFinder.init(mapper).new({ products: 50 })
102
+ expect(finder.next_plan_of(:medium)).to be(mapper.get(:large))
103
+ end
104
+
105
+ it 'returs medium if small plan' do
106
+ finder = PlanFinder.init(mapper).new({ products: 50 })
107
+ expect(finder.next_plan_of(:small)).to be(mapper.get(:medium))
108
+ end
109
+
110
+ end
111
+
64
112
  describe '#best_plan' do
65
113
 
66
114
  it 'returns nil if over all limits' do
data/zillion.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "zillion"
6
- s.version = '0.0.3'
6
+ s.version = '0.0.4'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ['Tomás Pollak']
9
9
  s.email = ['tomas@forkhq.com']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zillion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak