method_struct 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -48,9 +48,25 @@ You can change it into this:
48
48
  class UsersController
49
49
  def create
50
50
  Registrator.call(params[:email], params[:name])
51
+ # Or
52
+ Registrator.call(:email => params[:email], :name => params[:name])
51
53
  end
52
54
  end
53
55
 
56
+ You can also specify a different method name like so:
57
+
58
+ class Registrator < MethodStruct.new(:email, :name, :method_name => :register)
59
+ def register
60
+ # ...
61
+ end
62
+ end
63
+
64
+ class UsersController
65
+ def create
66
+ Registrator.register(params[:email], params[:name])
67
+ end
68
+ end
69
+
54
70
  One hopes the benefits will be more obvious for more complex methods
55
71
 
56
72
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module MethodStruct
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/method_struct.rb CHANGED
@@ -2,16 +2,31 @@ require "method_struct/version"
2
2
 
3
3
  module MethodStruct
4
4
  def self.new(*fields)
5
+ if fields.last.is_a?(Hash)
6
+ method_name = fields.last[:method_name]
7
+ fields = fields.take(fields.size - 1)
8
+ else
9
+ method_name = :call
10
+ end
11
+
5
12
  Class.new do
6
- class << self
7
- define_method(:call) do |*field_values|
8
- new(*field_values).call
13
+ singleton_class = (class << self; self; end)
14
+
15
+ singleton_class.instance_eval do
16
+ define_method(method_name) do |*field_values|
17
+ new(*field_values).send(method_name)
9
18
  end
10
19
  end
11
20
 
12
21
  define_method(:initialize) do |*values|
13
- fields.zip(values).each do |field, value|
14
- instance_variable_set("@#{field}", value)
22
+ if fields.size > 1 && values.first.is_a?(Hash)
23
+ fields.each do |field|
24
+ instance_variable_set("@#{field}", values.first[field])
25
+ end
26
+ else
27
+ fields.zip(values).each do |field, value|
28
+ instance_variable_set("@#{field}", value)
29
+ end
15
30
  end
16
31
  end
17
32
 
@@ -4,19 +4,37 @@ require "method_struct"
4
4
 
5
5
  describe MethodStruct do
6
6
  describe ".new" do
7
- it "creates a class method which calls the declared instance method with the given context" do
8
- argument1 = double("argument1")
9
- argument2 = double("argument2")
10
- verifier = double("verifier")
11
- verifier.should_receive(:poke).with(argument1, argument2)
7
+ let(:argument1) { double("argument1") }
8
+ let(:argument2) { double("argument2") }
9
+ let(:verifier) { double("verifier") }
12
10
 
13
- Example = Class.new(MethodStruct.new(:x, :y)) do
11
+ def create_poker(verifier)
12
+ Class.new(MethodStruct.new(:x, :y)) do
14
13
  define_method(:call) do
15
14
  verifier.poke(x, y)
16
15
  end
17
16
  end
17
+ end
18
+
19
+ before { verifier.should_receive(:poke).with(argument1, argument2) }
20
+
21
+ it "creates a class method which calls the declared instance method with the given context" do
22
+ create_poker(verifier).call(argument1, argument2)
23
+ end
24
+
25
+ it "creates a hash version of the call method" do
26
+ create_poker(verifier).call(:x => argument1, :y => argument2)
27
+ end
28
+
29
+ it "can change the name of the main method" do
30
+ the_verifier = verifier
31
+ poker = Class.new(MethodStruct.new(:x, :y, :method_name => :something)) do
32
+ define_method(:something) do
33
+ the_verifier.poke(x, y)
34
+ end
35
+ end
18
36
 
19
- Example.call(argument1, argument2)
37
+ poker.something(argument1, argument2)
20
38
  end
21
39
  end
22
40
  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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Pawe\xC5\x82 Obrok"