godel 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +10 -0
- data/godel.gemspec +24 -0
- data/lib/godel.rb +54 -0
- data/lib/godel/class_methods.rb +15 -0
- data/lib/godel/version.rb +3 -0
- data/lib/godel/warnings.rb +43 -0
- data/test/test_helper.rb +42 -0
- data/test/unit/incompleteness_test.rb +29 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07d0985ff32342c58cb61fe968a193fc5ab067f4
|
4
|
+
data.tar.gz: 4719d79dd938c653e29f7dc88023c9135bc89c84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f3a7521b20036ace6f568ed182da21aea39e94d80bd8cc487b59630105002dcef1f6d95eb92852f07d0780c0139c85a6386518c921537e3ae018c91aa5668a0
|
7
|
+
data.tar.gz: 5031bea9ed1f3cd2661639bc5962db26f97e3fbc104a4bd41ff8553e0a60811ca33d7e564ef56d19680f3a5c0bc584463b3682f066f48aa5a50c7dada80ac03b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Alex Jarvis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Godel
|
2
|
+
##Validations, Except they don't do anything.
|
3
|
+
|
4
|
+
In Rails apps, it is possible that a model might want another state above 'valid' or 'invalid'. Unreliable inputs may put you in a situation where you can not guarantee the integrity of every entry in your database, but strict validations might cause you to lose fuzzy data.
|
5
|
+
|
6
|
+
This gem gives another 'state' about validity, "completeness". A model can require certain attributes or methods to be present in order to be 'complete', giving you another powerful tool when solidfying the integrity of your data.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'godel'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install godel
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
include Godel in any model you'd like to measure completeness in.
|
25
|
+
|
26
|
+
include Godel
|
27
|
+
|
28
|
+
You can define attributes required for completeness with the 'completes' command. you can also use
|
29
|
+
"incomplete_without".
|
30
|
+
|
31
|
+
completes :foo, :bar, :baz
|
32
|
+
|
33
|
+
You can also define methods required for completeness. In these methods, create a warning (analagous to 'errors' in validations) should it be incomplete.
|
34
|
+
|
35
|
+
completes_method :must_have_name_or_email
|
36
|
+
|
37
|
+
...
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def must_have_name_or_email
|
42
|
+
if name.blank? && email.blank?
|
43
|
+
warnings.add(:name_or_email, "Needs either Name or Email")
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
You can query the completeness of a method with the #complete? or #incomplete? method, and you can see the relevant warnings with the #warnings method.
|
50
|
+
|
51
|
+
|
52
|
+
class ReadmeClass
|
53
|
+
completes :foo
|
54
|
+
completes_method :must_have_bar_over_five
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def must_have_bar_over_five
|
59
|
+
if bar < 5
|
60
|
+
warnings.add(:bar_over_five, "Bar must be > 5")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
readme = ReadmeClass.create
|
66
|
+
readme.incomplete?
|
67
|
+
# => true
|
68
|
+
readme.warnings
|
69
|
+
# => #<Godel::Warnings::0x007efc7f7b11a0 @messages={:foo => "ReademeClass is incomplete without foo", :bar_over_five => "Bar must be > 5"}
|
70
|
+
|
71
|
+
readme.update_attributes(foo: "Foo")
|
72
|
+
|
73
|
+
readme.incomplete?
|
74
|
+
# => true
|
75
|
+
readme.warnings
|
76
|
+
# => #<Godel::Warnings::0x007efc7f7b11a0 @messages={:bar_over_five => "Bar must be > 5"}
|
77
|
+
|
78
|
+
readme.update_attributes(bar: 6)
|
79
|
+
j
|
80
|
+
readme.incomplete?
|
81
|
+
# => false
|
82
|
+
readme.warnings
|
83
|
+
# => #<Godel::Warnings::0x007efc7f7b11a0 @messages={}
|
84
|
+
|
85
|
+
readme.complete?
|
86
|
+
#=> true
|
87
|
+
|
88
|
+
## Development Goals
|
89
|
+
- api matching rails validation syntax
|
90
|
+
- decouple from rails (allow completeness on PORO's)
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/godel.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'godel/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "godel"
|
8
|
+
spec.version = Godel::VERSION
|
9
|
+
spec.authors = ["Alex Jarvis"]
|
10
|
+
spec.email = ["alxjrvs@gmail.com"]
|
11
|
+
spec.description = %q{Validations, except they don't do anything.}
|
12
|
+
spec.summary = %q{In Rails apps, it is possible that a model might want another state above 'valid' or 'invalid'. Unreliable inputs may put you in a situation where you can not guarantee the integrity of every entry in your database, but strict validations might cause you to lose fuzzy data. This gem gives another 'state' about validity, "completeness". A model can require certain attributes or methods to be present in order to be 'complete', giving you another powerful tool when solidfying the integrity of your data. }
|
13
|
+
spec.homepage = "https://github.com/alxjrvs/godel"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "byebug"
|
24
|
+
end
|
data/lib/godel.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "godel/version"
|
2
|
+
require 'godel/warnings'
|
3
|
+
require 'godel/class_methods'
|
4
|
+
|
5
|
+
module Godel
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend ClassMethods
|
8
|
+
klass.cattr_accessor :_incomplete_methods, :_incomplete_attrs
|
9
|
+
klass.after_save :clear_godel_cache
|
10
|
+
klass.after_create :clear_godel_cache
|
11
|
+
klass._incomplete_methods ||= []
|
12
|
+
klass._incomplete_attrs ||= []
|
13
|
+
end
|
14
|
+
|
15
|
+
def complete?
|
16
|
+
!incomplete?
|
17
|
+
end
|
18
|
+
|
19
|
+
def incomplete?
|
20
|
+
@_incomplete ||= begin
|
21
|
+
return false if _incomplete_attrs.empty? && _incomplete_methods.empty?
|
22
|
+
warnings.any?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_accessor :warnings
|
27
|
+
|
28
|
+
def warnings
|
29
|
+
@_warnings ||= Warnings.new
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
|
35
|
+
def clear_godel_cache
|
36
|
+
@_incomplete = nil
|
37
|
+
warnings.clear!
|
38
|
+
_filter_incomplete_attributes
|
39
|
+
_filter_incomplete_methods
|
40
|
+
end
|
41
|
+
|
42
|
+
def _filter_incomplete_attributes
|
43
|
+
_incomplete_attrs.each do |attr|
|
44
|
+
value = self.send(attr)
|
45
|
+
warnings.add(attr, "#{self.class} is incomplete without #{attr}") unless value.present?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def _filter_incomplete_methods
|
50
|
+
_incomplete_methods.each do |method|
|
51
|
+
self.send(method)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ClassMethods
|
2
|
+
def completes(*args)
|
3
|
+
args.map(&:to_sym).each {|arg| _incomplete_attrs << arg}
|
4
|
+
end
|
5
|
+
alias_method :incomplete_without, :completes
|
6
|
+
|
7
|
+
def completes_method(method)
|
8
|
+
_incomplete_methods << method.to_sym
|
9
|
+
end
|
10
|
+
alias_method :incomplete_unless, :completes_method
|
11
|
+
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Godel
|
2
|
+
class Warnings
|
3
|
+
attr_accessor :messages
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@messages = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def values
|
10
|
+
messages.values
|
11
|
+
end
|
12
|
+
|
13
|
+
def add(symbol, message = nil)
|
14
|
+
message ||= "is missing"
|
15
|
+
messages[symbol] = message
|
16
|
+
end
|
17
|
+
|
18
|
+
def any?
|
19
|
+
messages.any?
|
20
|
+
end
|
21
|
+
|
22
|
+
def empty?
|
23
|
+
messages.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def blank?
|
27
|
+
messages.blank?
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_a
|
31
|
+
full_messages
|
32
|
+
end
|
33
|
+
|
34
|
+
def full_messages
|
35
|
+
array = []
|
36
|
+
messages.each {|k,v| array << "#{k.to_s.titleize} #{v}"}
|
37
|
+
end
|
38
|
+
|
39
|
+
def clear!
|
40
|
+
self.messages = {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
using_git = File.exist?(File.expand_path('../../.git/', __FILE__))
|
4
|
+
require 'bundler/setup' if using_git
|
5
|
+
|
6
|
+
require File.expand_path('../../lib/godel.rb', __FILE__)
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'minitest/pride'
|
9
|
+
require 'active_record'
|
10
|
+
require 'byebug'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
self.verbose = false
|
16
|
+
|
17
|
+
create_table :test_classes, force: true do |t|
|
18
|
+
t.string :foo
|
19
|
+
t.string :baz
|
20
|
+
t.integer :pug
|
21
|
+
t.integer :stuff
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
class TestClass < ActiveRecord::Base
|
26
|
+
include Godel
|
27
|
+
|
28
|
+
completes :foo, :baz
|
29
|
+
completes_method :either_pug_or_stuff
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def either_pug_or_stuff
|
34
|
+
if pug.blank? && stuff.blank?
|
35
|
+
warnings.add(:pug_or_stuff, "Needs either Pug or Stuff to be complete")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
class IncompletenessTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_that_included_models_respond_to_methods
|
6
|
+
assert @test_class.respond_to? :incomplete?
|
7
|
+
assert @test_class.respond_to? :complete?
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_that_incomplete_models_report_correctly
|
11
|
+
assert @test_class.incomplete?, "Freshly created model with no attributes should be incomplete"
|
12
|
+
refute @test_class.complete?, "Freshly created models with no attributes should not be complete"
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_that_incomplete_models_may_be_made_complete
|
16
|
+
assert @test_class.incomplete?, "Model is new and has no attributes, should be incomplete."
|
17
|
+
@test_class.update_attributes(foo: "foo", baz: "baz", pug: 52, stuff: 666)
|
18
|
+
assert @test_class.complete?, "Model has satisfied the given avenues of completeness, and should be complete"
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup
|
22
|
+
@test_class = TestClass.create
|
23
|
+
end
|
24
|
+
|
25
|
+
def teardown
|
26
|
+
TestClass.destroy_all
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: godel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Jarvis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-02 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Validations, except they don't do anything.
|
56
|
+
email:
|
57
|
+
- alxjrvs@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- godel.gemspec
|
68
|
+
- lib/godel.rb
|
69
|
+
- lib/godel/class_methods.rb
|
70
|
+
- lib/godel/version.rb
|
71
|
+
- lib/godel/warnings.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
- test/unit/incompleteness_test.rb
|
74
|
+
homepage: https://github.com/alxjrvs/godel
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: In Rails apps, it is possible that a model might want another state above
|
98
|
+
'valid' or 'invalid'. Unreliable inputs may put you in a situation where you can
|
99
|
+
not guarantee the integrity of every entry in your database, but strict validations
|
100
|
+
might cause you to lose fuzzy data. This gem gives another 'state' about validity,
|
101
|
+
"completeness". A model can require certain attributes or methods to be present
|
102
|
+
in order to be 'complete', giving you another powerful tool when solidfying the
|
103
|
+
integrity of your data.
|
104
|
+
test_files:
|
105
|
+
- test/test_helper.rb
|
106
|
+
- test/unit/incompleteness_test.rb
|