named_parameters 1.1.0 → 1.1.1
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.
- data/README +40 -8
- data/lib/named_parameters.rb +12 -2
- metadata +2 -2
data/README
CHANGED
@@ -39,8 +39,6 @@ Surprisingly it is this easy:
|
|
39
39
|
b: 42)
|
40
40
|
|
41
41
|
|
42
|
-
|
43
|
-
---
|
44
42
|
== Optional parameters
|
45
43
|
You may also define optional parameters with defaults values:
|
46
44
|
class AnyClass
|
@@ -103,39 +101,73 @@ actual named parameters hash. Use the super mode like this:
|
|
103
101
|
|
104
102
|
class SomeLibraryClass
|
105
103
|
def setup(options)
|
106
|
-
|
107
|
-
options
|
104
|
+
@d = options[:d]
|
108
105
|
end
|
109
106
|
end
|
110
107
|
|
111
108
|
class AnyClass
|
112
109
|
named_parameters_super
|
113
110
|
def setup(a,b,c)
|
114
|
-
|
111
|
+
@a = a
|
112
|
+
@b = b
|
113
|
+
@c = c
|
115
114
|
end
|
116
115
|
end
|
117
116
|
|
118
117
|
any_object.setup(a: "a",
|
119
118
|
b: "b",
|
120
119
|
c: "c",
|
121
|
-
d: "d")
|
120
|
+
d: "d")
|
121
|
+
any_object.a # => "a"
|
122
|
+
any_object.b # => "b"
|
123
|
+
any_object.c # => "c"
|
124
|
+
any_object.d # => "d"
|
122
125
|
|
123
126
|
== Combine super and strict
|
124
127
|
In case you need to combine super and strict mode, do:
|
125
128
|
|
126
129
|
class AnyClass < SomeLibraryClass
|
127
|
-
named_parameters_strict_super # or
|
130
|
+
named_parameters_strict_super # or named_parameters_super_strict
|
128
131
|
def some_method(a,b,c)
|
129
132
|
# do something with a and b, maybe something with c will be done in super
|
130
133
|
end
|
131
134
|
end
|
132
135
|
|
136
|
+
|
137
|
+
== <my_method>_has_named_parameters
|
138
|
+
There are actually two ways to make a your method accessible with named parameters. You can either declare the named
|
139
|
+
parameters just before the method definition (this is done above) or you can define them anytime after the method
|
140
|
+
definition:
|
141
|
+
|
142
|
+
class AnyClass
|
143
|
+
def some_method(a,b,c)
|
144
|
+
# do something with a, b and c
|
145
|
+
end
|
146
|
+
|
147
|
+
# lots of other code, if you like so
|
148
|
+
# ...
|
149
|
+
|
150
|
+
some_method_has_named_parameters(a: "a", b: "b")
|
151
|
+
end
|
152
|
+
|
153
|
+
This will do the same as:
|
154
|
+
|
155
|
+
class AnyClass
|
156
|
+
named_parameters(a: "a", b: "b")
|
157
|
+
def some_method(a,b,c)
|
158
|
+
# do something with a, b and c
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
With this method you can also use super and strict mode as usual
|
163
|
+
|
133
164
|
== Acknowledgements
|
134
165
|
|
135
166
|
Kudos to Juris Galang and his gem named-parameters. (http://www.rubygems.org/gems/named-parameters)
|
136
|
-
|
167
|
+
Definitely have a look at this gem also, if you want to use named parameters.
|
137
168
|
|
138
169
|
== Changelog
|
170
|
+
[1.1.1] * Added documentation for ghost methods "my_method_has_named_parameters"
|
139
171
|
[1.1.0] * Added ghost methods "my_method_has_named_parameters"
|
140
172
|
[1.0.2] * Removed unimportant methods from documentation (again)
|
141
173
|
[1.0.1] * Removed unimportant methods from documentation
|
data/lib/named_parameters.rb
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
#
|
2
|
-
# Include this module to enable the NamedParameters#named_parameters
|
2
|
+
# Include this module to enable the NamedParameters#named_parameters macros
|
3
3
|
#
|
4
4
|
module NamedParameters
|
5
5
|
|
6
|
+
# Works like NamedParameters#named_parameters, but instead of using it right before a method definition,
|
7
|
+
# you use it afterwards. You can also add the strict and super mode. Replace the +my_method+ with the name of the
|
8
|
+
# method that you want to have named parameters
|
9
|
+
# @see NamedParameters#named_parameters
|
10
|
+
# @see NamedParameters#named_parameters_strict
|
11
|
+
# @see NamedParameters#named_parameters_super
|
12
|
+
# @see NamedParameters#named_parameters_strict_super
|
13
|
+
def my_method_has_named_parameters(optionals = { })
|
14
|
+
# Empty method just for the documentation. Those methods are realized in NamedParameters#method_missing
|
15
|
+
end
|
16
|
+
|
6
17
|
# Makes the next method callable via named parameters (options hash)
|
7
18
|
# The caller hash may contain keys that are not contained in the parameters of the next method
|
8
19
|
# @param [Hash[Symbol,Object]] optionals Optional default values for the parameters
|
@@ -33,7 +44,6 @@ module NamedParameters
|
|
33
44
|
_named_parameters optionals, true, true
|
34
45
|
end
|
35
46
|
|
36
|
-
|
37
47
|
alias_method :named_parameters_super_strict, :named_parameters_strict_super
|
38
48
|
|
39
49
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: named_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|