ski_binding_calculator 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,27 +18,11 @@ class SkiBinding::Calculator
18
18
  def self.validate_attrs(attrs)
19
19
  attrs = attrs.with_indifferent_access
20
20
  error = SkiBinding::Error.new
21
-
22
- if attrs[:weight].blank?
23
- error.add_message(*[:weight, "weight is blank"])
24
- end
25
- if attrs[:height].blank?
26
- error.add_message(*[:height, "height is blank"])
27
- end
28
- if attrs[:sole_length].blank?
29
- error.add_message(*[:sole_length, "sole length is blank"])
30
- end
31
- if attrs[:birthday_year].blank?
32
- error.add_message(*[:birthday_year, "birthday year is blank"])
33
- end
34
- if attrs[:birthday_month].blank?
35
- error.add_message(*[:birthday_month, "birthday month is blank"])
36
- end
37
- if attrs[:birthday_day].blank?
38
- error.add_message(*[:birthday_day, "birthday day is blank"])
39
- end
40
- if attrs[:type].blank?
41
- error.add_message(*[:type, "type is blank"])
21
+ attributes = [:weight, :height, :birthday_year, :birthday_month, :birthday_day,
22
+ :sole_length, :type]
23
+
24
+ attributes.each do |attr|
25
+ error.add_message(*[attr, "is blank"]) if attrs[attr].blank?
42
26
  end
43
27
 
44
28
  raise error unless error.messages.empty?
@@ -49,7 +33,7 @@ class SkiBinding::Calculator
49
33
  hashy = {}
50
34
  hashy[:weight] = attrs[:weight].to_f
51
35
  if hashy[:weight] < 10.0
52
- raise ArgumentError, "Weight must be at least 10kg"
36
+ raise SkiBinding::Error.new(*[:weight, "is less than 10kg"])
53
37
  end
54
38
  hashy[:height] = attrs[:height].to_f
55
39
  hashy[:sole_length] = attrs[:sole_length].to_f
@@ -81,7 +65,7 @@ class SkiBinding::Calculator
81
65
  if keys.include?(type_string)
82
66
  attrs[:type] = types[type_string]
83
67
  else
84
- raise ArgumentError, "You have entered an invalid type."
68
+ raise SkiBinding::Error.new(*[:type, "You have entered an invalid type."])
85
69
  end
86
70
  attrs
87
71
  end
@@ -102,7 +86,7 @@ class SkiBinding::Calculator
102
86
  end
103
87
  end
104
88
  if code == -1
105
- raise ArgumentError, "You have entered invalid weight and/or height"
89
+ raise SkiBinding::Error.new(*[:base, "You have entered invalid weight and/or height"])
106
90
  end
107
91
  unless attrs[:weight] < 13
108
92
  code += attrs[:type]
@@ -120,7 +104,7 @@ class SkiBinding::Calculator
120
104
  unless s.z_value.nil?
121
105
  return {:z_value => s.z_value}
122
106
  else
123
- raise ArgumentError, "Please calculate z-index by hand."
107
+ raise SkiBinding::Error.new(*[:base, "Please calculate z-index by hand."])
124
108
  end
125
109
  end
126
110
  end
@@ -1,7 +1,11 @@
1
1
  class SkiBinding::Error < StandardError
2
2
 
3
- def initialize
3
+ def initialize(key = nil, value = nil)
4
4
  @messages = {}
5
+
6
+ if !key.nil? && !value.nil?
7
+ @messages[key] = value
8
+ end
5
9
  end
6
10
 
7
11
  def messages
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'ski_binding_calculator'
4
- s.version = '0.7.0'
4
+ s.version = '0.7.1'
5
5
  s.summary = "Ski Binding Calculator"
6
6
  s.description = "Calculates the z-value according to ISO 11088."
7
7
  s.authors = ["Jonas Ruef, Felix Langenegger"]
@@ -71,7 +71,7 @@ describe SkiBinding::Calculator do
71
71
 
72
72
  it "raise error with two messages" do
73
73
  expect { calculated_attr_validation }.to raise_error(SkiBinding::Error) { |e|
74
- e.messages.should == {:weight => "weight is blank", :height => "height is blank"} }
74
+ e.messages.should == { :weight => "is blank", :height => "is blank" } }
75
75
  end
76
76
  end
77
77
 
@@ -91,8 +91,10 @@ describe SkiBinding::Calculator do
91
91
  skiers_parameters[:weight] = 9
92
92
  skiers_parameters
93
93
  end
94
- it { expect { calculated_preped }
95
- .to raise_error(ArgumentError, "Weight must be at least 10kg") }
94
+ it "raise error" do
95
+ expect { calculated_preped }.to raise_error(SkiBinding::Error) { |e|
96
+ e.messages.should == { :weight => "is less than 10kg" } }
97
+ end
96
98
  end
97
99
  end
98
100
 
@@ -117,8 +119,10 @@ describe SkiBinding::Calculator do
117
119
  expected_aged
118
120
  end
119
121
 
120
- it{ expect { calculated_validated }
121
- .to raise_error(ArgumentError, "You have entered an invalid type.") }
122
+ it "raise error" do
123
+ expect { calculated_validated }.to raise_error(SkiBinding::Error) { |e|
124
+ e.messages.should == { :type => "You have entered an invalid type." } }
125
+ end
122
126
  end
123
127
  end
124
128
 
@@ -144,8 +148,10 @@ describe SkiBinding::Calculator do
144
148
  expected_validated
145
149
  end
146
150
 
147
- it { expect { calculated_code }.
148
- to raise_error(ArgumentError, "You have entered invalid weight and/or height") }
151
+ it "raises error" do
152
+ expect { calculated_code }.to raise_error(SkiBinding::Error) { |e|
153
+ e.messages.should == { :base => "You have entered invalid weight and/or height" } }
154
+ end
149
155
  end
150
156
 
151
157
  context "when age >= 50 || age < 10" do
@@ -187,8 +193,10 @@ describe SkiBinding::Calculator do
187
193
 
188
194
  context "when no setting found" do
189
195
  let(:binding_code) { 0 }
190
- it{ expect { calculated_setting }
191
- .to raise_error(ArgumentError, "Please calculate z-index by hand.") }
196
+ it "raise error" do
197
+ expect { calculated_setting }.to raise_error(SkiBinding::Error) { |e|
198
+ e.messages.should == { :base => "Please calculate z-index by hand." } }
199
+ end
192
200
  end
193
201
  end
194
202
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ski_binding_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: