active_conformity 0.2.13 → 0.2.14

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: 04c7e1d71487e2d58faf2cbf582e10386f4b1018
4
- data.tar.gz: 5f5a5e28dae4bb907dcf8644bb9de7c3157c15b4
3
+ metadata.gz: b887d6011a6343f3f3e1fc483a3f8d412e9d2dee
4
+ data.tar.gz: 405673f04c38f9befc12cb4e1465544ab2a72c27
5
5
  SHA512:
6
- metadata.gz: 5fd79e607d2317bc41ad6c9f2b46653e6ff54cf345742db1e3a9d9934d0552848230fd205998928d02bed98fddb86383ae16ee2e68f76a671f04442525ae1095
7
- data.tar.gz: e50e71111dc5c00dc907e09af1688574367ce5f433eb61323a007a283844f215014bc8291af887b3af9804c61c4fa1bb47a53c7325d0a5cb3a21a375175701c2
6
+ metadata.gz: 1cde6a00f8981dcf2d88804dbfc1b72dec9c51bcb7e73e7f1a137d18e9a3a4b32a564cb326663aa26e2129eba55bd11dab8a0d74c6934ad36223b41069f7ef2e
7
+ data.tar.gz: 777988858ef4c4f2ec9c538d6e90f08bedaa23825faa882d8b88a75e0ec30375c7d411344936e7efd18cf998730518b0630c12846461e05eb33777b9ac4e4c83
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # ActiveConformity
2
+
3
+ [![Join the chat at https://gitter.im/dandlezzz/active_conformity](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dandlezzz/active_conformity?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2
4
  [![Code Climate](https://codeclimate.com/github/dandlezzz/active_conformity/badges/gpa.svg)](https://codeclimate.com/github/dandlezzz/active_conformity)
3
5
 
4
6
  Your favorite rails validations driven not by code but by your data.
@@ -31,23 +33,23 @@ ActiveConformity comes with a helpful install generator.
31
33
  ActiveConformity is for use when validating if objects are of a specific composition.
32
34
  For example, lets say you have the following model structure:
33
35
 
34
- ```
36
+ ```ruby
35
37
  class Car
36
- has_one :engine
38
+ belongs_to :engine
37
39
  #attrs :size
38
40
  end
39
41
 
40
42
  class Engine
41
- belongs_to :car
43
+
42
44
  end
43
45
  ```
44
46
 
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.
47
+ In this example you have a database full of different engines. Each car instance has an engine. If you want ensure that the diesel engine is on a car of a size 2000 you have a couple of options.
46
48
 
47
- ```
49
+ ```ruby
48
50
  class Car
49
- has_one :engine
50
- validate :proper_engine
51
+ belongs_to :engine
52
+ validate :proper_engine
51
53
 
52
54
  def proper_engine
53
55
  return true if size >= 2000 && engine.name == "diesel"
@@ -57,14 +59,14 @@ end
57
59
 
58
60
  ```
59
61
 
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.
62
+ This works but can become very complex if you have lots of engines and each with their own special requirements of the car. ActiveConformity provides a way to add these conditions to your database, as json, and then run the validations from the persisted json, that json is referred to as a conformity_set.
61
63
 
62
- ```
64
+ ```ruby
63
65
  diesel_engine = Engine.find_by(name: "diesel")
64
66
  diesel_engine.add_conformity_set!( {size: {:numericality => { :greater_than => 2000} } }, conformist_type: "Car")
65
67
 
66
- car1 = Car.create!(size: 2000, engine: diesel_engine)
67
- car1.conforms? # true
68
+ car = Car.create!(size: 2000, engine: diesel_engine)
69
+ car.conforms? # true
68
70
  car2 = Car.create!(size: 1000, engine: diesel_engine)
69
71
  car2.conforms? # false
70
72
  car2.conformity_errors # [{size: "car is too small for diesel engine"}]
@@ -83,9 +85,10 @@ ActiveConformity refers to the objects that tell other objects what do as confor
83
85
 
84
86
  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
87
 
88
+ ```ruby
89
+ car.aggregate_conformity_set # {:size=> {:numericality => { :greater_than => 2000} } }
86
90
  ```
87
- car1.aggregate_conformity_set # {size: {:numericality => { :greater_than => 2000} } }
88
- ```
91
+
89
92
 
90
93
  This shows all of the validations that the model will have to run through when .conforms? is called.
91
94
 
@@ -93,19 +96,35 @@ This shows all of the validations that the model will have to run through when .
93
96
 
94
97
  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
98
 
96
- ```
97
- car1.conformable_references #returns [diesel_engine]
99
+ ```ruby
100
+ car.conformable_references #returns [diesel_engine]
98
101
  ```
99
102
  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} } } }
103
+
104
+ ```ruby
105
+ car.conformity_sets_by_reference # {"Engine id: 1" =>{:size=> {:numericality => { :greater_than => 2000} } } }
102
106
  ```
103
107
  This returns a complex hash that shows the id of all of the objects mapped to their individual conformity_set.
104
108
 
105
- ##
109
+ ## Removing Conformity sets
110
+
111
+ ActiveConformity provides two ways to remove a misplaced conformity rule or set from a conformable. For example, if you had incorrectly added an extra conformity set to the diesel engine, you could remove it like so:
112
+
113
+ ```ruby
114
+ # the bad rule
115
+ diesel_engine.add_conformity_set!( {size: {:numericality => { :greater_than => 999999} } }, conformist_type: "Car")
116
+ # remove it this way
117
+ diesel_engine.remove_coformity_rule!(:size)
106
118
 
119
+ ```
120
+ The bang here will run a save!, without the bang the diesel engine will still need to save in order to persist. Additionally, if you want to remove all of the conformity sets for a conformable, you can do the following:
107
121
 
122
+ ```ruby
123
+ diesel_engine.remove_rules!
124
+ ```
125
+ Again this method can be used with or without a bang!
108
126
 
127
+ ##
109
128
  ## Development
110
129
 
111
130
  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.
@@ -119,3 +138,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
119
138
  3. Commit your changes (`git commit -am 'Add some feature'`)
120
139
  4. Push to the branch (`git push origin my-new-feature`)
121
140
  5. Create a new Pull Request
141
+
142
+
143
+ ## Help and Inspiration
144
+ Thanks to [Dan Barrett](https://github.com/thoughtpunch) who has provided guidance and inspiration for this project.
@@ -23,6 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.8"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
- spec.add_development_dependency "activerecord"
26
+ spec.add_development_dependency "activerecord", "~> 4.2"
27
27
  spec.add_development_dependency "sqlite3"
28
28
  end
@@ -26,6 +26,15 @@ module ActiveConformity
26
26
  self.conformity_set = conformity_set.to_json
27
27
  end
28
28
 
29
+ def remove_rules
30
+ self.conformity_set = {}.to_json
31
+ end
32
+
33
+ def remove_rules!
34
+ remove_rules
35
+ save!
36
+ end
37
+
29
38
  def remove_coformity_rule!(attr)
30
39
  remove_coformity_rule(attr)
31
40
  save!
@@ -11,8 +11,9 @@ module ActiveConformity
11
11
  end
12
12
 
13
13
  def conforming_dependents(*dependents)
14
+ association_names = self.reflect_on_all_associations.map(&:name)
14
15
  dependents.each do |d|
15
- if !self.reflect_on_all_associations.map(&:name).include?(d)
16
+ if !association_names.include?(d)
16
17
  raise "NOT A VALID DEPENDENT, MUST BE ONE OF THE MODEL'S ASSOCIATIONS!"
17
18
  end
18
19
  end
@@ -56,8 +56,6 @@ class ConformitySetValidator < ActiveModel::EachValidator
56
56
  true
57
57
  end
58
58
 
59
- #conformity set by conformable
60
-
61
59
  def is_a_conformists_attribute?(str)
62
60
  str = str.to_s
63
61
  if !conformists_attributes.include?(str)
@@ -1,3 +1,3 @@
1
1
  module ActiveConformity
2
- VERSION = "0.2.13"
2
+ VERSION = "0.2.14"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_conformity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - dandlezzz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-23 00:00:00.000000000 Z
11
+ date: 2015-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: activerecord
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '4.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '4.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +97,6 @@ files:
97
97
  - active_conformity.gemspec
98
98
  - bin/console
99
99
  - bin/setup
100
- - lib/.DS_Store
101
100
  - lib/active_conformity.rb
102
101
  - lib/active_conformity/conformable.rb
103
102
  - lib/active_conformity/conformable_extensions.rb
@@ -129,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
128
  version: '0'
130
129
  requirements: []
131
130
  rubyforge_project:
132
- rubygems_version: 2.4.5
131
+ rubygems_version: 2.4.8
133
132
  signing_key:
134
133
  specification_version: 4
135
134
  summary: Database driven validations.
data/lib/.DS_Store DELETED
Binary file