specstar-models 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13690914c226ba34126023e5fa4780c96bb72013
4
- data.tar.gz: 8e65d4cc3d287db504038d6ecdd56c303c72ccc7
3
+ metadata.gz: e784cb5e009f04f5e545f88d7a1f14c86c706167
4
+ data.tar.gz: 561662558a4501a27744b28db68d6e3535dc94e9
5
5
  SHA512:
6
- metadata.gz: d31f50b197a678b571269815d5d9da6cf4e0b2bb01d87ad418b1d2e8b08a0163e4ca4da9e97f127e47967b45fc0dd9c9517635d552002ba284bf5d4d32ed24a7
7
- data.tar.gz: d203ab21ab597047ef02d5369d1dfeb277ccf51e191de79549caf52df1716a673a9e547d9d734634f92d67dbc53ab76f593935a71b1f13f79904642bcc3ae544
6
+ metadata.gz: aa7b2ac4f129c95b90fd4871b1d80ba4440d2c54ff7919da7b687b64908d24e052025c931399979047b107ae0452c938921b3753325204202f31da527ba927dc
7
+ data.tar.gz: 189eb054e7ce5f2abc3787dac866d45782b47abba561d70c49766dd78ec8df5fefaac0a3991248bb4bb3c724ecb4bebda1a308e3bb7abfc78de0f843adb5b1d5
@@ -53,10 +53,11 @@ module Specstar
53
53
  false
54
54
  else
55
55
  begin
56
- validators = custom_validations.each_pair.to_a.select { |attr, value| value == true }.map(&:first)
57
- validators.all? do |validator|
56
+ custom_validations.each_pair.to_a.all? do |validator, options|
58
57
  klass = "#{validator}_validator".classify.constantize
59
- model._validators[attr.to_sym].select { |v| v.instance_of? klass }.size > 0
58
+ model._validators[attr.to_sym].select { |v|
59
+ v.instance_of?(klass) && (!options.is_a?(Hash) || v.options == options)
60
+ }.size > 0
60
61
  end
61
62
  rescue
62
63
  false
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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sujoy Gupta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-23 00:00:00.000000000 Z
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-core
@@ -49,7 +49,6 @@ files:
49
49
  - lib/specstar/models/associations.rb
50
50
  - lib/specstar/models/attributes.rb
51
51
  - lib/specstar/models/validations.rb
52
- - lib/specstar/models/validations.rb~
53
52
  homepage: http://github.com/sujoyg/specstar-models
54
53
  licenses:
55
54
  - MIT
@@ -1,128 +0,0 @@
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?(ActiveModel::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