specstar-models 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +20 -0
- data/lib/specstar/models/associations.rb +32 -0
- data/lib/specstar/models/attributes.rb +43 -0
- data/lib/specstar/models/validations.rb +128 -0
- data/lib/specstar/models.rb +3 -174
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 340d09c4db76d22befcbff8f1c415df6a5082656
|
4
|
+
data.tar.gz: dc8ae599fbcdc34fb4106bc2f6b8e3dd19ffed54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cb803ff9c732d9d2f764aa34daff7512410239c878936daba8ed1ac9e976f989e1d9d8ffd70c934812806fcd330b3f1860a3fd92b9ed6ec95c4305e8dfd9c13
|
7
|
+
data.tar.gz: 655128e977fcc67f94a064a2db0753abd93d6b5433defc6eb59542b5ce2423dac23486a37fef18be4db9894c5962681792d8462ada097f6dfa530f63f226e28b
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rspec/core'
|
2
|
+
require 'rspec/expectations'
|
3
|
+
|
4
|
+
module Specstar
|
5
|
+
module Models
|
6
|
+
module Matchers
|
7
|
+
|
8
|
+
RSpec::Matchers.define :belong_to do |attr|
|
9
|
+
match do |model|
|
10
|
+
association = model.class.reflect_on_association(attr)
|
11
|
+
association && association.macro == :belongs_to
|
12
|
+
end
|
13
|
+
|
14
|
+
failure_message do |model|
|
15
|
+
"expected #{model.class} to belong to #{attr}."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec::Matchers.define :have_many do |attr|
|
20
|
+
match do |model|
|
21
|
+
association = model.class.reflect_on_association(attr)
|
22
|
+
association && association.macro == :has_many
|
23
|
+
end
|
24
|
+
|
25
|
+
failure_message do |model|
|
26
|
+
"expected #{model.class} to have many #{attr}."
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rspec/core'
|
2
|
+
require 'rspec/expectations'
|
3
|
+
|
4
|
+
module Specstar
|
5
|
+
module Models
|
6
|
+
module Matchers
|
7
|
+
RSpec::Matchers.define :have_attribute do |attr|
|
8
|
+
attr = attr.to_s
|
9
|
+
|
10
|
+
@extras = nil
|
11
|
+
chain :with do |extras|
|
12
|
+
@extras = extras
|
13
|
+
end
|
14
|
+
|
15
|
+
match do |target|
|
16
|
+
result = target.attributes.keys.map(&:to_s).include? attr
|
17
|
+
|
18
|
+
if result && @extras
|
19
|
+
properties = target.class.columns_hash[attr]
|
20
|
+
@extras.each_pair do |property, value|
|
21
|
+
result = false && break unless properties.send(property).to_s == value.to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
failure_message do |target|
|
29
|
+
"Expected #{target.class} to have an attribute '#{attr}'#{properties_to_sentence @extras}."
|
30
|
+
end
|
31
|
+
|
32
|
+
description do
|
33
|
+
"have an attribute '#{attr}'#{properties_to_sentence @extras}."
|
34
|
+
end
|
35
|
+
|
36
|
+
def properties_to_sentence(hash)
|
37
|
+
" of " + hash.map { |key, value| "#{key} #{value}" }.to_sentence if hash.present?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'rspec/core'
|
2
|
+
require 'rspec/expectations'
|
3
|
+
|
4
|
+
module Specstar
|
5
|
+
module Models
|
6
|
+
module Matchers
|
7
|
+
def validate_presence_of_methods_in_options(model, options)
|
8
|
+
if options[:if] && options[:if].is_a?(Symbol)
|
9
|
+
return false unless model.respond_to? options[:if]
|
10
|
+
end
|
11
|
+
|
12
|
+
if options[:unless] && options[:unless].is_a?(Symbol)
|
13
|
+
return false unless model.respond_to? options[:unless]
|
14
|
+
end
|
15
|
+
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
def undefined_method_in_options(model, options)
|
20
|
+
if options[:if] && options[:if].is_a?(Symbol)
|
21
|
+
return options[:if] unless model.respond_to? options[:if]
|
22
|
+
end
|
23
|
+
|
24
|
+
if options[:unless] && options[:unless].is_a?(Symbol)
|
25
|
+
return options[:unless] unless model.respond_to? options[:unless]
|
26
|
+
end
|
27
|
+
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_attribute?(model, attr, extras={})
|
32
|
+
attr = attr.to_s
|
33
|
+
|
34
|
+
result = model.attributes.include? attr
|
35
|
+
|
36
|
+
if result && extras.any?
|
37
|
+
properties = model.class.columns_hash[attr]
|
38
|
+
extras.each_pair do |property, value|
|
39
|
+
result = false && break unless properties.send(property).to_s == value.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
result
|
44
|
+
end
|
45
|
+
|
46
|
+
def has_association?(model, association)
|
47
|
+
model.class.reflect_on_all_associations.map { |a| a.name.to_s }.include? association.to_s
|
48
|
+
end
|
49
|
+
|
50
|
+
RSpec::Matchers.define :validate_presence_of do |attr, options|
|
51
|
+
match do |model|
|
52
|
+
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
53
|
+
model._validators[attr.to_sym].select do |validator|
|
54
|
+
validator.instance_of?(ActiveRecord::Validations::PresenceValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
|
55
|
+
end.size > 0
|
56
|
+
end
|
57
|
+
|
58
|
+
failure_message do |model|
|
59
|
+
if has_attribute?(model, attr) || has_association?(model, attr)
|
60
|
+
if options.nil? || validate_presence_of_methods_in_options(model, options)
|
61
|
+
"expected #{model.class} to validate presence of #{attr}."
|
62
|
+
else
|
63
|
+
"expected #{model.class} to define #{undefined_method_in_options(model, options)}."
|
64
|
+
end
|
65
|
+
else
|
66
|
+
"expected #{model.class} to have an attribute or association #{attr}."
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
RSpec::Matchers.define :validate_uniqueness_of do |attr, options|
|
72
|
+
match do |model|
|
73
|
+
(has_attribute?(model, attr) || has_association?(model, attr)) && model._validators[attr].select do |validator|
|
74
|
+
validator.instance_of?(ActiveRecord::Validations::UniquenessValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
|
75
|
+
end.size > 0
|
76
|
+
end
|
77
|
+
|
78
|
+
failure_message do |model|
|
79
|
+
if has_attribute?(model, attr) || has_association?(model, attr)
|
80
|
+
if options.nil? || validate_presence_of_methods_in_options(model, options)
|
81
|
+
"expected #{model.class} to validate uniqueness of #{attr}."
|
82
|
+
else
|
83
|
+
"expected #{model.class} to define #{undefined_method_in_options(model, options)}."
|
84
|
+
end
|
85
|
+
else
|
86
|
+
"expected #{model.class} to have an attribute or association #{attr}."
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
RSpec::Matchers.define :validate_inclusion_of do |attr, options|
|
92
|
+
match do |model|
|
93
|
+
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
94
|
+
model._validators[attr].select do |validator|
|
95
|
+
validator.instance_of?(ActiveRecord::Validations::InclusionValidator) && validator.options.merge(options) == validator.options
|
96
|
+
end.size > 0
|
97
|
+
end
|
98
|
+
|
99
|
+
failure_message do |model|
|
100
|
+
if has_attribute?(model, attr) || has_association?(model, attr)
|
101
|
+
"expected #{model.class} to validate inclusion of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
|
102
|
+
else
|
103
|
+
"expected #{model.class} to have an attribute or association #{attr}."
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
RSpec::Matchers.define :validate_numericality_of do |attr, options|
|
109
|
+
options = options || {}
|
110
|
+
|
111
|
+
match do |model|
|
112
|
+
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
113
|
+
model._validators[attr].select do |validator|
|
114
|
+
validator.instance_of?(ActiveRecord::Validations::NumericalityValidator) && validator.options.merge(options) == validator.options
|
115
|
+
end.size > 0
|
116
|
+
end
|
117
|
+
|
118
|
+
failure_message do |model|
|
119
|
+
if has_attribute?(model, attr) || has_association?(model, attr)
|
120
|
+
"expected #{model.class} to validate numericality of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
|
121
|
+
else
|
122
|
+
"expected #{model.class} to have an attribute or association #{attr}."
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/specstar/models.rb
CHANGED
@@ -1,174 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
module Specstar
|
5
|
-
module Models
|
6
|
-
module Matchers
|
7
|
-
def validate_presence_of_methods_in_options(model, options)
|
8
|
-
if options[:if] && options[:if].is_a?(Symbol)
|
9
|
-
return false unless model.respond_to? options[:if]
|
10
|
-
end
|
11
|
-
|
12
|
-
if options[:unless] && options[:unless].is_a?(Symbol)
|
13
|
-
return false unless model.respond_to? options[:unless]
|
14
|
-
end
|
15
|
-
|
16
|
-
true
|
17
|
-
end
|
18
|
-
|
19
|
-
def undefined_method_in_options(model, options)
|
20
|
-
if options[:if] && options[:if].is_a?(Symbol)
|
21
|
-
return options[:if] unless model.respond_to? options[:if]
|
22
|
-
end
|
23
|
-
|
24
|
-
if options[:unless] && options[:unless].is_a?(Symbol)
|
25
|
-
return options[:unless] unless model.respond_to? options[:unless]
|
26
|
-
end
|
27
|
-
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
|
31
|
-
RSpec::Matchers.define :have_attribute do |attr|
|
32
|
-
attr = attr.to_s
|
33
|
-
|
34
|
-
@extras = nil
|
35
|
-
chain :with do |extras|
|
36
|
-
@extras = extras
|
37
|
-
end
|
38
|
-
|
39
|
-
match do |target|
|
40
|
-
result = target.attributes.keys.map(&:to_s).include? attr
|
41
|
-
|
42
|
-
if result && @extras
|
43
|
-
properties = target.class.columns_hash[attr]
|
44
|
-
@extras.each_pair do |property, value|
|
45
|
-
result = false && break unless properties.send(property).to_s == value.to_s
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
result
|
50
|
-
end
|
51
|
-
|
52
|
-
failure_message do |target|
|
53
|
-
"Expected #{target.class} to have an attribute '#{attr}'#{properties_to_sentence @extras}."
|
54
|
-
end
|
55
|
-
|
56
|
-
description do
|
57
|
-
"have an attribute '#{attr}'#{properties_to_sentence @extras}."
|
58
|
-
end
|
59
|
-
|
60
|
-
def properties_to_sentence(hash)
|
61
|
-
" of " + hash.map { |key, value| "#{key} #{value}" }.to_sentence if hash.present?
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def has_attribute?(model, attr, extras={})
|
66
|
-
attr = attr.to_s
|
67
|
-
|
68
|
-
result = model.attributes.include? attr
|
69
|
-
|
70
|
-
if result && extras.any?
|
71
|
-
properties = model.class.columns_hash[attr]
|
72
|
-
extras.each_pair do |property, value|
|
73
|
-
result = false && break unless properties.send(property).to_s == value.to_s
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
result
|
78
|
-
end
|
79
|
-
|
80
|
-
def has_association?(model, association)
|
81
|
-
model.class.reflect_on_all_associations.map { |a| a.name.to_s }.include? association.to_s
|
82
|
-
end
|
83
|
-
|
84
|
-
RSpec::Matchers.define :validate_presence_of do |attr, options|
|
85
|
-
match do |model|
|
86
|
-
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
87
|
-
model._validators[attr.to_sym].select do |validator|
|
88
|
-
validator.instance_of?(ActiveRecord::Validations::PresenceValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
|
89
|
-
end.size > 0
|
90
|
-
end
|
91
|
-
|
92
|
-
failure_message do |model|
|
93
|
-
if has_attribute?(model, attr) || has_association?(model, attr)
|
94
|
-
if options.nil? || validate_presence_of_methods_in_options(model, options)
|
95
|
-
"expected #{model.class} to validate presence of #{attr}."
|
96
|
-
else
|
97
|
-
"expected #{model.class} to define #{undefined_method_in_options(model, options)}."
|
98
|
-
end
|
99
|
-
else
|
100
|
-
"expected #{model.class} to have an attribute or association #{attr}."
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
RSpec::Matchers.define :validate_uniqueness_of do |attr, options|
|
106
|
-
match do |model|
|
107
|
-
(has_attribute?(model, attr) || has_association?(model, attr)) && model._validators[attr].select do |validator|
|
108
|
-
validator.instance_of?(ActiveRecord::Validations::UniquenessValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
|
109
|
-
end.size > 0
|
110
|
-
end
|
111
|
-
|
112
|
-
failure_message do |model|
|
113
|
-
if has_attribute?(model, attr) || has_association?(model, attr)
|
114
|
-
if options.nil? || validate_presence_of_methods_in_options(model, options)
|
115
|
-
"expected #{model.class} to validate uniqueness of #{attr}."
|
116
|
-
else
|
117
|
-
"expected #{model.class} to define #{undefined_method_in_options(model, options)}."
|
118
|
-
end
|
119
|
-
else
|
120
|
-
"expected #{model.class} to have an attribute or association #{attr}."
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
RSpec::Matchers.define :validate_inclusion_of do |attr, options|
|
126
|
-
match do |model|
|
127
|
-
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
128
|
-
model._validators[attr].select do |validator|
|
129
|
-
validator.instance_of?(ActiveRecord::Validations::InclusionValidator) && validator.options.merge(options) == validator.options
|
130
|
-
end.size > 0
|
131
|
-
end
|
132
|
-
|
133
|
-
failure_message do |model|
|
134
|
-
if has_attribute?(model, attr) || has_association?(model, attr)
|
135
|
-
"expected #{model.class} to validate inclusion of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
|
136
|
-
else
|
137
|
-
"expected #{model.class} to have an attribute or association #{attr}."
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
RSpec::Matchers.define :validate_numericality_of do |attr, options|
|
143
|
-
options = options || {}
|
144
|
-
|
145
|
-
match do |model|
|
146
|
-
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
147
|
-
model._validators[attr].select do |validator|
|
148
|
-
validator.instance_of?(ActiveRecord::Validations::NumericalityValidator) && validator.options.merge(options) == validator.options
|
149
|
-
end.size > 0
|
150
|
-
end
|
151
|
-
|
152
|
-
failure_message do |model|
|
153
|
-
if has_attribute?(model, attr) || has_association?(model, attr)
|
154
|
-
"expected #{model.class} to validate numericality of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
|
155
|
-
else
|
156
|
-
"expected #{model.class} to have an attribute or association #{attr}."
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
RSpec::Matchers.define :belong_to do |attr|
|
162
|
-
match do |model|
|
163
|
-
association = model.class.reflect_on_association(attr)
|
164
|
-
association && association.macro == :belongs_to
|
165
|
-
end
|
166
|
-
|
167
|
-
failure_message do |model|
|
168
|
-
"expected #{model.class} to belong to #{attr}."
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
1
|
+
require_relative 'models/associations'
|
2
|
+
require_relative 'models/attributes'
|
3
|
+
require_relative 'models/validations'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: specstar-models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sujoy Gupta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-core
|
@@ -44,7 +44,11 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- MIT-LICENSE
|
47
48
|
- lib/specstar/models.rb
|
49
|
+
- lib/specstar/models/associations.rb
|
50
|
+
- lib/specstar/models/attributes.rb
|
51
|
+
- lib/specstar/models/validations.rb
|
48
52
|
homepage: http://github.com/sujoyg/specstar-models
|
49
53
|
licenses:
|
50
54
|
- MIT
|