method_struct 0.0.4 → 0.0.5
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/.gitignore +1 -0
- data/.travis.yml +9 -0
- data/README.md +19 -9
- data/lib/method_struct/version.rb +1 -1
- data/lib/method_struct.rb +1 -1
- data/spec/method_struct_spec.rb +28 -1
- metadata +5 -4
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# MethodStruct
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/basecrm/method_struct)
|
4
|
+
|
5
|
+
Facilitates extracting large methods into objects - see Usage.
|
6
|
+
For a more in-depth treatment of the refactoring see
|
7
|
+
http://sourcemaking.com/refactoring/replace-method-with-method-object
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -20,15 +24,18 @@ Or install it yourself as:
|
|
20
24
|
|
21
25
|
Say you have this:
|
22
26
|
|
27
|
+
```ruby
|
23
28
|
class UsersController
|
24
29
|
def create
|
25
30
|
User.create(:email => params[:email], :name => params[:name])
|
26
31
|
Mailer.registration_email(params[:email]).deliver
|
27
32
|
end
|
28
33
|
end
|
34
|
+
```
|
29
35
|
|
30
36
|
You can change it into this:
|
31
37
|
|
38
|
+
```ruby
|
32
39
|
class Registrator < MethodStruct.new(:email, :name)
|
33
40
|
def call
|
34
41
|
create_user!
|
@@ -52,20 +59,23 @@ You can change it into this:
|
|
52
59
|
Registrator.call(:email => params[:email], :name => params[:name])
|
53
60
|
end
|
54
61
|
end
|
62
|
+
```
|
55
63
|
|
56
64
|
You can also specify a different method name like so:
|
57
65
|
|
58
|
-
|
59
|
-
|
60
|
-
|
66
|
+
```ruby
|
67
|
+
class Registrator < MethodStruct.new(:email, :name, :method_name => :register)
|
68
|
+
def register
|
69
|
+
# ...
|
70
|
+
end
|
61
71
|
end
|
62
|
-
end
|
63
72
|
|
64
|
-
|
65
|
-
|
66
|
-
|
73
|
+
class UsersController
|
74
|
+
def create
|
75
|
+
Registrator.register(params[:email], params[:name])
|
76
|
+
end
|
67
77
|
end
|
68
|
-
|
78
|
+
```
|
69
79
|
|
70
80
|
One hopes the benefits will be more obvious for more complex methods
|
71
81
|
|
data/lib/method_struct.rb
CHANGED
@@ -19,7 +19,7 @@ module MethodStruct
|
|
19
19
|
end
|
20
20
|
|
21
21
|
define_method(:initialize) do |*values|
|
22
|
-
if fields.size > 1 && values.first.is_a?(Hash)
|
22
|
+
if fields.size > 1 && values.size == 1 && values.first.is_a?(Hash)
|
23
23
|
fields.each do |field|
|
24
24
|
instance_variable_set("@#{field}", values.first[field])
|
25
25
|
end
|
data/spec/method_struct_spec.rb
CHANGED
@@ -16,17 +16,21 @@ describe MethodStruct do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
before {
|
19
|
+
before {}
|
20
20
|
|
21
21
|
it "creates a class method which calls the declared instance method with the given context" do
|
22
|
+
verifier.should_receive(:poke).with(argument1, argument2)
|
22
23
|
create_poker(verifier).call(argument1, argument2)
|
23
24
|
end
|
24
25
|
|
25
26
|
it "creates a hash version of the call method" do
|
27
|
+
verifier.should_receive(:poke).with(argument1, argument2)
|
26
28
|
create_poker(verifier).call(:x => argument1, :y => argument2)
|
27
29
|
end
|
28
30
|
|
29
31
|
it "can change the name of the main method" do
|
32
|
+
verifier.should_receive(:poke).with(argument1, argument2)
|
33
|
+
|
30
34
|
the_verifier = verifier
|
31
35
|
poker = Class.new(MethodStruct.new(:x, :y, :method_name => :something)) do
|
32
36
|
define_method(:something) do
|
@@ -36,5 +40,28 @@ describe MethodStruct do
|
|
36
40
|
|
37
41
|
poker.something(argument1, argument2)
|
38
42
|
end
|
43
|
+
|
44
|
+
context "when arguments are hashes" do
|
45
|
+
let(:argument1) { { :things => true } }
|
46
|
+
let(:argument2) { { :stuff => true } }
|
47
|
+
|
48
|
+
it "handles them correctly" do
|
49
|
+
verifier.should_receive(:poke).with(argument1, argument2)
|
50
|
+
create_poker(verifier).call(argument1, argument2)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "allows the single argument to be a hash" do
|
54
|
+
verifier.should_receive(:poke).with(argument1)
|
55
|
+
|
56
|
+
the_verifier = verifier
|
57
|
+
poker = Class.new(MethodStruct.new(:x)) do
|
58
|
+
define_method(:call) do
|
59
|
+
the_verifier.poke(x)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
poker.call(argument1)
|
64
|
+
end
|
65
|
+
end
|
39
66
|
end
|
40
67
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Pawe\xC5\x82 Obrok"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-07-
|
18
|
+
date: 2013-07-17 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -72,6 +72,7 @@ extra_rdoc_files: []
|
|
72
72
|
|
73
73
|
files:
|
74
74
|
- .gitignore
|
75
|
+
- .travis.yml
|
75
76
|
- Gemfile
|
76
77
|
- LICENSE.txt
|
77
78
|
- README.md
|