reverse_parameters 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/reverse_parameters.rb +55 -9
- data/lib/reverse_parameters/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc400291392d1546d0296ebcd10e138871343f5b
|
4
|
+
data.tar.gz: c97d099f20ded0fef91f52e660d7cb624a04d4b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fdc4d1eb2bfafae669958d0ea2ce5f23a89a305404673e6eb67a2a0aa3163f9bffa818a7c7a499b385dc169acc0ca20433eddc385832ee90c5da384aaab1846
|
7
|
+
data.tar.gz: cc6bb27ee263954aded6cba16a820dd587c2ab28a8868f4777ae1bccc2ee1aabda9c689a1e8300c51cc98426ab9240ef5659a64afd24a238df61088f681d3229
|
data/CHANGELOG.md
ADDED
data/lib/reverse_parameters.rb
CHANGED
@@ -2,8 +2,19 @@ require "reverse_parameters/version"
|
|
2
2
|
|
3
3
|
class ReverseParameters
|
4
4
|
|
5
|
-
# @param
|
6
|
-
|
5
|
+
# @param [Proc, Array] input
|
6
|
+
# @params [true, false] blocks_as_values defaults to false
|
7
|
+
# @example
|
8
|
+
# def my_method(&block)
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# ReverseParameters.new(method(:my_method), blocks_as_values: true).arguments.to_s
|
12
|
+
# #=> "block"
|
13
|
+
#
|
14
|
+
# # ReverseParameters.new(method(:my_method)).to_s
|
15
|
+
# #=> "&block"
|
16
|
+
def initialize(input, blocks_as_values: false)
|
17
|
+
@blocks_as_values = blocks_as_values
|
7
18
|
if input.respond_to?(:to_proc)
|
8
19
|
@params = input.to_proc.parameters
|
9
20
|
elsif input.respond_to?(:to_ary)
|
@@ -22,17 +33,17 @@ class ReverseParameters
|
|
22
33
|
# Method arguments are the real values passed to (and received by) the function.
|
23
34
|
# @return [ReverseParameters::Arguments]
|
24
35
|
def arguments
|
25
|
-
Arguments.new(params)
|
36
|
+
Arguments.new(params, blocks_as_values: blocks_as_values)
|
26
37
|
end
|
27
38
|
|
28
39
|
private
|
29
|
-
attr_reader :params
|
40
|
+
attr_reader :params, :blocks_as_values
|
30
41
|
|
31
42
|
class BaseCollection
|
32
43
|
include Enumerable
|
33
44
|
|
34
|
-
def initialize(collection)
|
35
|
-
@collection = collection.map { |state, name| item_class.new(state: state, name: name) }
|
45
|
+
def initialize(collection, **options)
|
46
|
+
@collection = collection.map { |state, name| item_class.new(state: state, name: name, **options) }
|
36
47
|
end
|
37
48
|
|
38
49
|
def each(&block)
|
@@ -48,32 +59,67 @@ class ReverseParameters
|
|
48
59
|
end
|
49
60
|
|
50
61
|
class Item
|
51
|
-
def initialize(name:, state
|
62
|
+
def initialize(name:, state:, **options)
|
52
63
|
@name = name
|
53
64
|
@state = state
|
65
|
+
post_initialize(options)
|
66
|
+
end
|
67
|
+
|
68
|
+
def post_initialize(*)
|
54
69
|
end
|
55
70
|
|
56
71
|
attr_reader :name, :state
|
57
72
|
end
|
58
73
|
end
|
59
74
|
|
60
|
-
class Arguments < BaseCollection
|
75
|
+
class Arguments < BaseCollection
|
76
|
+
|
61
77
|
class Arg < BaseCollection::Item
|
62
78
|
def to_s
|
63
79
|
case state
|
64
80
|
when :key, :keyreq
|
65
81
|
"#{name}: #{name}"
|
66
82
|
when :block
|
67
|
-
|
83
|
+
block(name)
|
68
84
|
else
|
69
85
|
name
|
70
86
|
end.to_s
|
71
87
|
end
|
88
|
+
|
89
|
+
def block_as_value
|
90
|
+
case state
|
91
|
+
when :key, :keyreq
|
92
|
+
"#{name}: #{name}"
|
93
|
+
else
|
94
|
+
name
|
95
|
+
end.to_s
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def post_initialize(blocks_as_values:)
|
101
|
+
@blocks_as_values = blocks_as_values
|
102
|
+
end
|
103
|
+
|
104
|
+
def block(name)
|
105
|
+
if @blocks_as_values
|
106
|
+
name
|
107
|
+
else
|
108
|
+
"&#{name}"
|
109
|
+
end
|
110
|
+
end
|
72
111
|
end
|
73
112
|
|
113
|
+
|
74
114
|
def item_class
|
75
115
|
Arguments::Arg
|
76
116
|
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def block(name, pre="&")
|
121
|
+
[pre, name].compact!.join("")
|
122
|
+
end
|
77
123
|
end
|
78
124
|
class Parameters < BaseCollection;
|
79
125
|
class Param < BaseCollection::Item
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reverse_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- ".gitignore"
|
64
64
|
- ".rspec"
|
65
65
|
- ".travis.yml"
|
66
|
+
- CHANGELOG.md
|
66
67
|
- CODE_OF_CONDUCT.md
|
67
68
|
- Gemfile
|
68
69
|
- LICENSE.txt
|