proc_evaluate 1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d5bbc0f70c2017414214d9e67ab58da42120205
4
+ data.tar.gz: 760959a2ea491539da7ac693a94a376812ae43c4
5
+ SHA512:
6
+ metadata.gz: 218b7ee68fd2703071e384da31dfc33140ed92db7bf7b3755dced4017250c0c1a315dcfb83ed059ff4b233b3784d7b8913bcda12522febfe3d15972eae4c5e01
7
+ data.tar.gz: ccee34aba158e35c50055a1cebe438a3a73ca4f8f60cc40c10c97504286616f205fa846a5869177cfb08aeaa6fd5f9d24c62941f4752f5269d58fd5e604711ac
@@ -0,0 +1,62 @@
1
+ module ProcEvaluate
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_evaluate/version'
@@ -0,0 +1,3 @@
1
+ module ProcEvaluate
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proc_evaluate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brent Jacobs
8
+ - br3nt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-10-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: |2
15
+ Adds an `evaulate` refinement method to Proc and Object instances.
16
+
17
+ The goal of this gem is to allow evaluation of variables, procs, and lambdas with the same level of flexibility.
18
+
19
+ The `evaluate` method has been added to the Object class to simply return the value of the variable.
20
+ The `evaluate` method is overriden on the Proc class to allow parameters to be passed to lambdas in the same flexible way as procs.
21
+ This takes into consideration, required/optional/remaining parameters, and required/optional/remaining keyword parameters.
22
+
23
+ For information on Refinements, see:
24
+ - https://ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html
25
+ - https://ruby-doc.org/core-2.1.0/doc/syntax/refinements_rdoc.html
26
+ - https://ruby-doc.org/core-2.2.0/doc/syntax/refinements_rdoc.html
27
+ - https://ruby-doc.org/core-2.3.0/doc/syntax/refinements_rdoc.html
28
+ - https://ruby-doc.org/core-2.4.0/doc/syntax/refinements_rdoc.html
29
+ - http://yehudakatz.com/2010/11/30/ruby-2-0-refinements-in-practice/
30
+ email:
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/proc_evaluate.rb
36
+ - lib/proc_evaluate/version.rb
37
+ homepage: https://github.com/br3nt/proc_evaluate
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.6.12
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: []