ski_binding_calculator 0.6.8 → 0.6.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,12 @@
1
1
  require 'ski_binding_calculator/config_loader'
2
2
  require 'active_support/core_ext/hash/indifferent_access'
3
+ require 'active_support/core_ext/object/blank'
3
4
 
4
5
  class SkiBinding::Calculator
5
6
  extend SkiBinding::ConfigLoader
6
7
 
7
8
  def self.setting(attrs)
9
+ self.validate_attrs(attrs)
8
10
  attrs = self.prep_attributes(attrs)
9
11
  attrs = self.age(attrs)
10
12
  attrs = self.validate_type(attrs)
@@ -13,6 +15,29 @@ class SkiBinding::Calculator
13
15
  end
14
16
 
15
17
  private
18
+ def self.validate_attrs(attrs)
19
+ attrs = attrs.with_indifferent_access
20
+ error = SkiBinding::Error.new
21
+
22
+ if attrs[:weight].blank?
23
+ error.add_message(*[:weight, "weight is blank"])
24
+ elsif attrs[:height].blank?
25
+ error.add_message(*[:height, "height is blank"])
26
+ elsif attrs[:sole_length].blank?
27
+ error.add_message(*[:sole_length, "sole length is blank"])
28
+ elsif attrs[:birthday_year].blank?
29
+ error.add_message(*[:birthday_year, "birthday year is blank"])
30
+ elsif attrs[:birthday_month].blank?
31
+ error.add_message(*[:birthday_month, "birthday month is blank"])
32
+ elsif attrs[:birthday_day].blank?
33
+ error.add_message(*[:birthday_day, "birthday day is blank"])
34
+ elsif attrs[:type].blank?
35
+ error.add_message(*[:type, "type is blank"])
36
+ end
37
+
38
+ raise error unless error.messages.empty?
39
+ end
40
+
16
41
  def self.prep_attributes(attrs)
17
42
  attrs = attrs.with_indifferent_access
18
43
  hashy = {}
@@ -0,0 +1,14 @@
1
+ class SkiBinding::Error < StandardError
2
+
3
+ def initialize
4
+ @messages = {}
5
+ end
6
+
7
+ def messages
8
+ @messages
9
+ end
10
+
11
+ def add_message(key, value)
12
+ @messages[key] = value
13
+ end
14
+ end
@@ -3,3 +3,4 @@ require 'ski_binding_calculator/calculator'
3
3
  require 'ski_binding_calculator/config_loader'
4
4
  require 'ski_binding_calculator/code'
5
5
  require 'ski_binding_calculator/setting'
6
+ require 'ski_binding_calculator/error'
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'ski_binding_calculator'
4
- s.version = '0.6.8'
4
+ s.version = '0.6.9'
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"]
@@ -61,6 +61,23 @@ describe SkiBinding::Calculator do
61
61
  it { SkiBinding::Calculator.new.class.should == SkiBinding::Calculator }
62
62
  end
63
63
 
64
+ describe "#validate_attrs" do
65
+ subject(:calculated_attr_validation) { SkiBinding::Calculator.validate_attrs(parameters) }
66
+ let(:parameters) do
67
+ skiers_parameters[:weight] = ""
68
+ skiers_parameters[:height] = ""
69
+ skiers_parameters
70
+ end
71
+
72
+ it "raise error with two messages" do
73
+ expect { calculated_attr_validation }.to raise_error(SkiBinding::Error) do |e|
74
+ e.message.should == {:weight => "weight is blank", :height => "height is blank"}
75
+ end
76
+ end
77
+ #it { expect { calculated_attr_validation }
78
+ # .to raise_error(ArgumentError, "height is blank") }
79
+ end
80
+
64
81
  describe "#prep_attributes" do
65
82
  subject(:calculated_preped) { SkiBinding::Calculator.prep_attributes(parameters) }
66
83
  let(:parameters) { skiers_parameters }
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.6.8
4
+ version: 0.6.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-15 00:00:00.000000000 Z
12
+ date: 2013-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -118,6 +118,7 @@ files:
118
118
  - lib/ski_binding_calculator/calculator.rb
119
119
  - lib/ski_binding_calculator/code.rb
120
120
  - lib/ski_binding_calculator/config_loader.rb
121
+ - lib/ski_binding_calculator/error.rb
121
122
  - lib/ski_binding_calculator/setting.rb
122
123
  - lib/ski_binding_calculator/ski_binding.rb
123
124
  - lib/ski_binding_calculator.rb