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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: effb6b4959dd2fb08715672c764c4b378589df93
4
- data.tar.gz: 6ad27502f30524f5e7d4d607da5a0a52c1c49b41
3
+ metadata.gz: cc400291392d1546d0296ebcd10e138871343f5b
4
+ data.tar.gz: c97d099f20ded0fef91f52e660d7cb624a04d4b8
5
5
  SHA512:
6
- metadata.gz: d31b27a32dfb921dc203ff35e7bd17fef1d9aee35890cac9459ae9f885d0ecd8bbe7c1b963e3377e41ccff40bdffa9fa67ceaad8cd726e0764ed4220f6e13e12
7
- data.tar.gz: efd233e134289548af65024ceb5c3131197ef99a651d308aa6baa389efc0c7795c11685632723fe4c92ec46e05dc4176c61c42becfe0419bc71c0814c8f953a1
6
+ metadata.gz: 9fdc4d1eb2bfafae669958d0ea2ce5f23a89a305404673e6eb67a2a0aa3163f9bffa818a7c7a499b385dc169acc0ca20433eddc385832ee90c5da384aaab1846
7
+ data.tar.gz: cc6bb27ee263954aded6cba16a820dd587c2ab28a8868f4777ae1bccc2ee1aabda9c689a1e8300c51cc98426ab9240ef5659a64afd24a238df61088f681d3229
@@ -0,0 +1,6 @@
1
+ ## Current 0.4.0
2
+
3
+ ### Enhancement
4
+ - Add the ability to pass blocks as values with the options blocks_as_values: true given in the initializer.
5
+
6
+ ## Initial 0.3.0
@@ -2,8 +2,19 @@ require "reverse_parameters/version"
2
2
 
3
3
  class ReverseParameters
4
4
 
5
- # @param input [Proc, Array]
6
- def initialize(input)
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
- "&#{name}"
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
@@ -1,3 +1,3 @@
1
1
  class ReverseParameters
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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: 2015-11-28 00:00:00.000000000 Z
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