validate_args 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fecd8695811758e2dbd45d7814e361df8cb1d4e
4
+ data.tar.gz: 46ec88c290e91eae7d644a385a47ed95da384371
5
+ SHA512:
6
+ metadata.gz: 1bbfd63e9bd84ee890e52ca6d89b5b37fa73d83e9cb1262a6d79b62fe9196bac1fa3574b3cf724d2c4110a472aa04ec5404814440fb11c2b17c4e0bccf67e8fd
7
+ data.tar.gz: 8ac4353e80ce1bff1aff1621ce4e19a864a35f66995c235446383d2028bdb3a945eb9b619e15e67c900edd7bc606ffee488c866ea60ec3abe4be8ad6d76c5a36
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.byebug_history
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'byebug'
4
+ gem 'benchmark-ips'
5
+ gem 'rubype'
6
+ # Specify your gem's dependencies in validate_args.gemspec
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nobuhiro Nikushi
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,136 @@
1
+ # ValidateArgs
2
+
3
+ ValidateArgs allows you to specify validations for method arguments, using `validate_args` to supply validation rules of a hash or array. Validations you declared would be called automatically before method calls.
4
+
5
+ The syntax of validation rules is the same as shyouhei's [data-validator](https://github.com/shyouhei/data-validator) because ValidateArgs dispatches rules and validations to data-validator internally.
6
+
7
+ ## Example
8
+
9
+ ### Basic usages
10
+
11
+ ```ruby
12
+ require 'validate_args'
13
+
14
+ class MyClass
15
+
16
+ extend ValidateArgs
17
+
18
+ validate_args :do_something, 'foo' => Numeric
19
+ def do_something(params)
20
+ return params
21
+ end
22
+
23
+ end
24
+
25
+ # pass validation
26
+ MyClass.new.do_something('foo' => 42)
27
+ # => {"foo"=>42}
28
+
29
+ # pass
30
+ MyClass.new.do_something('foo' => 0, 'baz' => 42, 'qux' => 100)
31
+ # => {"foo"=>0, "baz"=>42, "qux"=>100}
32
+
33
+ # validation failure
34
+ MyClass.new.do_something('foo' => 'string')
35
+ # => ValidateArgs::ArgumentTypeError: for MyClass#do_something's 1st argument is invalid => foo:type mismatch
36
+
37
+ # validation failure
38
+ MyClass.new.do_something('bar' => 1)
39
+ # => ValidateArgs::ArgumentTypeError: for MyClass#do_something's 1st argument is invalid => ["foo"] missing
40
+ ```
41
+
42
+ ### Complex cases
43
+
44
+ ```ruby
45
+ require 'validate_args'
46
+
47
+ class MyClass
48
+
49
+ extend ValidateArgs
50
+
51
+ validate_args :do_something, {
52
+ 'foo' => { isa: Numeric, default: 99 },
53
+ 'bar' => { isa: Numeric, default: lambda {|validator, rule, args| args['foo'] + 1 } },
54
+ 'baz' => { isa: String, optional: true }
55
+ }
56
+ def do_something(params)
57
+ return params
58
+ end
59
+ end
60
+
61
+ # pass
62
+ MyClass.new.do_something('foo' => 1, 'bar' => 2)
63
+ # => {"foo"=>1, "bar"=>2}
64
+
65
+
66
+ # pass (`bar` is calculated)
67
+ MyClass.new.do_something('foo' => 5)
68
+ # => {"foo"=>5, "bar"=>6}
69
+ ```
70
+
71
+ ### non hash arguments
72
+
73
+ ```ruby
74
+ require 'validate_args'
75
+
76
+ class MyClass
77
+
78
+ extend ValidateArgs
79
+
80
+ validate_args :sum, [Numeric, Numeric]
81
+ def sum(x, y)
82
+ x + y
83
+ end
84
+
85
+ end
86
+
87
+ # pass
88
+ MyClass.new.sum(1, 2)
89
+ # => 3
90
+
91
+ # validation failure
92
+ MyClass.new.sum(1, 'a')
93
+ # => ValidateArgs::ArgumentTypeError: for MyClass#sum's 2nd argument is invalid => type mismatch
94
+ ```
95
+
96
+ ## Installation
97
+
98
+ Add this line to your application's Gemfile:
99
+
100
+ ```ruby
101
+ gem 'validate_args'
102
+ ```
103
+
104
+ And then execute:
105
+
106
+ $ bundle
107
+
108
+ Or install it yourself as:
109
+
110
+ $ gem install validate_args
111
+
112
+ ## Similar gems
113
+
114
+ * [data-validator](https://github.com/shyouhei/data-validator)
115
+ * ValidateArgs just wraps it to provide `validate_args` class method as DSL.
116
+ * [Rubype](https://github.com/gogotanaka/Rubype)
117
+ * Rubype is super cool gem!
118
+ * Rubype does not validate values of given hashes because it focuses on checking classes of given args.
119
+ * Rubype is faster than ValidateArgs
120
+ * Rubype can check return value, but ValidateArgs does not for now. (patches are welcome though!)
121
+
122
+ ## Development
123
+
124
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
125
+
126
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+
128
+ ## Contributing
129
+
130
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validate_args.
131
+
132
+
133
+ ## License
134
+
135
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
136
+
data/Rakefile ADDED
@@ -0,0 +1,161 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ # Benchmark
9
+ #-----------------------------------------------
10
+ desc "Compare with pure ruby and rubype"
11
+ task :benchmark do
12
+ require "benchmark/ips"
13
+ require "validate_args"
14
+ require "validate_args/version"
15
+ require 'rubype'
16
+ require 'rubype/version'
17
+
18
+ puts "ruby version: #{RUBY_VERSION}"
19
+ class PureClass
20
+ def sum(x, y)
21
+ x + y
22
+ end
23
+ end
24
+ pure_instance = PureClass.new
25
+
26
+ puts "validate_args version: #{ValidateArgs::VERSION}"
27
+ class ValidateArgsClass
28
+ extend ValidateArgs
29
+ validate_args :sum, [Numeric, Numeric]
30
+ def sum(x, y)
31
+ x + y
32
+ end
33
+ end
34
+ validate_args_instance = ValidateArgsClass.new
35
+
36
+ puts "rubype version: #{Rubype::VERSION}"
37
+ class RubypeClass
38
+ def sum(x, y)
39
+ x + y
40
+ end
41
+ typesig :sum, [Numeric, Numeric] => Numeric
42
+ end
43
+ rubype_instance = RubypeClass.new
44
+
45
+
46
+ Benchmark.ips do |x|
47
+ x.report('Pure Ruby') { pure_instance.sum(4, 2) }
48
+ x.report('ValidateArgs') { validate_args_instance.sum(4, 2) }
49
+ x.report('Rubype') { rubype_instance.sum(4, 2) }
50
+
51
+ x.compare!
52
+ end
53
+ end
54
+ task bm: :benchmark
55
+
56
+ # Benchmark2 hash validation
57
+ #-----------------------------------------------
58
+ desc "Compare with pure ruby and rubype for hash validation"
59
+ task :benchmark2 do
60
+ require "benchmark/ips"
61
+ require "validate_args"
62
+ require "validate_args/version"
63
+ require 'rubype'
64
+ require 'rubype/version'
65
+
66
+ puts "ruby version: #{RUBY_VERSION}"
67
+ class PureClass
68
+ def do_something(params = {})
69
+ params
70
+ end
71
+ end
72
+ pure_instance = PureClass.new
73
+
74
+ puts "validate_args version: #{ValidateArgs::VERSION}"
75
+ class ValidateArgsClass
76
+ extend ValidateArgs
77
+ validate_args :do_something, 'uri' => String
78
+ def do_something(params = {})
79
+ params
80
+ end
81
+ end
82
+ validate_args_instance = ValidateArgsClass.new
83
+
84
+ puts "rubype version: #{Rubype::VERSION}"
85
+ class RubypeClass
86
+ def do_something(params = {})
87
+ params
88
+ end
89
+ typesig :do_something, [Hash] => Hash
90
+ end
91
+ rubype_instance = RubypeClass.new
92
+
93
+ Benchmark.ips do |x|
94
+ x.report('Pure Ruby') { pure_instance.do_something('uri' => 'example.com') }
95
+ x.report('ValidateArgs') { validate_args_instance.do_something('uri' => 'example.com') }
96
+ x.report('Rubype') { rubype_instance.do_something('uri' => 'example.com') }
97
+
98
+ x.compare!
99
+ end
100
+ end
101
+ task bm2: :benchmark2
102
+
103
+ # Benchmark3 pure data-validator vs validate_args
104
+ #-----------------------------------------------
105
+ desc "Compare with pure data-validator and validate_args"
106
+ task :benchmark3 do
107
+ require "benchmark/ips"
108
+ require "validate_args"
109
+ require "validate_args/version"
110
+ require 'data/validator'
111
+ require 'data/validator/version'
112
+
113
+ puts "data validator version: #{Data::Validator::VERSION}"
114
+ class DataValidatorClass
115
+ @@validator = Data::Validator.new('uri' => String)
116
+ def do_something(params = {})
117
+ @@validator.validate params
118
+ end
119
+ end
120
+ data_validator_instance = DataValidatorClass.new
121
+
122
+ puts "validate_args version: #{ValidateArgs::VERSION}"
123
+ class ValidateArgsClass
124
+ extend ValidateArgs
125
+ validate_args :do_something, 'uri' => String
126
+ def do_something(params = {})
127
+ params
128
+ end
129
+ end
130
+ validate_args_instance = ValidateArgsClass.new
131
+
132
+ Benchmark.ips do |x|
133
+ x.report('DataValidator') { data_validator_instance.do_something('uri' => 'example.com') }
134
+ x.report('ValidateArgs') { validate_args_instance.do_something('uri' => 'example.com') }
135
+
136
+ x.compare!
137
+ end
138
+ end
139
+ task bm3: :benchmark3
140
+
141
+ desc "Memory growth test"
142
+ task :mem_benchmark do
143
+ require "validate_args"
144
+ require "validate_args/version"
145
+ N = (ENV['N'] || 100_000).to_i
146
+
147
+ puts "validate_args version: #{ValidateArgs::VERSION}"
148
+ class Foo
149
+ extend ValidateArgs
150
+ validate_args :do_something, [Numeric, 'uri' => String]
151
+ def do_something(i, params)
152
+ end
153
+ end
154
+ N.times do |i|
155
+ Foo.new.do_something(1, 'uri' => 'example.com')
156
+ if (i%10_000) == 0
157
+ GC.start
158
+ p `ps -o rss= -p #{Process.pid}`.to_i
159
+ end
160
+ end
161
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "validate_args"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ module ValidateArgs
2
+ require "validate_args/version"
3
+ require "validate_args/error_printer"
4
+ require "validate_args/validators"
5
+
6
+ class ArgumentTypeError < StandardError; end
7
+
8
+ private
9
+
10
+ def __proxy__
11
+ @__proxy__ ||= Module.new.tap {|proxy| prepend proxy }
12
+ end
13
+
14
+ # Define DSL
15
+ def validate_args(meth, rules)
16
+ Validators.register_validator(self, meth, rules, __proxy__)
17
+ self
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require 'validate_args/utils'
2
+
3
+ module ValidateArgs
4
+ class ErrorPrinter
5
+ def initialize(owner, meth, errors)
6
+ @owner = owner
7
+ @meth = meth
8
+ @errors = errors
9
+ end
10
+
11
+ def to_s
12
+ @errors.zip(0..@errors.size).reject{|message, i| message.nil? }.map do |message, i|
13
+ "for #{@owner.class}##{@meth}'s #{Utils.ordinalize(i+1)} argument is invalid => #{message}"
14
+ end.join("\n")
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # Borrowed from ActiveSupport::Inflector
2
+ module ValidateArgs
3
+ module Utils
4
+ class << self
5
+ def ordinalize(number)
6
+ if (11..13).include?(number % 100)
7
+ "#{number}th"
8
+ else
9
+ case number % 10
10
+ when 1 then "#{number}st"
11
+ when 2 then "#{number}nd"
12
+ when 3 then "#{number}rd"
13
+ else "#{number}th"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ require 'data/validator'
2
+ require 'validate_args'
3
+
4
+ module ValidateArgs
5
+ module Validators
6
+ @@validators = Hash.new({})
7
+
8
+ class << self
9
+
10
+ def register_validator(owner, meth, rules, __proxy__)
11
+ rules = [rules] unless rules.is_a? Array
12
+ @@validators[owner][meth] = rules.map {|rule| Data::Validator.new(rule) }
13
+ __proxy__.module_eval do
14
+ define_method(meth) do |*args|
15
+ ::ValidateArgs::Validators.validate!(self, meth, args)
16
+ super(*args)
17
+ end
18
+ end
19
+ end
20
+
21
+ def get_validator(owner, meth)
22
+ @@validators[owner][meth]
23
+ end
24
+
25
+ def validate!(owner, meth, args)
26
+ rules = get_validator(owner, meth)
27
+ errors = []
28
+ rules.zip(args).each_with_index do |(rule, arg), i|
29
+ if rule and arg
30
+ begin
31
+ rule.validate(arg)
32
+ rescue ::Data::Validator::Error => e
33
+ errors[i] = e.message
34
+ end
35
+ end
36
+ end
37
+ raise ArgumentTypeError, ErrorPrinter.new(owner, meth, errors).to_s if !errors.empty?
38
+ true
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module ValidateArgs
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'validate_args/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "validate_args"
8
+ spec.version = ValidateArgs::VERSION
9
+ spec.authors = ["Nobuhiro Nikushi"]
10
+ spec.email = ["deneb.ge@gmail.com"]
11
+
12
+ spec.summary = %q{Validation system for method arguments}
13
+ spec.description = %q{It allows you to specify validations for method arguments, using `validate_args` to supply validation rules of a hash or array.}
14
+ spec.homepage = "https://github.com/niku4i/validate_args"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ #end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "data-validator"
31
+ spec.add_development_dependency "bundler", "~> 1.11"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validate_args
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nobuhiro Nikushi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: data-validator
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: It allows you to specify validations for method arguments, using `validate_args`
70
+ to supply validation rules of a hash or array.
71
+ email:
72
+ - deneb.ge@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/validate_args.rb
87
+ - lib/validate_args/error_printer.rb
88
+ - lib/validate_args/utils.rb
89
+ - lib/validate_args/validators.rb
90
+ - lib/validate_args/version.rb
91
+ - validate_args.gemspec
92
+ homepage: https://github.com/niku4i/validate_args
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Validation system for method arguments
116
+ test_files: []