daily_affirmation 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 305ab950efd80d14e92eff86fd045a339a22aa8b
4
- data.tar.gz: 9afae48904dc91d603ce35cd7a261be311089eb7
3
+ metadata.gz: f4ecd8c16b9ba58d859435a2411832db38da7bba
4
+ data.tar.gz: ae497ebd542784fadc5f2ebc55545cfd2efd1599
5
5
  SHA512:
6
- metadata.gz: 40902db7caf7ddc098d8e2515637bcde4f1a3a47e1c2e854728c80776d85761917803dbf4023cc342d0bc59a8bc2761ce90c0d7b70b690a7eeea8f1eb449dcff
7
- data.tar.gz: 76486bf174bdaca77ba75b66e5a676b32aa7d9b24c77b839f4de11d5ade12042edd875cc1e5ea486aec5cb3a3088024bae4176f069e1b305c3e90e606ddf4289
6
+ metadata.gz: e2122846d1b00cc97b10f25a67f219306dd966813cd91610aed6af041f80b0c2faf8d2c6bf39564471bebf56caf4dc0821a5bdfd42140261d566e0b81564bce2
7
+ data.tar.gz: 7875f55e1ca8cdefee943e2619d847ebe510c4b9e5fb2fffcf1120a462a3d65bb1949a0461494a1a252456eebda743f4caa40f1d9cb832549d3061a5f7496260
@@ -4,14 +4,61 @@ module DailyAffirmation
4
4
  module Validators
5
5
  class NumericalityValidator < Validator
6
6
  def valid?
7
- @valid ||= value.is_a?(Numeric)
7
+ @valid ||= numeric? && greater_than? && less_than?
8
8
  end
9
9
 
10
10
  def error_message
11
11
  @error_message ||= i18n_error_message(
12
- :numericality, :default => "#{attribute} is not a number"
12
+ :numericality, :default => default_error_message
13
13
  )
14
14
  end
15
+
16
+ private
17
+
18
+ def numeric?
19
+ value.is_a?(Numeric)
20
+ end
21
+
22
+ def greater_than?
23
+ if gt = opts[:greater_than]
24
+ value > gt
25
+ else
26
+ true
27
+ end
28
+ end
29
+
30
+ def less_than?
31
+ if lt = opts[:less_than]
32
+ value < lt
33
+ else
34
+ true
35
+ end
36
+ end
37
+
38
+ def default_error_message
39
+ numeric_error_message + greater_than_error_message +
40
+ less_than_error_message
41
+ end
42
+
43
+ def numeric_error_message
44
+ "#{attribute} must be a number."
45
+ end
46
+
47
+ def greater_than_error_message
48
+ if greater_than?
49
+ ""
50
+ else
51
+ " Must also be greater than #{opts[:greater_than]}."
52
+ end
53
+ end
54
+
55
+ def less_than_error_message
56
+ if less_than?
57
+ ""
58
+ else
59
+ " Must also be less than #{opts[:less_than]}."
60
+ end
61
+ end
15
62
  end
16
63
  end
17
64
  end
@@ -1,3 +1,3 @@
1
1
  module DailyAffirmation
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -23,7 +23,57 @@ describe "NumericalityValidator" do
23
23
 
24
24
  validator = subject.new(obj, :age)
25
25
  expect(validator.error_message).to eq(
26
- "age is not a number"
26
+ "age must be a number."
27
27
  )
28
28
  end
29
+
30
+ context ":greater_than provided" do
31
+ it "passes validation if value is > opt" do
32
+ obj = double(:age => 10)
33
+
34
+ validator = subject.new(obj, :age, :greater_than => 5)
35
+ expect(validator).to be_valid
36
+ end
37
+
38
+ it "fails validation if value is < opt" do
39
+ obj = double(:age => 10)
40
+
41
+ validator = subject.new(obj, :age, :greater_than => 15)
42
+ expect(validator).to_not be_valid
43
+ end
44
+
45
+ it "has the correct error message" do
46
+ obj = double(:age => 10)
47
+
48
+ validator = subject.new(obj, :age, :greater_than => 15)
49
+ expect(validator.error_message).to eq(
50
+ "age must be a number. Must also be greater than 15."
51
+ )
52
+ end
53
+ end
54
+
55
+ context ":less_than provided" do
56
+ it "passes validation if value is < opt" do
57
+ obj = double(:age => 10)
58
+
59
+ validator = subject.new(obj, :age, :less_than => 15)
60
+ expect(validator).to be_valid
61
+ end
62
+
63
+ it "fails validation if value is > opt" do
64
+ obj = double(:age => 10)
65
+
66
+ validator = subject.new(obj, :age, :less_than => 5)
67
+ expect(validator).to_not be_valid
68
+ end
69
+
70
+ it "has the correct error message" do
71
+ obj = double(:age => 10)
72
+
73
+ validator = subject.new(obj, :age, :less_than => 5)
74
+ expect(validator.error_message).to eq(
75
+ "age must be a number. Must also be less than 5."
76
+ )
77
+ end
78
+ end
29
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daily_affirmation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-13 00:00:00.000000000 Z
11
+ date: 2014-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler