vanguard 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +17 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.devtools +56 -0
  7. data/Guardfile +18 -0
  8. data/LICENSE +21 -0
  9. data/README.md +80 -0
  10. data/Rakefile +2 -0
  11. data/TODO +3 -0
  12. data/config/flay.yml +3 -0
  13. data/config/flog.yml +2 -0
  14. data/config/mutant.yml +3 -0
  15. data/config/reek.yml +99 -0
  16. data/config/roodi.yml +26 -0
  17. data/config/yardstick.yml +2 -0
  18. data/lib/vanguard.rb +85 -0
  19. data/lib/vanguard/builder.rb +14 -0
  20. data/lib/vanguard/builder/nullary.rb +182 -0
  21. data/lib/vanguard/dsl.rb +43 -0
  22. data/lib/vanguard/dsl/evaluator.rb +197 -0
  23. data/lib/vanguard/evaluator.rb +72 -0
  24. data/lib/vanguard/instance_methods.rb +76 -0
  25. data/lib/vanguard/matcher.rb +19 -0
  26. data/lib/vanguard/matcher/binary.rb +59 -0
  27. data/lib/vanguard/matcher/binary/and.rb +20 -0
  28. data/lib/vanguard/matcher/binary/or.rb +20 -0
  29. data/lib/vanguard/matcher/binary/xor.rb +22 -0
  30. data/lib/vanguard/matcher/nullary.rb +27 -0
  31. data/lib/vanguard/matcher/nullary/equality.rb +46 -0
  32. data/lib/vanguard/matcher/nullary/format.rb +126 -0
  33. data/lib/vanguard/matcher/nullary/greater_than.rb +45 -0
  34. data/lib/vanguard/matcher/nullary/identity.rb +50 -0
  35. data/lib/vanguard/matcher/nullary/inclusion.rb +67 -0
  36. data/lib/vanguard/matcher/nullary/less_than.rb +48 -0
  37. data/lib/vanguard/matcher/nullary/primitive.rb +47 -0
  38. data/lib/vanguard/matcher/nullary/proc.rb +44 -0
  39. data/lib/vanguard/matcher/nullary/value.rb +32 -0
  40. data/lib/vanguard/matcher/unary.rb +45 -0
  41. data/lib/vanguard/matcher/unary/attribute.rb +49 -0
  42. data/lib/vanguard/matcher/unary/not.rb +25 -0
  43. data/lib/vanguard/result.rb +93 -0
  44. data/lib/vanguard/rule.rb +43 -0
  45. data/lib/vanguard/rule/guard.rb +103 -0
  46. data/lib/vanguard/rule/nullary.rb +81 -0
  47. data/lib/vanguard/rule/nullary/attribute.rb +69 -0
  48. data/lib/vanguard/rule/nullary/attribute/absence.rb +19 -0
  49. data/lib/vanguard/rule/nullary/attribute/format.rb +46 -0
  50. data/lib/vanguard/rule/nullary/attribute/inclusion.rb +62 -0
  51. data/lib/vanguard/rule/nullary/attribute/length.rb +184 -0
  52. data/lib/vanguard/rule/nullary/attribute/predicate.rb +10 -0
  53. data/lib/vanguard/rule/nullary/attribute/presence.rb +32 -0
  54. data/lib/vanguard/rule/nullary/attribute/presence/not_blank.rb +0 -0
  55. data/lib/vanguard/rule/nullary/attribute/presence/not_nil.rb +0 -0
  56. data/lib/vanguard/rule/nullary/attribute/primitive.rb +35 -0
  57. data/lib/vanguard/rule/nullary/confirmation.rb +210 -0
  58. data/lib/vanguard/support/blank.rb +29 -0
  59. data/lib/vanguard/validator.rb +116 -0
  60. data/lib/vanguard/validator/builder.rb +52 -0
  61. data/lib/vanguard/violation.rb +84 -0
  62. data/spec/integration/vanguard/dsl/guard_spec.rb +58 -0
  63. data/spec/integration/vanguard/dsl/validates_absence_of_spec.rb +19 -0
  64. data/spec/integration/vanguard/dsl/validates_acceptance_of_spec.rb +19 -0
  65. data/spec/integration/vanguard/dsl/validates_confirmation_of_spec.rb +27 -0
  66. data/spec/integration/vanguard/dsl/validates_format_of_spec.rb +79 -0
  67. data/spec/integration/vanguard/dsl/validates_inclusion_of_spec.rb +23 -0
  68. data/spec/integration/vanguard/dsl/validates_length_of_spec.rb +77 -0
  69. data/spec/integration/vanguard/dsl/validates_presence_of_spec.rb +19 -0
  70. data/spec/integration/vanguard/dsl/validates_value_of_spec.rb +30 -0
  71. data/spec/integration/vanguard/validator_spec.rb +57 -0
  72. data/spec/rcov.opts +6 -0
  73. data/spec/shared/dsl_spec.rb +73 -0
  74. data/spec/spec_helper.rb +3 -0
  75. data/spec/suite.rb +10 -0
  76. data/spec/unit/vanguard/support/blank_spec.rb +72 -0
  77. data/vanguard.gemspec +23 -0
  78. metadata +190 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f575ebb8349c3f8f5a118e6c1e724ce525072f16
4
+ data.tar.gz: 5a9fe00930811bc5884ce2cab9fb79a37fda2ad3
5
+ SHA512:
6
+ metadata.gz: 0be392034de335ce729c8443bc4eaa79ae9dd8a8b889327446ba6ba4225bf78d64d49991afdbeca4c1a938866dc00497874f3cd2b018a72da75c372ef860dcbe
7
+ data.tar.gz: 28e9ad166e44451e964be8aa1af11ef5f1bc572cdef8834503e8b3b94d5a1ffc2c4210cc7aaeb7f35dc058f99304c691be1baccf822bff15c737402da9720ad5
@@ -0,0 +1,4 @@
1
+ /.bundle
2
+ /Gemfile.lock
3
+ /.rbx
4
+ /vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --fail-fast
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ script: 'bundle exec rake spec'
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
7
+ - jruby-head
8
+ - rbx-19mode
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ notifications:
13
+ irc:
14
+ channels:
15
+ - irc.freenode.org#rom-rb
16
+ on_success: never
17
+ on_failure: change
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'devtools', :git => 'https://github.com/rom-rb/devtools.git'
6
+
7
+ eval File.read('Gemfile.devtools')
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ group :development do
4
+ gem 'rake', '~> 10.1.0'
5
+ gem 'rspec', '~> 2.13.0'
6
+ gem 'yard', '~> 0.8.6.2'
7
+ end
8
+
9
+ group :yard do
10
+ gem 'kramdown', '~> 1.1.0'
11
+ end
12
+
13
+ group :guard do
14
+ gem 'guard', '~> 1.8.1'
15
+ gem 'guard-bundler', '~> 1.0.0'
16
+ gem 'guard-rspec', '~> 3.0.2'
17
+
18
+ # file system change event handling
19
+ gem 'listen', '~> 1.2.2'
20
+ gem 'rb-fchange', '~> 0.0.6', :require => false
21
+ gem 'rb-fsevent', '~> 0.9.3', :require => false
22
+ gem 'rb-inotify', '~> 0.9.0', :require => false
23
+
24
+ # notification handling
25
+ gem 'libnotify', '~> 0.8.0', :require => false
26
+ gem 'rb-notifu', '~> 0.0.4', :require => false
27
+ gem 'terminal-notifier-guard', '~> 1.5.3', :require => false
28
+ end
29
+
30
+ group :metrics do
31
+ gem 'coveralls', '~> 0.6.7'
32
+ gem 'flay', '~> 2.3.0'
33
+ gem 'flog', '~> 4.1.0'
34
+ gem 'reek', '~> 1.3.1', :git => 'https://github.com/troessner/reek.git'
35
+ gem 'simplecov', '~> 0.7.1'
36
+ gem 'yardstick', '~> 0.9.6'
37
+
38
+ platforms :ruby_19, :ruby_20 do
39
+ gem 'mutant', '~> 0.3.0.beta12'
40
+ gem 'yard-spellcheck', '~> 0.1.5'
41
+ end
42
+
43
+ platforms :rbx do
44
+ gem 'pelusa', '~> 0.2.2'
45
+ end
46
+ end
47
+
48
+ group :benchmarks do
49
+ gem 'rbench', '~> 0.2.3'
50
+ end
51
+
52
+ platform :jruby do
53
+ group :jruby do
54
+ gem 'jruby-openssl', '~> 0.8.5'
55
+ end
56
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ guard :bundler do
4
+ watch('Gemfile')
5
+ end
6
+
7
+ guard :rspec do
8
+ # run all specs if the spec_helper or supporting files files are modified
9
+ watch('spec/spec_helper.rb') { 'spec' }
10
+ watch(%r{\Aspec/(?:lib|support|shared)/.+\.rb\z}) { 'spec' }
11
+
12
+ # run unit specs if associated lib code is modified
13
+ watch(%r{\Alib/(.+)\.rb\z}) { |m| Dir["spec/unit/#{m[1]}"] }
14
+ watch("lib/#{File.basename(File.expand_path('../', __FILE__))}.rb") { 'spec' }
15
+
16
+ # run a spec if it is modified
17
+ watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z})
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2007 Guy van den Berg
2
+ Copyright (c) 2011 DataMapper development team
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ vanguard
2
+ ========
3
+
4
+ This library provides external validations for any Ruby class.
5
+
6
+ History:
7
+ --------
8
+
9
+ It originates from [emmanuels aequitas repository](https://github.com/emmanuel/vanguard)
10
+ with the following changes:
11
+
12
+ * Only support for external validators
13
+ * Use composable algebra for internals
14
+ * Will allow serialization to javascript for client side validation (not implemented)
15
+ * No contextual validators anymore (use additional external validators)
16
+ * 100% code rewrite
17
+ * Use [equalizer](https://github.com/dkubb/equalizer) and [adamantium](https://github.com/dkubb/adamantium) where possible.
18
+
19
+ ## Specifying Validations
20
+
21
+ ```ruby
22
+ require 'vanguard'
23
+
24
+ class ProgrammingLanguage
25
+ attr_reader :name
26
+
27
+ def initialize(name)
28
+ @name = name
29
+ end
30
+ end
31
+
32
+ VALIDATOR = Vanguard::Validator.build do
33
+ validates_presence_of :name
34
+ end
35
+
36
+ ruby = ProgrammingLanguage.new('ruby')
37
+
38
+ result = VALIDATOR.call(ruby)
39
+ result.valid? # => true
40
+ result.violations # => #<Set: {}>
41
+
42
+ other = ProgrammingLanguage.new('')
43
+
44
+ result = VALIDATOR.call(other)
45
+ result.valid? # => false
46
+ result.violations # => #<Set: {<Vanguard:::Violation ....>}>
47
+ ```
48
+
49
+ See `Vanguard::Macros` to learn about the complete collection of validation rules available.
50
+
51
+ ## Credits
52
+
53
+ * Markus Schirp [mbj](https://github.com/mbj)
54
+ * Emmanuel Gomez [emmanuel](https://github.com/emmanuel)
55
+
56
+ ## Working with Validation Errors
57
+
58
+ If an instance fails one or more validation rules, `Vanguard::Violation` instances
59
+ will populate the `Vanguard::ViolationSet` object that is available through
60
+ the `Vanguard::Result#violations` method.
61
+
62
+ Vanguard currently has no support for generating human readable violation messages!
63
+
64
+ For example:
65
+
66
+ ```ruby
67
+ result = YOUR_VALIDATOR.call(Account.new(:name => "Jose"))
68
+ if result.valid?
69
+ # my_account is valid and can be saved
70
+ else
71
+ result.violations.each do |e|
72
+ do_something_with(e)
73
+ end
74
+ end
75
+ ```
76
+
77
+ ##Contextual Validation
78
+
79
+ Vanguard does not provide a means of grouping your validations into
80
+ contexts. Define a validator per context for this.
@@ -0,0 +1,2 @@
1
+ require 'devtools'
2
+ Devtools.init_rake_tasks
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ * Add 100% mutation covered specs
2
+ * Bring this lib to the same quality level like other dm2 stuff
3
+ * Extract boolean logic from veritas into axiom-logic and use instead of Matchers.
@@ -0,0 +1,3 @@
1
+ ---
2
+ threshold: 20
3
+ total_score: 257
@@ -0,0 +1,2 @@
1
+ ---
2
+ threshold: 10.4
@@ -0,0 +1,3 @@
1
+ ---
2
+ name: vanguard
3
+ namespace: Vanguard
@@ -0,0 +1,99 @@
1
+ ---
2
+ UncommunicativeParameterName:
3
+ accept: []
4
+ exclude: []
5
+ enabled: false # Todo enable
6
+ reject:
7
+ - !ruby/regexp /^.$/
8
+ - !ruby/regexp /[0-9]$/
9
+ - !ruby/regexp /[A-Z]/
10
+ TooManyMethods:
11
+ max_methods: 10 # Todo decrease to 10
12
+ exclude: []
13
+ enabled: true
14
+ max_instance_variables: 3
15
+ UncommunicativeMethodName:
16
+ accept: []
17
+ exclude: []
18
+ enabled: true
19
+ reject:
20
+ - !ruby/regexp /^[a-z]$/
21
+ - !ruby/regexp /[0-9]$/
22
+ - !ruby/regexp /[A-Z]/
23
+ LongParameterList:
24
+ max_params: 2
25
+ exclude:
26
+ - Vanguard::Builder::Nullary#initialize # 3 Parameters, fix this!
27
+ enabled: true
28
+ overrides: {}
29
+ FeatureEnvy:
30
+ exclude: []
31
+ enabled: true
32
+ ClassVariable:
33
+ exclude: []
34
+ enabled: true
35
+ BooleanParameter:
36
+ exclude: []
37
+ enabled: true
38
+ IrresponsibleModule:
39
+ exclude: []
40
+ enabled: false # Fix false positives and reenable
41
+ UncommunicativeModuleName:
42
+ accept: []
43
+ exclude: []
44
+ enabled: true
45
+ reject:
46
+ - !ruby/regexp /^.$/
47
+ - !ruby/regexp /[0-9]$/
48
+ NestedIterators:
49
+ ignore_iterators: []
50
+ exclude: []
51
+ enabled: true
52
+ max_allowed_nesting: 1
53
+ TooManyStatements:
54
+ max_statements: 8
55
+ exclude: []
56
+ enabled: true
57
+ DuplicateMethodCall:
58
+ allow_calls: []
59
+ exclude:
60
+ - Vanguard#self.blank
61
+ enabled: true
62
+ max_calls: 1
63
+ UtilityFunction:
64
+ max_helper_calls: 0
65
+ exclude:
66
+ - Vanguard::Validator#add # TODO fix false positive
67
+ - Vanguard::Matcher::Nullary#matches?
68
+ enabled: true
69
+ Attribute:
70
+ exclude: []
71
+ enabled: false
72
+ UncommunicativeVariableName:
73
+ accept: [] # TODO remove them
74
+ exclude: []
75
+ enabled: false
76
+ reject:
77
+ - !ruby/regexp /^.$/
78
+ - !ruby/regexp /[0-9]$/
79
+ - !ruby/regexp /[A-Z]/
80
+ RepeatedConditional:
81
+ exclude: []
82
+ enabled: true
83
+ max_ifs: 1
84
+ DataClump:
85
+ exclude: []
86
+ enabled: true
87
+ max_copies: 2
88
+ min_clump_size: 2
89
+ ControlParameter:
90
+ exclude: []
91
+ enabled: true
92
+ LongYieldList:
93
+ max_params: 1
94
+ exclude: []
95
+ enabled: true
96
+ NilCheck:
97
+ enabled: true
98
+ exclude:
99
+ - Vanguard#self.blank?
@@ -0,0 +1,26 @@
1
+ ---
2
+ AbcMetricMethodCheck:
3
+ score: 12.2
4
+ AssignmentInConditionalCheck: {}
5
+ CaseMissingElseCheck: {}
6
+ ClassLineCountCheck:
7
+ line_count: 324
8
+ ClassNameCheck:
9
+ pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/
10
+ ClassVariableCheck: {}
11
+ CyclomaticComplexityBlockCheck:
12
+ complexity: 2
13
+ CyclomaticComplexityMethodCheck:
14
+ complexity: 4
15
+ EmptyRescueBodyCheck: {}
16
+ ForLoopCheck: {}
17
+ MethodLineCountCheck:
18
+ line_count: 8 # TODO decrease to 9
19
+ MethodNameCheck:
20
+ pattern: !ruby/regexp /\A(?:[a-z\d](?:_?[a-z\d])+[?!=]?|\[\]=?|==|<=>|<<|[+*&|-])\z/
21
+ ModuleLineCountCheck:
22
+ line_count: 376
23
+ ModuleNameCheck:
24
+ pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/
25
+ ParameterNumberCheck:
26
+ parameter_count: 4 # TODO decrease to 2
@@ -0,0 +1,2 @@
1
+ ---
2
+ threshold: 100.0
@@ -0,0 +1,85 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'date'
4
+ require 'bigdecimal'
5
+ require 'bigdecimal/util'
6
+ require 'forwardable'
7
+ require 'adamantium'
8
+ require 'equalizer'
9
+ require 'abstract_type'
10
+ require 'ice_nine'
11
+ require 'ice_nine/core_ext/object'
12
+
13
+ # Library namespace
14
+ module Vanguard
15
+
16
+ # Define constant singleton class
17
+ #
18
+ # @param [Class] klass
19
+ # @param [Symbol] name
20
+ #
21
+ # @return [self]
22
+ #
23
+ # @api private
24
+ #
25
+ def self.singleton_constant(klass, name, &block)
26
+ subclass = Class.new(klass) do
27
+ # Return inspection string
28
+ #
29
+ # @return [String]
30
+ #
31
+ # @api private
32
+ #
33
+ def inspect
34
+ klass = self.class
35
+ "#{klass.superclass.name}::#{klass.name}".freeze
36
+ end
37
+ end
38
+ subclass.class_eval(&block)
39
+ klass.const_set(name, subclass.new)
40
+ self
41
+ end
42
+
43
+ end
44
+
45
+ require 'vanguard/support/blank'
46
+ require 'vanguard/evaluator'
47
+ require 'vanguard/dsl'
48
+ require 'vanguard/dsl/evaluator'
49
+ require 'vanguard/instance_methods'
50
+ require 'vanguard/validator'
51
+ require 'vanguard/validator/builder'
52
+ require 'vanguard/builder'
53
+ require 'vanguard/builder/nullary'
54
+ require 'vanguard/matcher'
55
+ require 'vanguard/matcher/unary'
56
+ require 'vanguard/matcher/unary/attribute'
57
+ require 'vanguard/matcher/unary/not'
58
+ require 'vanguard/matcher/binary'
59
+ require 'vanguard/matcher/binary/and'
60
+ require 'vanguard/matcher/binary/or'
61
+ require 'vanguard/matcher/binary/xor'
62
+ require 'vanguard/matcher/nullary'
63
+ require 'vanguard/matcher/nullary/proc'
64
+ require 'vanguard/matcher/nullary/format'
65
+ require 'vanguard/matcher/nullary/primitive'
66
+ require 'vanguard/matcher/nullary/inclusion'
67
+ require 'vanguard/matcher/nullary/equality'
68
+ require 'vanguard/matcher/nullary/identity'
69
+ require 'vanguard/matcher/nullary/greater_than'
70
+ require 'vanguard/matcher/nullary/less_than'
71
+ require 'vanguard/matcher/nullary/value'
72
+ require 'vanguard/rule'
73
+ require 'vanguard/rule/guard'
74
+ require 'vanguard/rule/nullary'
75
+ require 'vanguard/rule/nullary/confirmation'
76
+ require 'vanguard/rule/nullary/attribute'
77
+ require 'vanguard/rule/nullary/attribute/format'
78
+ require 'vanguard/rule/nullary/attribute/length'
79
+ require 'vanguard/rule/nullary/attribute/absence'
80
+ require 'vanguard/rule/nullary/attribute/presence'
81
+ require 'vanguard/rule/nullary/attribute/primitive'
82
+ require 'vanguard/rule/nullary/attribute/inclusion'
83
+ require 'vanguard/rule/nullary/attribute/predicate'
84
+ require 'vanguard/violation'
85
+ require 'vanguard/result'