proc_eval 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/proc_eval/version.rb +3 -0
- data/lib/proc_eval.rb +62 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: afe87a0835b93b5f3e7e5bc0aa5f277c6b9f85a7bbb8362d54387e77b38bbfed
|
4
|
+
data.tar.gz: 1a2e8a6c7912ba38541210929b4573906b2d34c06a54f4234813cab982b2135d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b38cb7e79e3983eb849f61e17ededbd8476bc3c74a1a55f0e6972ca44f1be0b18d1a0be2c0c0c030b9bd13e3887163b86fd2ee12641e69ca294ed1d2581f52e1
|
7
|
+
data.tar.gz: 95a7d4a56250cfdeb4de92333ce16a586cd00e8b87796bd1f52636355df87dda80afd1ee6b81e8fc958b2d61de38895107e6e38c7aed9518d6615d0b98c8d709
|
data/lib/proc_eval.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module ProcEval
|
2
|
+
|
3
|
+
refine Object do
|
4
|
+
def evaluate(*args, **options)
|
5
|
+
self
|
6
|
+
end
|
7
|
+
end # refine Object
|
8
|
+
|
9
|
+
refine Proc do
|
10
|
+
|
11
|
+
def parameter_type_counts
|
12
|
+
@parameter_type_counts ||= parameters.each_with_object(Hash.new(0)) {|(param_type, _), counts| counts[param_type] += 1 }
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_parameter_type_counts
|
16
|
+
parameters.each_with_object(Hash.new(0)) {|(param_type, _), counts| counts[param_type] += 1 }
|
17
|
+
end
|
18
|
+
|
19
|
+
def parameter_type_count(*types)
|
20
|
+
parameter_type_counts.values_at(*types).compact.reduce(:+)
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_parameter_type?(*types)
|
24
|
+
parameter_type_count(*types) > 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_optional_parameter?
|
28
|
+
@has_optional_parameter ||= has_parameter_type?(:rest)
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_key_parameter?
|
32
|
+
@has_key_parameter ||= has_parameter_type?(:key, :keyreq, :keyrest)
|
33
|
+
end
|
34
|
+
|
35
|
+
def required_parameter_count
|
36
|
+
@required_parameter_count ||= parameter_type_count(:req, :opt)
|
37
|
+
end
|
38
|
+
|
39
|
+
def parameter_count
|
40
|
+
@parameter_count ||= has_parameter_type?(:rest) ? Float::INFINITY : required_parameter_count
|
41
|
+
end
|
42
|
+
|
43
|
+
# override
|
44
|
+
def evaluate(*args, **options)
|
45
|
+
if lambda?
|
46
|
+
required_args = Array.new(required_parameter_count) {|i| args[i] }
|
47
|
+
optional_args = args[required_parameter_count...args.length] if has_optional_parameter?
|
48
|
+
else
|
49
|
+
required_args, optional_args = args, nil
|
50
|
+
end
|
51
|
+
|
52
|
+
if has_key_parameter?
|
53
|
+
call(*required_args, *optional_args, **options)
|
54
|
+
else
|
55
|
+
call(*required_args, *optional_args)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end # refine Proc
|
60
|
+
end
|
61
|
+
|
62
|
+
require 'proc_eval/version'
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proc_eval
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brent Jacobs
|
8
|
+
- br3nt
|
9
|
+
- reeganviljoen
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2023-09-01 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: |2
|
16
|
+
Adds an `evaulate` refinement method to Proc and Object instances.
|
17
|
+
|
18
|
+
The goal of this gem is to allow evaluation of variables, procs, and lambdas with the same level of flexibility.
|
19
|
+
|
20
|
+
The `evaluate` method has been added to the Object class to simply return the value of the variable.
|
21
|
+
The `evaluate` method is overriden on the Proc class to allow parameters to be passed to lambdas in the same flexible way as procs.
|
22
|
+
This takes into consideration, required/optional/remaining parameters, and required/optional/remaining keyword parameters.
|
23
|
+
|
24
|
+
For information on Refinements, see:
|
25
|
+
- https://ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html
|
26
|
+
- https://ruby-doc.org/core-2.1.0/doc/syntax/refinements_rdoc.html
|
27
|
+
- https://ruby-doc.org/core-2.2.0/doc/syntax/refinements_rdoc.html
|
28
|
+
- https://ruby-doc.org/core-2.3.0/doc/syntax/refinements_rdoc.html
|
29
|
+
- https://ruby-doc.org/core-2.4.0/doc/syntax/refinements_rdoc.html
|
30
|
+
- http://yehudakatz.com/2010/11/30/ruby-2-0-refinements-in-practice/
|
31
|
+
email:
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/proc_eval.rb
|
37
|
+
- lib/proc_eval/version.rb
|
38
|
+
homepage: https://github.com/reeganviljoen/proc_eval
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '2.7'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.4.15
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Allow evaluation of variables, procs, and lambdas with the same level of
|
61
|
+
flexibility.
|
62
|
+
test_files: []
|