daily_affirmation 0.3.0 → 0.3.1

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: 8bebab94b6507fded2c3c6b8620064c31753fbd7
4
- data.tar.gz: 7ec0f8b962d6981a99f62d3475524437ecdec690
3
+ metadata.gz: 94605e7d69f4f97c4ec51b7ae89f813f74a75e8f
4
+ data.tar.gz: 3754de93153d51826cc98410c199cc6cac4730db
5
5
  SHA512:
6
- metadata.gz: b73c9f489458982f747535fa715dd90f6a10d8f7cf7814b9f00c247b99aa521f4b53a1a70d56794b38961aabeef41f4722ac98931afd535bc160a21460aa6dbf
7
- data.tar.gz: 3cd11055f9b7fc591796320fb951324d522481835e69e8e588bf6360a425f9e642945d8929ce86189d1f13b1c984ba0d8df4022ca8013793aa83a4627ec17e99
6
+ metadata.gz: 6e7006de8350a3b28611692d7b4a1e2dbe0430c90175e6f6e02f9f70f393a0aa4203433f5b7d9c8564233e5d47a530166f308da1f48b2854cbf8fcedf6766c59
7
+ data.tar.gz: b4d8275d96b85de094dd78fa89f4c9aef10e6d2aed4db502f2a56fd933e430c5663c55a5d89ad157e7b025d2e89c50c83307ec15ba463f4a73fe7586c80e9397
@@ -37,55 +37,75 @@ module DailyAffirmation
37
37
  attribute = affirmation[:attribute]
38
38
  args = affirmation.reject { |k, _| [:type, :attribute].include?(k) }
39
39
 
40
- validator = Object.const_get(
41
- "DailyAffirmation::Validators::#{type.to_s.capitalize}Validator"
42
- )
43
- validator.new(object, attribute, args).affirm
40
+ process_validation = if args.include?(:if)
41
+ instance_eval(&args[:if])
42
+ else
43
+ true
44
+ end
45
+
46
+ if process_validation
47
+ validator = Object.const_get(
48
+ "DailyAffirmation::Validators::#{type.to_s.capitalize}Validator"
49
+ )
50
+ validator.new(object, attribute, args).affirm
51
+ else
52
+ [true, nil]
53
+ end
44
54
  end
45
55
 
46
56
  module ClassMethods
47
- def affirms_absence_of(attribute)
48
- affirmations << {:attribute => attribute, :type => :absence}
57
+ def affirms_absence_of(attribute, opts = {})
58
+ affirmations << {
59
+ :attribute => attribute, :type => :absence
60
+ }.merge(opts)
49
61
  end
50
62
 
51
- def affirms_acceptance_of(attribute)
52
- affirmations << {:attribute => attribute, :type => :acceptance}
63
+ def affirms_acceptance_of(attribute, opts = {})
64
+ affirmations << {
65
+ :attribute => attribute, :type => :acceptance
66
+ }.merge(opts)
53
67
  end
54
68
 
55
- def affirms_confirmation_of(attribute)
56
- affirmations << {:attribute => attribute, :type => :confirmation}
69
+ def affirms_confirmation_of(attribute, opts = {})
70
+ affirmations << {
71
+ :attribute => attribute, :type => :confirmation
72
+ }.merge(opts)
57
73
  end
58
74
 
59
- def affirms_exclusion_of(attribute, list: [])
75
+ def affirms_exclusion_of(attribute, opts = {}, list: [])
60
76
  affirmations << {
61
77
  :attribute => attribute, :type => :exclusion, :list => list
62
- }
78
+ }.merge(opts)
63
79
  end
64
80
 
65
- def affirms_format_of(attribute, regex: //)
81
+ def affirms_format_of(attribute, opts = {}, regex: //)
66
82
  affirmations << {
67
83
  :attribute => attribute, :type => :format, :regex => regex
68
- }
84
+ }.merge(opts)
69
85
  end
70
86
 
71
- def affirms_inclusion_of(attribute, list: [])
87
+ def affirms_inclusion_of(attribute, opts = {}, list: [])
72
88
  affirmations << {
73
89
  :attribute => attribute, :type => :inclusion, :list => list
74
- }
90
+ }.merge(opts)
75
91
  end
76
92
 
77
- def affirms_length_of(attribute, range: 0..0)
93
+ def affirms_length_of(attribute, opts = {}, range: 0..0)
78
94
  affirmations << {
79
95
  :attribute => attribute, :type => :length, :range => range
80
- }
96
+ }.merge(opts)
81
97
  end
82
98
 
83
- def affirms_numericality_of(attribute)
84
- affirmations << {:attribute => attribute, :type => :numericality}
99
+ def affirms_numericality_of(attribute, opts = {})
100
+ affirmations << {
101
+ :attribute => attribute, :type => :numericality
102
+ }.merge(opts)
85
103
  end
86
104
 
87
- def affirms_presence_of(attribute)
88
- affirmations << {:attribute => attribute, :type => :presence}
105
+ def affirms_presence_of(attribute, opts = {})
106
+ affirmations << {
107
+ :attribute => attribute, :type => :presence
108
+ }.merge(opts)
89
109
  end
90
110
 
91
111
  def affirmations
@@ -1,3 +1,3 @@
1
1
  module DailyAffirmation
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -114,4 +114,20 @@ describe DailyAffirmation do
114
114
  expect(affirmation).to be_valid
115
115
  end
116
116
  end
117
+
118
+ it "allows each affirmation to be skipped via an :if option" do
119
+ cls = Class.new do
120
+ include DailyAffirmation.affirmations
121
+
122
+ affirms_presence_of :name, :if => Proc.new { object.age > 10 }
123
+ end
124
+
125
+ obj1 = double(:name => nil, :age => 1)
126
+ obj2 = double(:name => nil, :age => 11)
127
+
128
+ affirmation1 = cls.new(obj1)
129
+ affirmation2 = cls.new(obj2)
130
+ expect(affirmation1).to be_valid
131
+ expect(affirmation2).to_not be_valid
132
+ end
117
133
  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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-30 00:00:00.000000000 Z
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler