active_conformity 0.2.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +3 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +121 -0
- data/Rakefile +3 -0
- data/active_conformity.gemspec +28 -0
- data/bin/console +7 -0
- data/bin/setup +7 -0
- data/lib/.DS_Store +0 -0
- data/lib/active_conformity/conformable.rb +34 -0
- data/lib/active_conformity/conformable_extensions.rb +116 -0
- data/lib/active_conformity/conformity_set_validator.rb +81 -0
- data/lib/active_conformity/object_validator.rb +99 -0
- data/lib/active_conformity/reifiers.rb +19 -0
- data/lib/active_conformity/version.rb +3 -0
- data/lib/active_conformity.rb +9 -0
- data/lib/generators/active_conformity/install/install_generator.rb +50 -0
- data/lib/generators/active_conformity/install/templates/active_conformity_custom_validation_methods.rb.erb +29 -0
- data/lib/generators/active_conformity/install/templates/active_conformity_migration.rb.erb +15 -0
- data/tasks/test.rake +12 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04c7e1d71487e2d58faf2cbf582e10386f4b1018
|
4
|
+
data.tar.gz: 5f5a5e28dae4bb907dcf8644bb9de7c3157c15b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5fd79e607d2317bc41ad6c9f2b46653e6ff54cf345742db1e3a9d9934d0552848230fd205998928d02bed98fddb86383ae16ee2e68f76a671f04442525ae1095
|
7
|
+
data.tar.gz: e50e71111dc5c00dc907e09af1688574367ce5f433eb61323a007a283844f215014bc8291af887b3af9804c61c4fa1bb47a53c7325d0a5cb3a21a375175701c2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Daniel Miller
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# ActiveConformity
|
2
|
+
[![Code Climate](https://codeclimate.com/github/dandlezzz/active_conformity/badges/gpa.svg)](https://codeclimate.com/github/dandlezzz/active_conformity)
|
3
|
+
|
4
|
+
Your favorite rails validations driven not by code but by your data.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'active_conformity'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install active_conformity
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
ActiveConformity comes with a helpful install generator.
|
25
|
+
|
26
|
+
$ rails g active_conformity:install
|
27
|
+
|
28
|
+
This will generate a migration for you that creates the conformables table. The table responsible for storing all of the validation data. Additionally, it will create a module in your lib file where you can write custom validation methods.
|
29
|
+
|
30
|
+
|
31
|
+
ActiveConformity is for use when validating if objects are of a specific composition.
|
32
|
+
For example, lets say you have the following model structure:
|
33
|
+
|
34
|
+
```
|
35
|
+
class Car
|
36
|
+
has_one :engine
|
37
|
+
#attrs :size
|
38
|
+
end
|
39
|
+
|
40
|
+
class Engine
|
41
|
+
belongs_to :car
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
In this example you have a database full of different engines. Each car model has a link to these things. If you want ensure that the diesel engine is on a car of a size 2000 you have a couple of options.
|
46
|
+
|
47
|
+
```
|
48
|
+
class Car
|
49
|
+
has_one :engine
|
50
|
+
validate :proper_engine
|
51
|
+
|
52
|
+
def proper_engine
|
53
|
+
return true if size >= 2000 && engine.name == "diesel"
|
54
|
+
errors.add("car is too small for diesel engine")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
```
|
59
|
+
|
60
|
+
This works but can become very complex if you have lots of engines and even more complicated conditions. ActiveConformity provides a way to add these conditions to your database, as json.
|
61
|
+
|
62
|
+
```
|
63
|
+
diesel_engine = Engine.find_by(name: "diesel")
|
64
|
+
diesel_engine.add_conformity_set!( {size: {:numericality => { :greater_than => 2000} } }, conformist_type: "Car")
|
65
|
+
|
66
|
+
car1 = Car.create!(size: 2000, engine: diesel_engine)
|
67
|
+
car1.conforms? # true
|
68
|
+
car2 = Car.create!(size: 1000, engine: diesel_engine)
|
69
|
+
car2.conforms? # false
|
70
|
+
car2.conformity_errors # [{size: "car is too small for diesel engine"}]
|
71
|
+
|
72
|
+
```
|
73
|
+
|
74
|
+
The add_conformity_set! method saves the json to your database any time a car has a diesel engine, calling .conforms? will check the car's size to ensure it can accomodate the diesel engine.
|
75
|
+
|
76
|
+
##
|
77
|
+
|
78
|
+
Please note, lack of conformity does not prevent persistence as it does with .valid? in Rails. It is suggested that you implement this in a callback on the model.
|
79
|
+
|
80
|
+
##
|
81
|
+
|
82
|
+
ActiveConformity refers to the objects that tell other objects what do as conformables and the objects that are being told what to do as conformists. In the example above, the car is the conformist and the engine is the conformable.
|
83
|
+
|
84
|
+
There are several methods available to inspect what makes an object on conform. In the previous example if you want to see all of the rules the car most conform to you can do the following.
|
85
|
+
|
86
|
+
```
|
87
|
+
car1.aggregate_conformity_set # {size: {:numericality => { :greater_than => 2000} } }
|
88
|
+
```
|
89
|
+
|
90
|
+
This shows all of the validations that the model will have to run through when .conforms? is called.
|
91
|
+
|
92
|
+
##
|
93
|
+
|
94
|
+
In order to debug conformity errors, ActiveConformity provides several methods to query the database in order to get a better understanding of why the object conforms or does not.
|
95
|
+
|
96
|
+
```
|
97
|
+
car1.conformable_references #returns [diesel_engine]
|
98
|
+
```
|
99
|
+
The conformable references returns a list of all the objects that the car gets a conformity set from. Additionally, for even more fine grained debugging you can call
|
100
|
+
```
|
101
|
+
car1.conformity_sets_by_reference # {"Engine id: 1" =>{size: {:numericality => { :greater_than => 2000} } } }
|
102
|
+
```
|
103
|
+
This returns a complex hash that shows the id of all of the objects mapped to their individual conformity_set.
|
104
|
+
|
105
|
+
##
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
## Development
|
110
|
+
|
111
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
112
|
+
|
113
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
1. Fork it ( https://github.com/[my-github-username]/active_conformity/fork )
|
118
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
119
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
120
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
121
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_conformity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_conformity"
|
8
|
+
spec.version = ActiveConformity::VERSION
|
9
|
+
spec.authors = ["dandlezzz"]
|
10
|
+
spec.email = ["danm@workwithopal.com"]
|
11
|
+
|
12
|
+
|
13
|
+
spec.summary = "Database driven validations."
|
14
|
+
spec.description = "Store Rails validations as JSON to serve via api and drive complex validation logic."
|
15
|
+
spec.homepage = "http://www.github.com/dandlezzz/active_conformity"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "activerecord"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_conformity/conformity_set_validator'
|
2
|
+
module ActiveConformity
|
3
|
+
class Conformable < ActiveRecord::Base
|
4
|
+
validates :conformity_set, conformity_set: true
|
5
|
+
|
6
|
+
def add_conformity_set(incoming_set={})
|
7
|
+
self.conformity_set = JSON.parse(self.conformity_set) if self.conformity_set.is_a?(String)
|
8
|
+
conformity_set = JSON.parse(incoming_set) rescue incoming_set
|
9
|
+
conformity_set = self.conformity_set.deep_merge(incoming_set) rescue conformity_set
|
10
|
+
self.conformity_set = conformity_set.to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
def conformity_set
|
14
|
+
if super.is_a? String
|
15
|
+
JSON.parse(super).deep_symbolize_keys! rescue super
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def remove_coformity_rule(attr)
|
22
|
+
conformity_set = JSON.parse(self.conformity_set) rescue self.conformity_set
|
23
|
+
conformity_set.delete(attr) do
|
24
|
+
raise "no rule found for #{attr.to_s}"
|
25
|
+
end
|
26
|
+
self.conformity_set = conformity_set.to_json
|
27
|
+
end
|
28
|
+
|
29
|
+
def remove_coformity_rule!(attr)
|
30
|
+
remove_coformity_rule(attr)
|
31
|
+
save!
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
module ActiveConformity
|
3
|
+
module ConformableExtensions
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
@dependents = []
|
8
|
+
|
9
|
+
def dependents
|
10
|
+
@dependents
|
11
|
+
end
|
12
|
+
|
13
|
+
def conforming_dependents(*dependents)
|
14
|
+
dependents.each do |d|
|
15
|
+
if !self.reflect_on_all_associations.map(&:name).include?(d)
|
16
|
+
raise "NOT A VALID DEPENDENT, MUST BE ONE OF THE MODEL'S ASSOCIATIONS!"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
@dependents = dependents
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def conforms?
|
24
|
+
validator.conforms? && conforming_dependents_conform?
|
25
|
+
end
|
26
|
+
|
27
|
+
def conformity_errors
|
28
|
+
validator.errors.messages
|
29
|
+
end
|
30
|
+
|
31
|
+
def aggregate_conformity_set
|
32
|
+
acs = {}
|
33
|
+
return acs if !conformable_references.any? #need to think about this a little more
|
34
|
+
conformable_references.each do |c|
|
35
|
+
# This could be more efficient with some advanced sql techniques
|
36
|
+
# Also need indexes on these
|
37
|
+
c = Conformable.find_by!(conformable_id: c.id, conformable_type: c.class.name).conformity_set
|
38
|
+
c = JSON.parse(c) if c.is_a?(String)
|
39
|
+
acs.merge!(c)
|
40
|
+
end
|
41
|
+
acs
|
42
|
+
end
|
43
|
+
|
44
|
+
def conforming_dependents_conform?
|
45
|
+
return true if dependents.blank?
|
46
|
+
!dependents.flat_map { |da| self.send(da)}
|
47
|
+
.any?{ |da| !da.conforms? }
|
48
|
+
end
|
49
|
+
|
50
|
+
def conformity_sets_by_reference
|
51
|
+
conformable_references.flat_map do |cr|
|
52
|
+
{"#{cr.class.name} id: #{cr.id}"=> cr.conformable.conformity_set}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def conformable
|
57
|
+
@conformable ||= Conformable.find_by(conformable_id: self.id, conformable_type: self.class.name)
|
58
|
+
@conformable
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_conformity_set!(conformity_set = {}, conformist_type)
|
62
|
+
conformable_attrs = {conformable_id: self.id, conformable_type: self.class.name, conformist_type: conformist_type}
|
63
|
+
@conformable = Conformable.where(conformable_attrs).first_or_create
|
64
|
+
@conformable.add_conformity_set(conformity_set)
|
65
|
+
@conformable.save!
|
66
|
+
end
|
67
|
+
|
68
|
+
def conformable_references
|
69
|
+
[conformable_references_from_associations + add_self_to_conformable_references.to_a]
|
70
|
+
.flatten.compact.uniq
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def dependents
|
76
|
+
self.class.dependents
|
77
|
+
end
|
78
|
+
|
79
|
+
def method_missing(m, *args, &block)
|
80
|
+
if m.to_sym == :conformity_set && !self.class.column_names.include?(m.to_s)
|
81
|
+
conformable.try(:conformity_set)
|
82
|
+
else
|
83
|
+
super
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def validator
|
88
|
+
ActiveConformity::ObjectValidator.new(self, aggregate_conformity_set)
|
89
|
+
end
|
90
|
+
|
91
|
+
def conformable_references_from_associations
|
92
|
+
self.class.reflect_on_all_associations.flat_map do |assoc|
|
93
|
+
self.send(assoc.name) if conformable_types.include?(assoc.klass.name) rescue nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def add_self_to_conformable_references
|
98
|
+
[self] if Conformable.where(conformable_id: self.id,
|
99
|
+
conformable_type: self.class.name).any?
|
100
|
+
end
|
101
|
+
|
102
|
+
def conformable_types
|
103
|
+
return @conformable_types if defined?(@conformable_types)
|
104
|
+
@conformable_types = conformables_for_class.pluck(:conformable_type)
|
105
|
+
@conformable_types
|
106
|
+
end
|
107
|
+
|
108
|
+
def conformables_for_class
|
109
|
+
return @conformables_for_class if defined?(@conformables_for_class)
|
110
|
+
@conformables_for_class = Conformable.where(conformist_type: self.class.name)
|
111
|
+
@conformables_for_class
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
ActiveRecord::Base.include ActiveConformity::ConformableExtensions
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class ConformitySetValidator < ActiveModel::EachValidator
|
4
|
+
include ::ActiveConformity::Reifiers
|
5
|
+
|
6
|
+
attr_accessor :conformable
|
7
|
+
|
8
|
+
def validate_each(conformable, conformity_set, conformity_set_value)
|
9
|
+
@conformable = conformable
|
10
|
+
return add_errors("Conformity set required!") if conformity_set_value.nil?
|
11
|
+
begin
|
12
|
+
if conformity_set_value.is_a? String
|
13
|
+
conformity_set_value = JSON.parse(conformity_set_value)
|
14
|
+
end
|
15
|
+
rescue
|
16
|
+
return add_errors "#{conformity_set_value} cannot be parsed to a hash!"
|
17
|
+
end
|
18
|
+
conformity_set_value.each do |attribute, value|
|
19
|
+
return validate_custom_method(value) if attribute.to_sym == :method
|
20
|
+
validate_attr_based_validations(attribute, value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def validate_attr_based_validations(attribute, value)
|
27
|
+
is_a_conformists_attribute?(attribute)
|
28
|
+
value.each do |rule, constraint|
|
29
|
+
validation_rule_conforms?(attribute, rule, constraint)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def custom_method_is_defined?(method_name)
|
34
|
+
ActiveConformityCustomMethods.public_instance_methods.include?(method_name.to_sym)
|
35
|
+
end
|
36
|
+
|
37
|
+
def custom_method_error(method_name)
|
38
|
+
add_errors("#{method_name} is not defined in ActiveConformityCustomMethods!")
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate_custom_method(method_name)
|
42
|
+
return true
|
43
|
+
# custom_method_error(method_name) if !custom_method_is_defined?(method_name) # need a better solution here
|
44
|
+
end
|
45
|
+
|
46
|
+
def validation_rule_conforms?(attribute, rule, constraint)
|
47
|
+
attribute = attribute.to_sym if attribute.is_a?(String)
|
48
|
+
rule = rule.to_sym if rule.is_a?(String)
|
49
|
+
constraint.symbolize_keys! if constraint.is_a?(Hash)
|
50
|
+
begin
|
51
|
+
@conformable.conformist_type.constantize.dup.validates(attribute, reify_regex({rule => constraint}))
|
52
|
+
rescue ArgumentError => e
|
53
|
+
add_errors(e.to_s)
|
54
|
+
false
|
55
|
+
end
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
#conformity set by conformable
|
60
|
+
|
61
|
+
def is_a_conformists_attribute?(str)
|
62
|
+
str = str.to_s
|
63
|
+
if !conformists_attributes.include?(str)
|
64
|
+
return add_errors("#{str} is not an attribute of #{conformable.conformist_type.to_s}!")
|
65
|
+
end
|
66
|
+
return true
|
67
|
+
end
|
68
|
+
|
69
|
+
def conformists_attributes
|
70
|
+
if !conformable.conformist_type.constantize.respond_to?(:column_names)
|
71
|
+
raise "#{conformable.conformist_type} is not a valid conformist, must be an ActiveRecord Model"
|
72
|
+
else
|
73
|
+
conformable.conformist_type.constantize.column_names
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def add_errors(msg)
|
78
|
+
@conformable.errors.add(:conformity_set, msg)
|
79
|
+
return false
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
begin
|
3
|
+
require 'active_conformity_custom_methods'
|
4
|
+
rescue LoadError
|
5
|
+
#lets the user load their own custom methods
|
6
|
+
end
|
7
|
+
module ActiveConformity
|
8
|
+
class DynamicValidator
|
9
|
+
include ActiveModel::Validations
|
10
|
+
include ::ActiveConformityCustomMethods rescue false# complicated here
|
11
|
+
|
12
|
+
attr_reader :obj
|
13
|
+
attr_accessor :method_args
|
14
|
+
|
15
|
+
def initialize(obj)
|
16
|
+
@obj = obj
|
17
|
+
@method_args = {}
|
18
|
+
set_accessors
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def set_accessors
|
24
|
+
obj.attributes.each do |k,v|
|
25
|
+
self.class_eval do
|
26
|
+
attr_accessor k.to_sym
|
27
|
+
end
|
28
|
+
instance_variable_set("@#{k}", v)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ObjectValidator
|
34
|
+
include ActiveConformity::Reifiers
|
35
|
+
|
36
|
+
attr_accessor :conformity_set, :errors, :obj,
|
37
|
+
:validator_klass, :conforms, :validator
|
38
|
+
|
39
|
+
|
40
|
+
def initialize(obj, conformity_set)
|
41
|
+
@obj = obj
|
42
|
+
@conformity_set = ::HashWithIndifferentAccess.new(conformity_set)
|
43
|
+
@errors = {}
|
44
|
+
create_validator_klass
|
45
|
+
end
|
46
|
+
|
47
|
+
def conforms?
|
48
|
+
@conforms = true if @conformity_set.blank?
|
49
|
+
check_conformity
|
50
|
+
remove_dynamic_validator
|
51
|
+
@conforms
|
52
|
+
end
|
53
|
+
|
54
|
+
def errors
|
55
|
+
check_conformity
|
56
|
+
remove_dynamic_validator
|
57
|
+
@errors
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def remove_dynamic_validator
|
63
|
+
Object.send(:remove_const, @validator_klass.name.to_sym)
|
64
|
+
end
|
65
|
+
|
66
|
+
def create_validator_klass
|
67
|
+
validator_klass_name = (0...50).map { ('A'..'Z').to_a[rand(26)] }.join
|
68
|
+
@validator_klass = Object.const_set(validator_klass_name, Class.new(DynamicValidator))
|
69
|
+
end
|
70
|
+
|
71
|
+
def check_conformity
|
72
|
+
@validator = @validator_klass.new(@obj)
|
73
|
+
@conformity_set.each do |attr,rule|
|
74
|
+
call_validation_method(attr, rule)
|
75
|
+
end
|
76
|
+
@conforms = @validator.valid?
|
77
|
+
@errors = @validator.errors
|
78
|
+
end
|
79
|
+
|
80
|
+
def call_validation_method(attr, rule)
|
81
|
+
if attr.to_sym == :method
|
82
|
+
if rule.is_a?(String)
|
83
|
+
rule_name = rule.to_sym
|
84
|
+
elsif rule.is_a?(Hash)
|
85
|
+
rule_name = rule[:name].to_sym
|
86
|
+
set_custom_method_arguments(rule[:arguments])
|
87
|
+
end
|
88
|
+
|
89
|
+
@validator_klass.validate rule_name
|
90
|
+
else
|
91
|
+
@validator_klass.validates attr, reify_rule(rule)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def set_custom_method_arguments(args_hash)
|
96
|
+
args_hash.each{ |k,v| @validator.method_args[k] = v }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
module ActiveConformity
|
3
|
+
module Reifiers
|
4
|
+
|
5
|
+
def reify_rule(rule)
|
6
|
+
reify_regex(rule).deep_symbolize_keys
|
7
|
+
end
|
8
|
+
|
9
|
+
def reify_regex(rule)
|
10
|
+
return rule unless rule.is_a?(Hash)
|
11
|
+
if rule["format"]
|
12
|
+
rule["format"]["with"] = Regexp.new(rule["format"]["with"])
|
13
|
+
elsif rule[:format]
|
14
|
+
rule[:format][:with] = Regexp.new(rule[:format][:with])
|
15
|
+
end
|
16
|
+
return rule
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require "active_conformity/version"
|
2
|
+
require "active_conformity/reifiers"
|
3
|
+
require "active_conformity/object_validator"
|
4
|
+
require "active_conformity/conformable"
|
5
|
+
require "active_conformity/conformity_set_validator"
|
6
|
+
require "active_conformity/conformable_extensions"
|
7
|
+
|
8
|
+
|
9
|
+
module ActiveConformity; end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
module ActiveConformity
|
3
|
+
module Generators
|
4
|
+
class Install < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
desc "Creates the conformable model and table which stores the conformity sets as json
|
8
|
+
and the custom validation methods module in the lib directory."
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.next_migration_number(path)
|
15
|
+
@migration_number = Time.now.strftime("%Y%m%d%H%M%S")
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate_migration
|
19
|
+
migration_template "active_conformity_migration.rb.erb", "db/migrate/#{migration_file_name}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def migration_name
|
23
|
+
"create_conformables"
|
24
|
+
end
|
25
|
+
|
26
|
+
def migration_class_name
|
27
|
+
migration_name.camelize
|
28
|
+
end
|
29
|
+
|
30
|
+
def migration_file_name
|
31
|
+
"#{migration_name}.rb"
|
32
|
+
end
|
33
|
+
|
34
|
+
def conformity_set_type
|
35
|
+
database_adapters.fetch(ActiveRecord::Base.connection.adapter_name, 'json')
|
36
|
+
end
|
37
|
+
|
38
|
+
def database_adapters
|
39
|
+
{
|
40
|
+
'MySQL' => 'json',
|
41
|
+
'PostgreSQL' => 'json' # This can be jsonb for 9.4 and above
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_custom_methods_module_file
|
46
|
+
template "active_conformity_custom_validation_methods.rb.erb", "lib/active_conformity_custom_validation_methods.rb"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveConformityCustomMethods
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
=begin
|
8
|
+
This file is for creating custom methods that you can add to the conformity set
|
9
|
+
of a conformable. The object that these methods will run against is availible by
|
10
|
+
calling 'obj'. Additionally, you can utilize any of the named arguments that
|
11
|
+
were saved in the conformity set.
|
12
|
+
|
13
|
+
For example:
|
14
|
+
For a conformable with the conformity_set like:
|
15
|
+
|
16
|
+
{ method: { name: 'content_is?', arguments: {string: 'hello world' } } }
|
17
|
+
|
18
|
+
You can define the following method in this module:
|
19
|
+
|
20
|
+
def content_is?
|
21
|
+
if obj.content == method_args[:string]
|
22
|
+
return true
|
23
|
+
else
|
24
|
+
errors.add(:content, "does not match #{method_args[:string]}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Be sure to follow the rails convention of adding errors in the false case!
|
29
|
+
=end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :conformables do |t|
|
4
|
+
t.string :conformist_type
|
5
|
+
t.integer :conformable_id
|
6
|
+
t.string :conformable_type
|
7
|
+
t.<%=conformity_set_type%> :conformity_set
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :conformables
|
14
|
+
end
|
15
|
+
end
|
data/tasks/test.rake
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_conformity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.13
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dandlezzz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Store Rails validations as JSON to serve via api and drive complex validation
|
84
|
+
logic.
|
85
|
+
email:
|
86
|
+
- danm@workwithopal.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- active_conformity.gemspec
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/.DS_Store
|
101
|
+
- lib/active_conformity.rb
|
102
|
+
- lib/active_conformity/conformable.rb
|
103
|
+
- lib/active_conformity/conformable_extensions.rb
|
104
|
+
- lib/active_conformity/conformity_set_validator.rb
|
105
|
+
- lib/active_conformity/object_validator.rb
|
106
|
+
- lib/active_conformity/reifiers.rb
|
107
|
+
- lib/active_conformity/version.rb
|
108
|
+
- lib/generators/active_conformity/install/install_generator.rb
|
109
|
+
- lib/generators/active_conformity/install/templates/active_conformity_custom_validation_methods.rb.erb
|
110
|
+
- lib/generators/active_conformity/install/templates/active_conformity_migration.rb.erb
|
111
|
+
- tasks/test.rake
|
112
|
+
homepage: http://www.github.com/dandlezzz/active_conformity
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.4.5
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Database driven validations.
|
136
|
+
test_files: []
|