argspec 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: e489a3f3ed71350a6e16435cbf19efda2a9bac84
4
+ data.tar.gz: 2af97d57c3ea415a720d0db32c3ba22d0d7ae7c8
5
+ SHA512:
6
+ metadata.gz: 3dda7ebf91f61ad7c310950f0788f0941f175019e46e470417c286756c27f1f932db0cda8443817af0bb7b93dc81a371562c83b1fa9e320c7ec1766374b3f47f
7
+ data.tar.gz: 44e82ea8b0b64566e51540615024e20b38b1f7225ed6eee4d57de71c642f8f6f9d2dfe0a1d0e2cbcef9fae5ee4979c0d0fd90690df4351969c97a1b88fcf37c2
data/.gitignore ADDED
@@ -0,0 +1,44 @@
1
+ # Packages #
2
+ ############
3
+ *.7z
4
+ *.dmg
5
+ *.gz
6
+ *.iso
7
+ *.jar
8
+ *.rar
9
+ *.tar
10
+ *.zip
11
+
12
+ # Logs #
13
+ ########
14
+ *.log
15
+
16
+ # Databases #
17
+ #############
18
+ *.sql
19
+ *.sqlite
20
+
21
+ # OS Files #
22
+ ############
23
+ .DS_Store
24
+ .Trashes
25
+ ehthumbs.db
26
+ Icon?
27
+ Thumbs.db
28
+
29
+ # Vagrant #
30
+ ###########
31
+ .vagrant
32
+
33
+ # Ruby Files #
34
+ ##############
35
+ /.bundle/
36
+ /.yardoc
37
+ /Gemfile.lock
38
+ /_yardoc/
39
+ /coverage/
40
+ /doc/
41
+ /pkg/
42
+ /spec/reports/
43
+ /tmp/
44
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format doc
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Argument Specification
2
+
3
+ Using argspec, you can easily validate that arguments match your required specification.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'argspec'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install argspec
20
+
21
+ ## Usage
22
+
23
+ Let's say you have the following ```Person``` class:
24
+
25
+ ```ruby
26
+ class Person
27
+ attr_reader :name, :gender, :birthdate
28
+
29
+ def initialize(name, gender, birthdate)
30
+ @name = name
31
+ @gender = gender
32
+ @birthdate = birthdate
33
+ end
34
+ end
35
+ ```
36
+
37
+ You'd like to validate those three arguments match the expected type. You might come up with something like this:
38
+
39
+ ```ruby
40
+ class Person
41
+ attr_reader :name, :gender, :birthdate
42
+
43
+ def initialize(name, gender, birthdate)
44
+ raise ArgumentError, "The name must be a string and must not be nil." if !name.is_a?(String) || name.empty?
45
+ raise ArgumentError, "The gender must be a symbol." unless gender.is_a?(Symbol)
46
+ raise ArgumentError, "The birthdate must be a date." unless birthdate.is_a?(Date)
47
+
48
+ @name = name
49
+ @gender = gender
50
+ @birthdate = birthdate
51
+ end
52
+ end
53
+ ```
54
+
55
+ That will work fine, but it looks a little messy. Let's use argspec instead:
56
+
57
+ ```ruby
58
+ class Person
59
+ include ArgumentSpecification::DSL
60
+
61
+ attr_reader :name, :gender, :birthdate
62
+
63
+ def initialize(name, gender, birthdate)
64
+ argument(name).should(:be_a, String)
65
+ argument(gender).should(:be_a, Symbol)
66
+ argument(birthdate).should(:be_a, Date)
67
+
68
+ argument(name).should_not(:be, nil)
69
+ argument(gender).should_not(:be, nil)
70
+ argument(birthdate).should_not(:be, nil)
71
+
72
+ @name = name
73
+ @gender = gender
74
+ @birthdate = birthdate
75
+ end
76
+ end
77
+ ```
78
+
79
+ That's better. It's now really clear what validations you're performing.
80
+
81
+ ## Development
82
+
83
+ 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.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/argspec.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 'argspec/constants'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'argspec'
8
+ spec.version = ArgumentSpecification::VERSION
9
+ spec.authors = ['Nialto Services']
10
+ spec.email = ['support@nialtoservices.co.uk']
11
+
12
+ spec.summary = %q{Argument Validation Checks}
13
+ spec.description = %q{RSpec style(ish) checks for arguments}
14
+ spec.homepage = 'https://rubygems.org/gems/argspec'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ end
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'argspec'
5
+
6
+ require 'irb'
7
+
8
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,130 @@
1
+ module ArgumentSpecification
2
+ class Argument
3
+ attr_reader :object
4
+
5
+ # Create a new argument
6
+ #
7
+ # Arguments:
8
+ # object: (?)
9
+ #
10
+ # Example:
11
+ # >> test = :test
12
+ # >> ArgumentSpecification::Argument.new(test)
13
+ # => #<Argument:0x00000000000000 @object=:test>
14
+ #
15
+ def initialize(object)
16
+ @object = object
17
+ end
18
+
19
+ # Should
20
+ #
21
+ # Arguments:
22
+ # matcher: (Symbol) A method name as a symbol (see the matchers.rb file)
23
+ # args: (Splat)
24
+ #
25
+ # Example:
26
+ # >> argument.should(:be, true)
27
+ # => nil
28
+ #
29
+ # Raises:
30
+ # ArgumentError: If the matcher determines the object does not match
31
+ #
32
+ def should(matcher, *args)
33
+ ensure_matcher(matcher)
34
+
35
+ return if Matchers.send(matcher, self, *args)
36
+
37
+ raise ArgumentError, "'#{prettify_object}' should #{prettify_matcher(matcher)} '#{prettify_args(args)}'"
38
+ end
39
+
40
+ # Should not
41
+ #
42
+ # Arguments:
43
+ # matcher: (Symbol) A method name as a symbol (see the matchers.rb file)
44
+ # args: (Splat)
45
+ #
46
+ # Example:
47
+ # >> argument.should_not(:be, true)
48
+ # => nil
49
+ #
50
+ # Raises:
51
+ # ArgumentError: If the matcher determines the object does match
52
+ #
53
+ def should_not(matcher, *args)
54
+ ensure_matcher(matcher)
55
+
56
+ return if Matchers.send(matcher, self, *args) == false
57
+
58
+ raise ArgumentError, "'#{prettify_object}' should not #{prettify_matcher(matcher)} '#{prettify_args(args)}'"
59
+ end
60
+
61
+ private
62
+ # Ensure a matcher exists
63
+ #
64
+ # Arguments:
65
+ # matcher: (Symbol)
66
+ #
67
+ # Example:
68
+ # >> ensure_matcher(:be)
69
+ # => nil
70
+ #
71
+ # Raises:
72
+ # ArgumentError: If the specified matcher does not exist
73
+ #
74
+ def ensure_matcher(matcher)
75
+ return if Matchers.all.include?(matcher)
76
+
77
+ raise ArgumentError, "The matcher '#{matcher}' does not exist."
78
+ end
79
+
80
+ # Prettify a matcher
81
+ #
82
+ # Arguments:
83
+ # matcher: (String)
84
+ #
85
+ # Example:
86
+ # >> prettify_matcher(:be_a)
87
+ # => "be a"
88
+ #
89
+ def prettify_matcher(matcher)
90
+ matcher.to_s.gsub('_', ' ')
91
+ end
92
+
93
+ # Prettify arguments
94
+ #
95
+ # Arguments:
96
+ # args: (Array)
97
+ #
98
+ # Example:
99
+ # >> prettify_args([:a])
100
+ # => ":a"
101
+ #
102
+ def prettify_args(args)
103
+ pretty_args = args.map do |argument|
104
+ if argument == nil
105
+ next 'nil'
106
+ elsif argument.is_a?(Symbol)
107
+ next ":#{argument}"
108
+ else
109
+ next argument
110
+ end
111
+ end
112
+
113
+ return 'nil' if pretty_args.empty?
114
+ return pretty_args.first if pretty_args.count == 1
115
+
116
+ pretty_args
117
+ end
118
+
119
+ # Prettify the object
120
+ #
121
+ # Example:
122
+ # >> # argument.object = :a
123
+ # >> prettify_object
124
+ # => ":a"
125
+ #
126
+ def prettify_object
127
+ prettify_args([@object])
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,3 @@
1
+ module ArgumentSpecification
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,17 @@
1
+ module ArgumentSpecification
2
+ module DSL
3
+ # Get an argument object
4
+ #
5
+ # Arguments:
6
+ # object: (?)
7
+ #
8
+ # Example:
9
+ # >> test = :test
10
+ # >> argument(test)
11
+ # => #<Argument:0x00000000000000 @object=:test>
12
+ #
13
+ def argument(object)
14
+ ArgumentSpecification::Argument.new(object)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,60 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class << self
4
+ # Get an array of all available matchers
5
+ #
6
+ # Example:
7
+ # >> all
8
+ # => [:be, ...]
9
+ #
10
+ def all
11
+ [:be, :be_a, :include]
12
+ end
13
+
14
+ # The be matcher
15
+ #
16
+ # Arguments:
17
+ # argument: (ArgumentSpecification::Argument)
18
+ # object: (?)
19
+ #
20
+ # Example:
21
+ # >> test = 'Test'
22
+ # >> argument(test).should_not(:be, nil)
23
+ # => nil
24
+ #
25
+ def be(argument, object)
26
+ argument.object == object
27
+ end
28
+
29
+ # The be_a matcher
30
+ #
31
+ # Arguments:
32
+ # argument: (ArgumentSpecification::Argument)
33
+ # object: (?)
34
+ #
35
+ # Example:
36
+ # >> test = :test
37
+ # >> argument(test).should(:be_a, Symbol)
38
+ # => nil
39
+ #
40
+ def be_a(argument, klass)
41
+ argument.object.is_a?(klass)
42
+ end
43
+
44
+ # The include matcher
45
+ #
46
+ # Arguments:
47
+ # argument: (ArgumentSpecification::Argument)
48
+ # object: (?)
49
+ #
50
+ # Example:
51
+ # >> test = [:test]
52
+ # >> argument(test).should(:include, :test)
53
+ # => nil
54
+ #
55
+ def include(argument, object)
56
+ argument.object.include?(object)
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/argspec.rb ADDED
@@ -0,0 +1,20 @@
1
+ module ArgumentSpecification
2
+ class << self
3
+ # Require dependencies
4
+ #
5
+ # Example:
6
+ # >> ArgumentSpecification.require!
7
+ # => nil
8
+ #
9
+ def require!
10
+ require 'argspec/constants'
11
+ require 'argspec/matchers'
12
+ require 'argspec/argument'
13
+ require 'argspec/dsl'
14
+
15
+ nil
16
+ end
17
+ end
18
+ end
19
+
20
+ ArgumentSpecification.require!
@@ -0,0 +1,5 @@
1
+ describe ArgumentSpecification do
2
+ it 'has a version number' do
3
+ expect(ArgumentSpecification::VERSION).not_to be nil
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'argspec'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: argspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nialto Services
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-24 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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
+ description: RSpec style(ish) checks for arguments
56
+ email:
57
+ - support@nialtoservices.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - argspec.gemspec
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/argspec.rb
72
+ - lib/argspec/argument.rb
73
+ - lib/argspec/constants.rb
74
+ - lib/argspec/dsl.rb
75
+ - lib/argspec/matchers.rb
76
+ - spec/argspec_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://rubygems.org/gems/argspec
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Argument Validation Checks
102
+ test_files:
103
+ - spec/argspec_spec.rb
104
+ - spec/spec_helper.rb