reverse_parameters 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +8 -0
- data/README.md +18 -0
- data/lib/reverse_parameters.rb +58 -49
- data/lib/reverse_parameters/core_ext.rb +1 -0
- data/lib/reverse_parameters/core_ext/refinements.rb +15 -0
- data/lib/reverse_parameters/version.rb +2 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0137ecfeb2ebd8f1b36b33d81bf081071729ad77
|
4
|
+
data.tar.gz: 14a1e5ba91398b76e45600e149c5e33d08d230e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d970197d444268b8863e0717f40c34b23de1d3b9110be953ca4940b902b83d6ebb2aca69404b5b0b983ea184d73627051d20cd92365eb7c685c992f03aa9a96b
|
7
|
+
data.tar.gz: 1f363ed82c88350f66ff4fa587290a69e9895df7a54de08c22fffa82a6079ad07a8d1142ad2a18bb2cd47298098fc20098d8a4c9b27bccf079ca19c6927e9a88
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## 1.1.0 - 2016-03-24
|
2
|
+
|
3
|
+
### API Changes
|
4
|
+
- `ReverseParameters` instance will now be `ReverseParameters::Base`. This should not change any usage.
|
5
|
+
|
6
|
+
### Enhancement
|
7
|
+
- Add Ruby 2.1 Refinements ie. `using ReverseParameters` to enable `Method#reverse_parameters` and `UnboundMethod#reverse_parameters`
|
8
|
+
|
1
9
|
## 1.0.0 - 2016-03-24
|
2
10
|
|
3
11
|
### API Changes
|
data/README.md
CHANGED
@@ -43,6 +43,24 @@ method(:example_method).parameters
|
|
43
43
|
#=> [[:keyreq, :named_param]]
|
44
44
|
```
|
45
45
|
|
46
|
+
### Ruby Refinements
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
require 'reverse_parameters'
|
50
|
+
using ReverseParameters
|
51
|
+
|
52
|
+
def example_method(named_param:)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Method arguments are the real values passed to (and received by) the function.
|
56
|
+
method(:example_method).reverse_parameters_.arguments.to_s
|
57
|
+
#=> "named_param: named_param"
|
58
|
+
|
59
|
+
# Method parameters are the names listed in the function definition.
|
60
|
+
method(:example_method).reverse_parameters.parameters.to_s
|
61
|
+
#=> "named_param:"
|
62
|
+
```
|
63
|
+
|
46
64
|
### Monkey Patch to Ruby Core
|
47
65
|
|
48
66
|
*`Method` and `UnboundMethod`*
|
data/lib/reverse_parameters.rb
CHANGED
@@ -1,40 +1,49 @@
|
|
1
1
|
require "reverse_parameters/version"
|
2
|
+
require "reverse_parameters/core_ext/refinements"
|
2
3
|
|
3
|
-
|
4
|
+
module ReverseParameters
|
4
5
|
|
5
6
|
# @param [Proc, Array] input
|
6
|
-
def
|
7
|
-
|
8
|
-
@params = input.to_proc.parameters
|
9
|
-
elsif input.respond_to?(:to_ary)
|
10
|
-
@params = input.to_ary
|
11
|
-
else
|
12
|
-
raise ArgumentError.new("Input must be an Array of parameters or a Proc object.")
|
13
|
-
end
|
7
|
+
def self.new(*args)
|
8
|
+
Base.new(*args)
|
14
9
|
end
|
15
10
|
|
16
|
-
|
17
|
-
# @return [ReverseParameters::Parameters]
|
18
|
-
def parameters
|
19
|
-
Parameters.new(params)
|
20
|
-
end
|
11
|
+
class Base
|
21
12
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
def arguments(blocks_as_values: false)
|
33
|
-
Arguments.new(params, blocks_as_values: blocks_as_values)
|
34
|
-
end
|
13
|
+
# @param [Proc, Array] input
|
14
|
+
def initialize(input)
|
15
|
+
if input.respond_to?(:to_proc)
|
16
|
+
@params = input.to_proc.parameters
|
17
|
+
elsif input.respond_to?(:to_ary)
|
18
|
+
@params = input.to_ary
|
19
|
+
else
|
20
|
+
raise ArgumentError.new("Input must be an Array of parameters or a Proc object.")
|
21
|
+
end
|
22
|
+
end
|
35
23
|
|
36
|
-
|
37
|
-
|
24
|
+
# Method parameters are the names listed in the function definition.
|
25
|
+
# @return [ReverseParameters::Parameters]
|
26
|
+
def parameters
|
27
|
+
Parameters.new(params)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Method arguments are the real values passed to (and received by) the function.
|
31
|
+
# @return [ReverseParameters::Arguments]
|
32
|
+
# @param [true, false] blocks_as_values: express block as variable vs a proc passed to the end of a method.
|
33
|
+
# def my_method(&block)
|
34
|
+
# end
|
35
|
+
# ReverseParameters.new(method(:my_method)).arguments(blocks_as_values: true).to_s
|
36
|
+
# #=> "block"
|
37
|
+
#
|
38
|
+
# # ReverseParameters.new(method(:my_method)).arguments.to_s
|
39
|
+
# #=> "&block"
|
40
|
+
def arguments(blocks_as_values: false)
|
41
|
+
Arguments.new(params, blocks_as_values: blocks_as_values)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
attr_reader :params
|
46
|
+
end
|
38
47
|
|
39
48
|
class BaseCollection
|
40
49
|
include Enumerable
|
@@ -74,12 +83,12 @@ class ReverseParameters
|
|
74
83
|
class Arg < BaseCollection::Item
|
75
84
|
def to_s
|
76
85
|
case state
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
86
|
+
when :key, :keyreq
|
87
|
+
"#{name}: #{name}"
|
88
|
+
when :block
|
89
|
+
block(name)
|
90
|
+
else
|
91
|
+
name
|
83
92
|
end.to_s
|
84
93
|
end
|
85
94
|
|
@@ -122,20 +131,20 @@ class ReverseParameters
|
|
122
131
|
class Param < BaseCollection::Item
|
123
132
|
def to_s
|
124
133
|
case state
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
134
|
+
when :req
|
135
|
+
name
|
136
|
+
when :rest
|
137
|
+
"*#{name}"
|
138
|
+
when :keyrest
|
139
|
+
"**#{name}"
|
140
|
+
when :opt
|
141
|
+
"#{name}=nil"
|
142
|
+
when :keyreq
|
143
|
+
"#{name}:"
|
144
|
+
when :key
|
145
|
+
"#{name}: nil"
|
146
|
+
when :block
|
147
|
+
"&#{name}"
|
139
148
|
end.to_s
|
140
149
|
end
|
141
150
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ReverseParameters
|
2
|
+
refine Method do
|
3
|
+
# @return [ReverseParameters]
|
4
|
+
def reverse_parameters
|
5
|
+
ReverseParameters.new(parameters)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
refine UnboundMethod do
|
10
|
+
# @return [ReverseParameters]
|
11
|
+
def reverse_parameters
|
12
|
+
ReverseParameters.new(parameters)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "1.
|
1
|
+
module ReverseParameters
|
2
|
+
VERSION = "1.1.0".freeze
|
3
3
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reverse_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/reverse_parameters.rb
|
75
75
|
- lib/reverse_parameters/core_ext.rb
|
76
76
|
- lib/reverse_parameters/core_ext/method.rb
|
77
|
+
- lib/reverse_parameters/core_ext/refinements.rb
|
77
78
|
- lib/reverse_parameters/core_ext/unbound_method.rb
|
78
79
|
- lib/reverse_parameters/version.rb
|
79
80
|
- reverse_parameters.gemspec
|