method_object 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -37
- data/lib/method_object.rb +22 -14
- data/method_object.gemspec +3 -1
- metadata +21 -6
- data/lib/method_object/version.rb +0 -3
data/README.md
CHANGED
@@ -25,22 +25,11 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
# take the following long method
|
27
27
|
def drive_to location, speed=:slow
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
et dolore magna aliqua. Ut enim ad minim veniam,
|
34
|
-
quis nostrud exercitation ullamco laboris nisi ut
|
35
|
-
aliquip ex ea commodo
|
36
|
-
|
37
|
-
# start car
|
38
|
-
consequat. Duis aute irure dolor in reprehenderit in voluptate
|
39
|
-
cillum dolore eu fugiat nulla pariatur. Excepteur
|
40
|
-
proident, sunt in culpa qui officia deserunt mollit anim
|
41
|
-
|
42
|
-
# go
|
43
|
-
id est laborum.
|
28
|
+
car = Car.new
|
29
|
+
car.location = GoogleMaps.find(location)
|
30
|
+
car.start!
|
31
|
+
car.speed = speed
|
32
|
+
return car
|
44
33
|
end
|
45
34
|
|
46
35
|
end
|
@@ -52,40 +41,36 @@ up your code without cluttering Car
|
|
52
41
|
class Car
|
53
42
|
|
54
43
|
class DriveTo < MethodObject
|
55
|
-
def call location, speed
|
56
|
-
@location, @speed = location, speed
|
57
|
-
find_location
|
58
|
-
set_speed
|
59
|
-
start_car
|
60
|
-
go
|
61
|
-
end
|
62
44
|
|
63
|
-
|
64
|
-
|
65
|
-
|
45
|
+
# Note: call takes no arguments. The hash given to Car.call is
|
46
|
+
# turn into instance variables
|
47
|
+
def call
|
48
|
+
find_location!
|
49
|
+
start_car!
|
50
|
+
set_speed!
|
51
|
+
return car
|
66
52
|
end
|
67
53
|
|
68
|
-
def
|
69
|
-
|
70
|
-
quis nostrud exercitation ullamco laboris nisi ut
|
71
|
-
aliquip ex ea commodo
|
54
|
+
def car
|
55
|
+
@car ||= Car.new
|
72
56
|
end
|
73
57
|
|
74
|
-
def
|
75
|
-
|
76
|
-
cillum dolore eu fugiat nulla pariatur. Excepteur
|
77
|
-
proident, sunt in culpa qui officia deserunt mollit anim
|
58
|
+
def find_location!
|
59
|
+
car.location = GoogleMaps.find(@location)
|
78
60
|
end
|
79
61
|
|
80
|
-
def
|
81
|
-
|
62
|
+
def start_car!
|
63
|
+
car.start!
|
82
64
|
end
|
83
65
|
|
66
|
+
def set_speed!
|
67
|
+
car.speed = @speed
|
68
|
+
end
|
84
69
|
|
85
70
|
end
|
86
71
|
|
87
72
|
def drive_to location, speed=:slow
|
88
|
-
DriveTo.call(location, speed)
|
73
|
+
DriveTo.call(:location => location, :speed => speed)
|
89
74
|
end
|
90
75
|
|
91
76
|
end
|
data/lib/method_object.rb
CHANGED
@@ -1,12 +1,19 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_struct'
|
2
2
|
|
3
|
-
class MethodObject
|
3
|
+
class MethodObject < SimpleStruct
|
4
|
+
|
5
|
+
VERSION = "0.0.3"
|
4
6
|
|
5
7
|
class << self
|
6
8
|
|
7
|
-
|
9
|
+
def new *members, &block
|
10
|
+
subclass = super(*members, &block)
|
11
|
+
subclass.send(:private_class_method, :new)
|
12
|
+
subclass
|
13
|
+
end
|
14
|
+
|
8
15
|
def call *args
|
9
|
-
new
|
16
|
+
new(*args).call
|
10
17
|
end
|
11
18
|
|
12
19
|
def to_proc
|
@@ -22,9 +29,9 @@ eval <<-RUBY if $0 == __FILE__
|
|
22
29
|
require 'test/unit'
|
23
30
|
|
24
31
|
# example method objects
|
25
|
-
|
26
|
-
def call
|
27
|
-
|
32
|
+
FindTreasureChest = MethodObject.new(:color, :size) do
|
33
|
+
def call
|
34
|
+
[@color, @size, :treasure_chest]
|
28
35
|
end
|
29
36
|
end
|
30
37
|
|
@@ -35,17 +42,18 @@ class MethodObjectTestUnitTestCase < Test::Unit::TestCase
|
|
35
42
|
assert_raises(NoMethodError){ FindTreasureChest.new }
|
36
43
|
end
|
37
44
|
|
38
|
-
def test_unknown_options
|
39
|
-
assert_equal FindTreasureChest.call, [:treasure_chest]
|
40
|
-
end
|
41
|
-
|
42
45
|
def test_that_expected_options_are_ok
|
43
|
-
|
46
|
+
run_proc_tests FindTreasureChest
|
44
47
|
end
|
45
48
|
|
46
49
|
def test_to_proc
|
47
|
-
|
48
|
-
|
50
|
+
run_proc_tests FindTreasureChest.to_proc
|
51
|
+
end
|
52
|
+
|
53
|
+
def run_proc_tests proc
|
54
|
+
assert_equal proc.call, [nil, nil, :treasure_chest]
|
55
|
+
assert_equal proc.call(:red), [:red, nil, :treasure_chest]
|
56
|
+
assert_equal proc.call(:red, 42), [:red, 42, :treasure_chest]
|
49
57
|
end
|
50
58
|
|
51
59
|
end
|
data/method_object.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/method_object
|
2
|
+
require File.expand_path('../lib/method_object', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Jared Grippe"]
|
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "method_object"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = MethodObject::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "simple_struct"
|
17
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
-
dependencies:
|
12
|
+
date: 2012-11-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: simple_struct
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
description: wraps up the method object pattern into a class
|
15
31
|
email:
|
16
32
|
- jared@deadlyicon.com
|
@@ -24,7 +40,6 @@ files:
|
|
24
40
|
- README.md
|
25
41
|
- Rakefile
|
26
42
|
- lib/method_object.rb
|
27
|
-
- lib/method_object/version.rb
|
28
43
|
- method_object.gemspec
|
29
44
|
homepage: https://github.com/deadlyicon/method_object
|
30
45
|
licenses: []
|
@@ -40,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
55
|
version: '0'
|
41
56
|
segments:
|
42
57
|
- 0
|
43
|
-
hash:
|
58
|
+
hash: 296936207624278339
|
44
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
60
|
none: false
|
46
61
|
requirements:
|
@@ -49,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
64
|
version: '0'
|
50
65
|
segments:
|
51
66
|
- 0
|
52
|
-
hash:
|
67
|
+
hash: 296936207624278339
|
53
68
|
requirements: []
|
54
69
|
rubyforge_project:
|
55
70
|
rubygems_version: 1.8.24
|