named_parameters 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -0
- data/spec/named_parameters_spec.rb +18 -5
- data/src/named_parameters.rb +5 -5
- metadata +3 -3
data/README
CHANGED
@@ -74,3 +74,20 @@ All paramaters with no default value are mandatory:
|
|
74
74
|
a: "overwritten") # => Error: Mandatory parameter 'c' not given
|
75
75
|
|
76
76
|
|
77
|
+
== Blocks
|
78
|
+
Blocks are not interfered by named_parameters. You can just use them as usual.
|
79
|
+
class AnyClass
|
80
|
+
named_parameters(a: [])
|
81
|
+
def some_method(a)
|
82
|
+
a.each {|e| yield e}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
any_object.some_method(a: %w(hello world)) {|s| s.upcase!} # => ["HELLO", "WORLD"]
|
87
|
+
|
88
|
+
|
89
|
+
---
|
90
|
+
|
91
|
+
== Changelog
|
92
|
+
[0.1.0] First release
|
93
|
+
[0.1.1] Added documentation for blocks
|
@@ -7,7 +7,7 @@ class Book
|
|
7
7
|
|
8
8
|
named_parameters
|
9
9
|
|
10
|
-
def initialize author, title, year
|
10
|
+
def initialize (author, title, year)
|
11
11
|
@author = author
|
12
12
|
@title = title
|
13
13
|
@year = year
|
@@ -15,22 +15,29 @@ class Book
|
|
15
15
|
|
16
16
|
named_parameters
|
17
17
|
|
18
|
-
def foo bar, baz
|
18
|
+
def foo (bar, baz)
|
19
19
|
[bar, baz]
|
20
20
|
end
|
21
21
|
|
22
22
|
named_parameters(foo: "foo", bar: "bar")
|
23
23
|
|
24
|
-
def method_with_defaults_only foo, bar
|
24
|
+
def method_with_defaults_only (foo, bar)
|
25
25
|
[foo, bar]
|
26
26
|
end
|
27
27
|
|
28
28
|
named_parameters(optional: "optional")
|
29
29
|
|
30
|
-
|
30
|
+
#noinspection RubyInstanceMethodNamingConvention
|
31
|
+
def method_with_defaults_and_mandatory (optional, mandatory)
|
31
32
|
[optional, mandatory]
|
32
33
|
end
|
33
34
|
|
35
|
+
named_parameters
|
36
|
+
|
37
|
+
def method_with_block (array)
|
38
|
+
array.each { |e| yield e }
|
39
|
+
end
|
40
|
+
|
34
41
|
end
|
35
42
|
|
36
43
|
describe Book do
|
@@ -76,7 +83,7 @@ describe Book do
|
|
76
83
|
class Book
|
77
84
|
some_defaults(a: "a", b: "b")
|
78
85
|
|
79
|
-
def a_method a, c
|
86
|
+
def a_method (a, c)
|
80
87
|
#... do something
|
81
88
|
end
|
82
89
|
end
|
@@ -90,5 +97,11 @@ describe Book do
|
|
90
97
|
optional: "only optional is given") }.should raise_error(RuntimeError)
|
91
98
|
end
|
92
99
|
|
100
|
+
it "should accept blocks for methods" do
|
101
|
+
array = []
|
102
|
+
book.method_with_block(array: [1, 2, 3]) { |e| array << e }
|
103
|
+
array.should == [1, 2, 3]
|
104
|
+
end
|
105
|
+
|
93
106
|
|
94
107
|
end
|
data/src/named_parameters.rb
CHANGED
@@ -62,13 +62,13 @@ module NamedParameters
|
|
62
62
|
|
63
63
|
# @param method_name [Symbol]
|
64
64
|
# @return [Object] Not important
|
65
|
-
def redefine_all_required_method(method_name)
|
65
|
+
def redefine_all_required_method(method_name,&block)
|
66
66
|
method = instance_method method_name
|
67
67
|
parameter_definition = method.parameters.collect { |_, b| b }
|
68
68
|
old_name = (method_name.to_s+"_before_named_parameters").to_sym
|
69
69
|
alias_method old_name, method_name
|
70
70
|
module_eval do
|
71
|
-
define_method method_name do |options|
|
71
|
+
define_method method_name do |options,&block|
|
72
72
|
missing = parameter_definition-(options.keys)
|
73
73
|
unless missing.empty?
|
74
74
|
raise RuntimeError.new("Missing arguments: #{missing}")
|
@@ -81,7 +81,7 @@ module NamedParameters
|
|
81
81
|
parameter_definition.each { |parameter| parameters << options[parameter] }
|
82
82
|
eval <<-STRING
|
83
83
|
obj = self
|
84
|
-
method.bind(obj).call *parameters
|
84
|
+
method.bind(obj).call *parameters, &block
|
85
85
|
STRING
|
86
86
|
end
|
87
87
|
end
|
@@ -102,7 +102,7 @@ but it is not a parameter of #{method_name}") unless parameter_definition.includ
|
|
102
102
|
old_name = (method_name.to_s+"_before_named_parameters").to_sym
|
103
103
|
alias_method old_name, method_name
|
104
104
|
module_eval do
|
105
|
-
define_method method_name do |options = { }|
|
105
|
+
define_method method_name do |options = { },&block|
|
106
106
|
unexpected = options.keys - parameter_definition
|
107
107
|
unless unexpected.empty?
|
108
108
|
raise RuntimeError.new("Unexpected arguments: #{unexpected}")
|
@@ -120,7 +120,7 @@ given") }
|
|
120
120
|
end
|
121
121
|
eval <<-STRING
|
122
122
|
obj = self
|
123
|
-
method.bind(obj).call *parameters
|
123
|
+
method.bind(obj).call *parameters, &block
|
124
124
|
STRING
|
125
125
|
end
|
126
126
|
end
|
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: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-11 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &27135960 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version: 3.0.0
|
25
25
|
type: :development
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *27135960
|
28
28
|
description: ! 'By including the NamedParameters module into your class/module the
|
29
29
|
''named_parameters'' macro is
|
30
30
|
|