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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fdf9c667689874d90a1054cc41df3113bcaafd35
4
- data.tar.gz: 18f492d6d41851a951832e2ee1092090f4ae411f
3
+ metadata.gz: 0137ecfeb2ebd8f1b36b33d81bf081071729ad77
4
+ data.tar.gz: 14a1e5ba91398b76e45600e149c5e33d08d230e2
5
5
  SHA512:
6
- metadata.gz: ab20dbc01afe4bbb3f2f0063719be8ea0b556f73a459a6bf1a5159c9bef5efffa21b1e789d7d31947fc66318b314c6145afa8ab3dbd95d9dc419a25a24f13461
7
- data.tar.gz: da12846f06e9da789f2da0ffb1f0b585cacdf6a359ec81dfe725f47ecd08a618b68f60106d0be7348702424327d5945561a9d4aceb68ace4abc8747b66b5e60e
6
+ metadata.gz: d970197d444268b8863e0717f40c34b23de1d3b9110be953ca4940b902b83d6ebb2aca69404b5b0b983ea184d73627051d20cd92365eb7c685c992f03aa9a96b
7
+ data.tar.gz: 1f363ed82c88350f66ff4fa587290a69e9895df7a54de08c22fffa82a6079ad07a8d1142ad2a18bb2cd47298098fc20098d8a4c9b27bccf079ca19c6927e9a88
@@ -2,4 +2,5 @@ language: ruby
2
2
  rvm:
3
3
  - 2.1.6
4
4
  - 2.2.3
5
+ - 2.3.0
5
6
  before_install: gem install bundler -v 1.10.6
@@ -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`*
@@ -1,40 +1,49 @@
1
1
  require "reverse_parameters/version"
2
+ require "reverse_parameters/core_ext/refinements"
2
3
 
3
- class ReverseParameters
4
+ module ReverseParameters
4
5
 
5
6
  # @param [Proc, Array] input
6
- def initialize(input)
7
- if input.respond_to?(:to_proc)
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
- # Method parameters are the names listed in the function definition.
17
- # @return [ReverseParameters::Parameters]
18
- def parameters
19
- Parameters.new(params)
20
- end
11
+ class Base
21
12
 
22
- # Method arguments are the real values passed to (and received by) the function.
23
- # @return [ReverseParameters::Arguments]
24
- # @param [true, false] blocks_as_values: express block as variable vs a proc passed to the end of a method.
25
- # def my_method(&block)
26
- # end
27
- # ReverseParameters.new(method(:my_method)).arguments(blocks_as_values: true).to_s
28
- # #=> "block"
29
- #
30
- # # ReverseParameters.new(method(:my_method)).arguments.to_s
31
- # #=> "&block"
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
- private
37
- attr_reader :params
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
- when :key, :keyreq
78
- "#{name}: #{name}"
79
- when :block
80
- block(name)
81
- else
82
- name
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
- when :req
126
- name
127
- when :rest
128
- "*#{name}"
129
- when :keyrest
130
- "**#{name}"
131
- when :opt
132
- "#{name}=nil"
133
- when :keyreq
134
- "#{name}:"
135
- when :key
136
- "#{name}: nil"
137
- when :block
138
- "&#{name}"
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
@@ -1,2 +1,3 @@
1
+ require "reverse_parameters"
1
2
  require "reverse_parameters/core_ext/method"
2
3
  require "reverse_parameters/core_ext/unbound_method"
@@ -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
- class ReverseParameters
2
- VERSION = "1.0.0".freeze
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.0.0
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