method_struct 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .ruby-version
19
+ bin
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-18mode
5
+ - jruby-19mode
6
+ - rbx-18mode
7
+ - rbx-19mode
8
+ - 1.8.7
9
+ script: "bundle exec rspec"
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # MethodStruct
2
2
 
3
- Facilitates extracting large methods into objects - see Usage
3
+ [![Build Status](https://travis-ci.org/basecrm/method_struct.png?branch=master)](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
- class Registrator < MethodStruct.new(:email, :name, :method_name => :register)
59
- def register
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
- class UsersController
65
- def create
66
- Registrator.register(params[:email], params[:name])
73
+ class UsersController
74
+ def create
75
+ Registrator.register(params[:email], params[:name])
76
+ end
67
77
  end
68
- end
78
+ ```
69
79
 
70
80
  One hopes the benefits will be more obvious for more complex methods
71
81
 
@@ -1,3 +1,3 @@
1
1
  module MethodStruct
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
@@ -16,17 +16,21 @@ describe MethodStruct do
16
16
  end
17
17
  end
18
18
 
19
- before { verifier.should_receive(:poke).with(argument1, argument2) }
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
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-15 00:00:00 +02:00
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