direction 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99cab337abb6c620be8fe3226ced09dab0d5da77
4
- data.tar.gz: d986a770755c0e04e05d14781ea6c6aa59b98d40
3
+ metadata.gz: 727eb9585c633d12cc9995c9c73cc524eeba82ff
4
+ data.tar.gz: d069923519bf447766d13afbe2ab621137d229b4
5
5
  SHA512:
6
- metadata.gz: 86177cffdf52eea6125884f34ab68e91f1cc445f6e8484dce645c63f20397f994c61bffa2159033f1595f64888f999885eb9d8dd7cc51c23af41a37b3c858dea
7
- data.tar.gz: 3a0d61725e295c362273c653a4901e0226e1a64648c733aeff0e46cb2534c0852d213e87ea393ea2f5ccb38b4676b3de430708d14c31d7add889169054b91bfb
6
+ metadata.gz: c5e95f10ed99d01e632041d81d2157da594d5c46c8f3a85895b1ce70e47119345b66b77d29cd846582ae4c4b9bbe00b4766e7ebf21996bfbfb38e0ce58cfad8c
7
+ data.tar.gz: b84abee5835f01a005bfc615585e3bb71ba454088b012a63eac26a4f539c7afd7a907eb80e12ff0e6df96b506649e8663679868def45d6ce2902fec3931fd527
data/README.md CHANGED
@@ -11,11 +11,26 @@ Provide a feature like the Forwardable library, but set the return value to self
11
11
  It provides a class level "command" method to do message forwarding.
12
12
 
13
13
  ```ruby
14
- class SomeClass
14
+ class Person
15
15
  extend Direction
16
16
 
17
- command [:name, :id] => :collaborator, [:color, :type] => :partner
17
+ command [:print_address] => :home
18
+
19
+ attr_accessor :home
20
+ end
21
+
22
+ class Home
23
+ def print_address(template)
24
+ template << "... the address.."
25
+ end
18
26
  end
27
+
28
+ template = STDOUT
29
+ person = Person.new
30
+ person.home = Home.new
31
+ #commands won't leak internal structure and return the receiver of the command
32
+ person.print_address(template) #=> person
33
+
19
34
  ```
20
35
 
21
36
  This will define methods on instances that forward to the provided receiver while enforcing encapsulation of the relationship between objects.
data/lib/direction.rb CHANGED
@@ -7,7 +7,8 @@ require "direction/version"
7
7
  # class SomeClass
8
8
  # extend Direction
9
9
  #
10
- # command [:name, :id] => :collaborator, [:color, :type] => :@partner
10
+ # command [:print_details, :setup_things] => :collaborator
11
+ # query [:name, :id] => :collaborator, :type => :@partner
11
12
  # end
12
13
  #
13
14
  # This will define methods on instances that forward to the
@@ -28,4 +29,18 @@ module Direction
28
29
  end
29
30
  self.class_eval method_defs.join(' '), __FILE__, __LINE__
30
31
  end
32
+
33
+ def query(options)
34
+ method_defs = []
35
+ options.each_pair do |key, value|
36
+ Array(key).map do |command_name|
37
+ method_defs.unshift %{
38
+ def #{command_name}(*args, &block)
39
+ #{value}.__send__(:#{command_name}, *args, &block)
40
+ end
41
+ }
42
+ end
43
+ end
44
+ self.class_eval method_defs.join(' '), __FILE__, __LINE__
45
+ end
31
46
  end
@@ -1,3 +1,3 @@
1
1
  module Direction
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -3,6 +3,7 @@ require 'test_helper'
3
3
  class Person
4
4
  extend Direction
5
5
  command [:make_me_a_sandwich, :cook] => :@friend
6
+ query [:activities, :go] => :friend
6
7
  attr_accessor :friend
7
8
  end
8
9
 
@@ -14,6 +15,14 @@ class Friend
14
15
  def cook(what)
15
16
  Menu.record what
16
17
  end
18
+
19
+ def activities
20
+ 'running, biking, hiking'
21
+ end
22
+
23
+ def go(do_what)
24
+ Activities.record do_what
25
+ end
17
26
  end
18
27
 
19
28
  module Menu
@@ -28,7 +37,19 @@ module Menu
28
37
  end
29
38
  end
30
39
 
31
- describe Direction do
40
+ module Activities
41
+ def self.record(text)
42
+ list << text
43
+ end
44
+ def self.list
45
+ @list ||= []
46
+ end
47
+ def self.clear
48
+ @list = []
49
+ end
50
+ end
51
+
52
+ describe Direction, 'command' do
32
53
  let(:friend){ Friend.new }
33
54
  let(:person){ person = Person.new
34
55
  person.friend = friend
@@ -52,4 +73,25 @@ describe Direction do
52
73
  person.cook('yum')
53
74
  assert_includes Menu.list, "yum"
54
75
  end
76
+ end
77
+
78
+ describe Direction, 'query' do
79
+ let(:friend){ Friend.new }
80
+ let(:person){ person = Person.new
81
+ person.friend = friend
82
+ person
83
+ }
84
+ before do
85
+ Activities.clear
86
+ end
87
+
88
+ it 'forwards a message to another object' do
89
+ assert_includes person.activities, "running, biking, hiking"
90
+ end
91
+
92
+ it 'forwards additional arguments' do
93
+ assert_equal [], Activities.list
94
+ person.go('have fun')
95
+ assert_includes Activities.list, "have fun"
96
+ end
55
97
  end
data/test/sample.rb ADDED
@@ -0,0 +1,83 @@
1
+ require 'direction'
2
+
3
+ class MicroManager
4
+ def initialize(server)
5
+ @server = server
6
+ end
7
+ attr_accessor :server
8
+ end
9
+
10
+ class Server
11
+ def initialize(kitchen)
12
+ @kitchen = kitchen
13
+ @accountant = kitchen.accountant
14
+ end
15
+ attr_reader :kitchen
16
+ attr_accessor :customer
17
+
18
+ def order_food(food)
19
+ puts "I'll put that order in for you right now"
20
+ sleep(2)
21
+ kitchen.make_food(food)
22
+ serve_food(food)
23
+ end
24
+
25
+ def serve_food(food)
26
+ retrieve_food(food)
27
+ sleep(2)
28
+ puts " <Server retrieved #{food}>"
29
+ return_to_customer(food)
30
+ end
31
+
32
+ def retrieve_food(food)
33
+ kitchen.provide_food(food)
34
+ end
35
+
36
+ def return_to_customer(food)
37
+ puts "Here's your order of #{food}"
38
+ end
39
+
40
+ def pay_bill
41
+ @accountant.accept_funds
42
+ end
43
+ end
44
+
45
+ class Kitchen
46
+ def make_food(food)
47
+ puts " <Kitchen preparing #{food}>"
48
+ sleep(3)
49
+ puts " <Kitchen made #{food}>"
50
+ end
51
+
52
+ def provide_food(food)
53
+ puts " <Kitchen served up #{food}>"
54
+ end
55
+
56
+ def accountant
57
+ @accountant ||= Accountant.new
58
+ end
59
+ end
60
+
61
+ class Accountant
62
+ def accept_funds
63
+ puts " $$$ Cha-ching!"
64
+ end
65
+ end
66
+
67
+ class Customer
68
+ extend Direction
69
+ def initialize(server)
70
+ @server = server
71
+ end
72
+ attr_accessor :server
73
+
74
+ command [:order_food, :pay_bill] => :server
75
+ end
76
+
77
+ def setup
78
+ @kitchen = Kitchen.new
79
+ @server = Server.new(@kitchen)
80
+ @manager = MicroManager.new(@server)
81
+ @customer = Customer.new(@server)
82
+ @manager
83
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: direction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Jim Gay'"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-09 00:00:00.000000000 Z
11
+ date: 2014-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,6 +54,7 @@ files:
54
54
  - lib/direction.rb
55
55
  - lib/direction/version.rb
56
56
  - test/direction_test.rb
57
+ - test/sample.rb
57
58
  - test/test_helper.rb
58
59
  homepage: ''
59
60
  licenses:
@@ -81,4 +82,5 @@ specification_version: 4
81
82
  summary: Forward messages to collaborators in East-oriented style.
82
83
  test_files:
83
84
  - test/direction_test.rb
85
+ - test/sample.rb
84
86
  - test/test_helper.rb