arity 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +6 -0
- data/arity.gemspec +23 -0
- data/bin/console +9 -0
- data/bin/setup +8 -0
- data/lib/arity.rb +3 -0
- data/lib/arity/errors.rb +4 -0
- data/lib/arity/function.rb +106 -0
- data/lib/arity/version.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 351129da5280429ffcbd59db97d3e63e5fefa318
|
4
|
+
data.tar.gz: 2c6909a6ef229c5fcc696cf25ceef7e822cf3f07
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7f36e9e56e66ea4f7e9d124ce1ba8fcabc238358a9ed29d87f4e8e1f8a56ddc34d162f971a46e9d21f571f92c76cc1abe63ac9ab8342284d58d7a0bc767ea92e
|
7
|
+
data.tar.gz: 3959f8ceb2c80f0bb9cf960b41668678dfea2c84d5e7c08a352d7a146d276cce96d6622529b584a3d48bc821c78a32404c9e2c6265ed698cda6bab1fad8e4c83
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 David Feldman
|
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,45 @@
|
|
1
|
+
# Arity
|
2
|
+
|
3
|
+
Logic to determine when Ruby functions can be called.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add `gem 'arity'` to your Gemfile.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
```ruby
|
11
|
+
function = Arity::Function.make(some_callable)
|
12
|
+
# note the use of .make versus .new
|
13
|
+
# some_callable should respond to :call
|
14
|
+
# otherwise, .make will raise an Arity::NotCallableError
|
15
|
+
# to avoid raising, pass {silent: true} to .make and it will return nil instead
|
16
|
+
```
|
17
|
+
```ruby
|
18
|
+
function.arity
|
19
|
+
# delegates to Ruby's built-in arity logic
|
20
|
+
```
|
21
|
+
```ruby
|
22
|
+
function.takes?(arg_count: 3, keywords: [:things])
|
23
|
+
# determines if the supplied method parameter structure is valid.
|
24
|
+
```
|
25
|
+
```ruby
|
26
|
+
function.runnable?('thing-1','thing-2','thing-3', things: 3)
|
27
|
+
# computes the parameter structure and delegates to .takes?
|
28
|
+
```
|
29
|
+
```ruby
|
30
|
+
function.run('thing-1','thing-2','thing-3', things: 3)
|
31
|
+
# calls .call on the callable with the supplied parameters
|
32
|
+
```
|
33
|
+
|
34
|
+
## Development
|
35
|
+
|
36
|
+
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.
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fledman/arity.
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
45
|
+
|
data/Rakefile
ADDED
data/arity.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'arity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "arity"
|
8
|
+
spec.version = Arity::VERSION
|
9
|
+
spec.authors = ["David Feldman"]
|
10
|
+
spec.email = ["dbfeldman@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "logic regarding function arity"
|
13
|
+
spec.description = "logic to determine when Ruby functions can be called"
|
14
|
+
spec.homepage = "https://github.com/fledman/arity"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
23
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/arity.rb
ADDED
data/lib/arity/errors.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
module Arity
|
2
|
+
class Function
|
3
|
+
|
4
|
+
attr_reader :callable
|
5
|
+
|
6
|
+
private_class_method :new
|
7
|
+
|
8
|
+
def self.make(fn, silent: false)
|
9
|
+
return new(fn) if fn.respond_to?(:call)
|
10
|
+
return if silent
|
11
|
+
raise NotCallableError, "function is not callable: #{fn.inspect}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(fn)
|
15
|
+
@callable = unwrap_function(fn)
|
16
|
+
end
|
17
|
+
|
18
|
+
def arity
|
19
|
+
callable.arity
|
20
|
+
end
|
21
|
+
|
22
|
+
def signature
|
23
|
+
@signature ||= compute_signature
|
24
|
+
end
|
25
|
+
|
26
|
+
def takes?(arg_count:, keywords:)
|
27
|
+
bad_param('arg_count', 'an int', arg_count) if !arg_count.is_a?(Integer)
|
28
|
+
bad_param('keywords', 'an array', keywords) if !keywords.is_a?(Array)
|
29
|
+
|
30
|
+
return false if arg_count < signature[:min_args]
|
31
|
+
|
32
|
+
if signature[:max_args] > -1
|
33
|
+
return false if arg_count > signature[:max_args]
|
34
|
+
end
|
35
|
+
|
36
|
+
missing = signature[:required_keys] - keywords
|
37
|
+
return false if !missing.empty?
|
38
|
+
|
39
|
+
return true if signature[:any_key]
|
40
|
+
|
41
|
+
extra_keys = keywords - signature[:required_keys] \
|
42
|
+
- signature[:optional_keys]
|
43
|
+
extra_keys.empty?
|
44
|
+
end
|
45
|
+
|
46
|
+
def runnable?(*args, **opts)
|
47
|
+
takes?(arg_count: args.size, keywords: opts.keys)
|
48
|
+
end
|
49
|
+
|
50
|
+
def run(*args, **opts)
|
51
|
+
args.push(opts) unless opts.empty?
|
52
|
+
callable.call(*args)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def unwrap_function(fn)
|
58
|
+
fn.respond_to?(:arity) ? fn : fn.method(:call)
|
59
|
+
end
|
60
|
+
|
61
|
+
def unknown_parameter_type!(type, name)
|
62
|
+
msg = "do not known how to parse the function signature"
|
63
|
+
msg += " for type #{type.inspect} with parameter #{name.inspect}"
|
64
|
+
msg += "; please file an issue at https://www.github.com/fledman/arity"
|
65
|
+
raise UnknownParameterTypeError, msg
|
66
|
+
end
|
67
|
+
|
68
|
+
def bad_param(name, type, value)
|
69
|
+
raise ArgumentError, "#{name} must be #{type}, got: #{value.inspect}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def compute_signature
|
73
|
+
splat = false
|
74
|
+
{ min_args: 0,
|
75
|
+
max_args: 0,
|
76
|
+
required_keys: [],
|
77
|
+
optional_keys: [],
|
78
|
+
any_key: false
|
79
|
+
}.tap do |signature|
|
80
|
+
callable.parameters.each do |type,name|
|
81
|
+
case type
|
82
|
+
when :req
|
83
|
+
signature[:min_args] += 1
|
84
|
+
signature[:max_args] += 1
|
85
|
+
when :opt
|
86
|
+
signature[:max_args] += 1
|
87
|
+
when :rest
|
88
|
+
splat = true
|
89
|
+
when :key
|
90
|
+
signature[:optional_keys] << name
|
91
|
+
when :keyreq
|
92
|
+
signature[:required_keys] << name
|
93
|
+
when :keyrest
|
94
|
+
signature[:any_key] = true
|
95
|
+
else
|
96
|
+
unknown_parameter_type!(type, name)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
signature[:max_args] = -1 if splat
|
100
|
+
signature[:required_keys].freeze
|
101
|
+
signature[:optional_keys].freeze
|
102
|
+
end.freeze
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Feldman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.10'
|
55
|
+
description: logic to determine when Ruby functions can be called
|
56
|
+
email:
|
57
|
+
- dbfeldman@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- arity.gemspec
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/arity.rb
|
72
|
+
- lib/arity/errors.rb
|
73
|
+
- lib/arity/function.rb
|
74
|
+
- lib/arity/version.rb
|
75
|
+
homepage: https://github.com/fledman/arity
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.8
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: logic regarding function arity
|
99
|
+
test_files: []
|