barney 0.8.1 → 0.9.0

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/.yardopts CHANGED
@@ -3,6 +3,7 @@
3
3
  --readme README.md
4
4
  --hide-void-return
5
5
  --no-cache
6
+ --main README.md
6
7
  -
7
8
  LICENSE
8
9
  ChangeLog
data/ChangeLog CHANGED
@@ -1,51 +1,19 @@
1
- 2011-04-19 Robert Gleeson <rob@flowof.info> v0.8.1
1
+ 2011-04-21 Robert Gleeson <rob@flowof.info>
2
2
 
3
- * Don't call #to_sym when it isn't required.
4
- * Don't allow @variables be populated with duplicates. (bugfix)
3
+ * ChangeLog:
4
+ Change to GNU ChangeLog format.
5
5
 
6
- 2011-04-19 Robert Gleeson <rob@flowof.info> v0.8.0
6
+ * lib/barney/share.rb:
7
+ Don't lazy-delete objects from @streams.
8
+ Objects are removed from @streams during a call to #unshare now.
9
+ * lib/barney/share.rb:
10
+ Add the member 'variable' to StreamPair.
11
+ * lib/barney/share.rb:
12
+ Remove @shared.
13
+ Replaced by @streams, an Array of StreamPair objects.
14
+
15
+
7
16
 
8
- * Remove "seq" attribute from StreamPair.
9
- * Banish @seq.
10
- * Add HistoryItem (subclass of Struct).
11
- * Share#history returns an Array of HistoryItem objects.
12
- * Share#shared is deprecated. Use Shared#variables instead.
13
17
 
14
18
 
15
- 2011-03-14 Robert Gleeson <rob@flowof.info> v0.7.0
16
-
17
- * Barney::Share#initialize yields a block.
18
-
19
- 2011-03-04 Robert Gleeson <rob@flowof.info> v0.6.0
20
-
21
- * Store instances of StreamPair in an array.
22
- * Store sequence and pipe objects in a Struct referenced by StreamPair.
23
-
24
- 2011-03-04 Robert Gleeson <rob@flowof.info> v0.5.0
25
-
26
- * Solve Github Issue no. 3
27
- * Solve Github Issue no. 2
28
- * Write a more efficient and easier to understand Barney::Share#sync method.
29
-
30
- 2011-02-16 Robert Gleeson <rob@flowof.info> v0.4.1
31
-
32
- * Add http://www.gem-testers.org support.
33
- * Rewrite test suite using 'minitest'.
34
-
35
- 2011-02-12 Robert Gleeson <rob@flowof.info> v0.4.0
36
-
37
- * Add Barney::Share#history.
38
-
39
- 2011-01-16 Robert Gleeson <rob@flowof.info> v0.3.1
40
-
41
- * Solve Github Issue no. 1.
42
- * Add Barney::Share#pid.
43
-
44
- 2011-01-04 Robert Gleeson <rob@flowof.info> v0.2.0
45
-
46
- * Alias #synchronize as #sync.
47
-
48
- 2011-01-03 Robert Gleeson <rob@flowof.info> v0.2.0
49
-
50
- * Add Barney::Share#unshare.
51
- * Add Barney::Share#shared.
19
+
data/README.md CHANGED
@@ -26,7 +26,6 @@ Okay, now that we've got that out of the way, let's see what using Barney is lik
26
26
  #!/usr/bin/env ruby
27
27
  require 'barney'
28
28
 
29
-
30
29
  obj = Barney::Share.new
31
30
  obj.share :message
32
31
  message = 'Hello, '
@@ -60,14 +59,13 @@ I'm following the [Semantic Versioning](http://www.semver.org) policy.
60
59
 
61
60
  **API**
62
61
 
63
- * [master (git)](http://rubydoc.info/github/robgleeson/Barney/master/)
62
+ * [master (git)](http://rubydoc.info/github/robgleeson/barney/master/)
63
+ * [0.9.0](http://rubydoc.info/gems/barney/0.9.0/)
64
64
  * [0.8.1](http://rubydoc.info/gems/barney/0.8.1/)
65
65
  * [0.8.0](http://rubydoc.info/gems/barney/0.8.0/)
66
66
  * [0.7.0](http://rubydoc.info/gems/barney/0.7.0)
67
67
  * [0.6.0](http://rubydoc.info/gems/barney/0.6.0)
68
68
  * [0.5.0](http://rubydoc.info/gems/barney/0.5.0)
69
- * [0.4.1](http://rubydoc.info/gems/barney/0.4.1)
70
- * [0.4.0](http://rubydoc.info/gems/barney/0.4.0)
71
69
  * …
72
70
 
73
71
 
data/lib/barney/share.rb CHANGED
@@ -2,10 +2,11 @@ module Barney
2
2
 
3
3
  class Share
4
4
 
5
- # @attr [IO] in The pipe which is used to read data.
6
- # @attr [IO] out The pipe which is used to write data.
5
+ # @attr [Symbol] variable The name of the variable associated with _in_, and _out_.
6
+ # @attr [IO] in The pipe which is used to read data.
7
+ # @attr [IO] out The pipe which is used to write data.
7
8
  # @api private
8
- StreamPair = Struct.new :in, :out
9
+ StreamPair = Struct.new :variable, :in, :out
9
10
 
10
11
  # @attr [Symbol] variable The variable name.
11
12
  # @attr [Object] value The value of the variable.
@@ -14,12 +15,12 @@ module Barney
14
15
  @mutex = Mutex.new
15
16
 
16
17
  class << self
17
- # Returns the latest value read from a spawned child process.
18
+ # Returns the last value read from a spawned child process.
18
19
  # @api private
19
20
  # @return [Object]
20
21
  attr_accessor :value
21
22
 
22
- # Returns a Mutex that is used when {Barney::Share.value Barney::Share.value} is being accessed by {Barney::Share#synchronize}
23
+ # Returns a Mutex that is used by the {Barney::Share#sync} method.
23
24
  # @api private
24
25
  # @return [Mutex]
25
26
  attr_reader :mutex
@@ -33,55 +34,55 @@ module Barney
33
34
  # @return [Fixnum]
34
35
  attr_reader :pid
35
36
 
37
+ # Returns an Array of {HistoryItem} objects.
38
+ # @see HistoryItem
36
39
  # @return [Array<HistoryItem>]
37
40
  attr_reader :history
38
41
 
39
42
  # @yieldparam [Barney::Share] self Yields an instance of {Barney::Share}.
40
43
  # @return [Barney::Share]
41
44
  def initialize
42
- @shared = Hash.new { |h,k| h[k] = [] }
45
+ @streams = []
43
46
  @variables = []
44
47
  @history = []
45
48
  @context = nil
46
49
  yield self if block_given?
47
50
  end
48
51
 
49
- # Serves as a method to mark a variable or constant to be shared between two processes.
50
- # @param [Symbol] Variable Accepts a variable amount of Symbol objects.
52
+ # Marks a variable or constant to be shared between two processes.
53
+ # @param [Symbol] Variable Accepts the name(s) of the variables or constants you want to share.
51
54
  # @return [Array<Symbol>] Returns a list of all variables that are being shared.
52
55
  def share *variables
53
56
  @variables.push *variables.map(&:to_sym)
54
57
  @variables.uniq!
55
58
  end
56
59
 
57
- # Serves as a method to remove a variable or constant from being shared between two processes.
58
- # @param [Symbol] Variable Accepts a variable amount of Symbol objects.
60
+ # Removes a variable or constant from being shared between two processes.
61
+ # @param [Symbol] Variable Accepts the name(s) of the variables or constants you want to stop sharing.
59
62
  # @return [Array<Symbol>] Returns a list of the variables that are still being shared.
60
63
  def unshare *variables
61
- variables.map(&:to_sym).each { |variable| @variables.delete variable }
64
+ variables.map(&:to_sym).each do |variable|
65
+ @streams.delete_if { |stream| stream.variable == variable }
66
+ @variables.delete variable
67
+ end
62
68
  @variables
63
69
  end
64
70
 
65
- # Serves as a method to spawn a new child process.
71
+ # Spawns a child process.
66
72
  # It can be treated like the Kernel.fork method, but a block or Proc object is a required argument.
67
- # @param [Proc] Proc Accepts a block or Proc object that will be executed in a child
68
- # process.
69
- #
70
- # @raise [ArgumentError] It will raise an ArgumentError if a block or Proc object isn't
71
- # supplied as an argument.
72
- #
73
+ # @param [Proc] Proc Accepts a block or Proc object that will be executed in a child process.
74
+ # @raise [ArgumentError] Raises an ArgumentError if a block or Proc object isn't supplied.
73
75
  # @return [Fixnum] Returns the Process ID(PID) of the spawned child process.
74
76
  def fork &blk
75
77
  raise ArgumentError, "A block or Proc object is expected" unless block_given?
76
78
  spawn_pipes
77
79
 
78
80
  @context = blk.binding
79
- @pid = Kernel.fork do
81
+ @pid = Kernel.fork do
80
82
  blk.call
81
- @shared.each do |variable, history|
82
- stream = history[-1]
83
+ @streams.each do |stream|
83
84
  stream.in.close
84
- stream.out.write Marshal.dump(eval("#{variable}", @context))
85
+ stream.out.write Marshal.dump(eval("#{stream.variable}", @context))
85
86
  stream.out.close
86
87
  end
87
88
  end
@@ -89,33 +90,30 @@ module Barney
89
90
  @pid
90
91
  end
91
92
 
92
- # Serves as a method that synchronizes data between the parent and child process.
93
- # It will block until the spawned child process has exited.
93
+ # Synchronizes data between the parent and child process.
94
+ # It will block until the spawned child process has exited.
94
95
  # @return [void]
95
96
  def synchronize
96
97
  Barney::Share.mutex.synchronize do
97
- @shared.each do |variable, history|
98
- history.each do |stream|
99
- stream.out.close
100
- Barney::Share.value = Marshal.load stream.in.read
101
- stream.in.close
102
- value = eval "#{variable} = Barney::Share.value", @context
103
- @history.push HistoryItem.new variable, value
104
- end
98
+ @streams.each do |stream|
99
+ stream.out.close
100
+ Barney::Share.value = Marshal.load stream.in.read
101
+ stream.in.close
102
+ value = eval "#{stream.variable} = Barney::Share.value", @context
103
+ @history.push HistoryItem.new(stream.variable, value)
105
104
  end
105
+ @streams.clear
106
106
  end
107
107
  end
108
108
  alias_method :sync, :synchronize
109
109
 
110
110
  private
111
111
 
112
+ # Manages the creation of pipes used for cross-process communcation.
113
+ # @api private
112
114
  def spawn_pipes
113
- @shared.keep_if do |variable|
114
- @variables.member? variable
115
- end
116
-
117
- @variables.each do |variable|
118
- @shared[variable].push StreamPair.new *IO.pipe
115
+ @variables.each do |variable|
116
+ @streams.push StreamPair.new(variable, *IO.pipe)
119
117
  end
120
118
  end
121
119
 
data/lib/barney.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'barney/share'
2
2
 
3
3
  module Barney
4
- VERSION = '0.8.1'
4
+ VERSION = '0.9.0'
5
5
  end
6
6
 
@@ -90,6 +90,16 @@ describe Barney::Share do
90
90
  assert_equal 5, @instance.history[1].value
91
91
  end
92
92
 
93
+ it 'should assert a IOError (Closed Stream) is not raised.' do
94
+ x = 5
95
+ pids = []
96
+ @instance.share :x
97
+ pids << @instance.fork { }
98
+ @instance.sync
99
+ pids << @instance.fork { }
100
+ pids.each { |pid| Process.wait pid }
101
+ end
102
+
93
103
  end
94
104
 
95
105
  end
@@ -31,11 +31,11 @@ describe Barney::Share do
31
31
  x = 5
32
32
  @instance.share :x
33
33
  @instance.fork { }
34
- assert true, @instance.instance_variable_get(:@shared).has_key?(:x)
34
+ assert true, @instance.instance_variable_get(:@streams).first.variable == :x
35
35
 
36
36
  @instance.unshare :x
37
37
  @instance.fork { }
38
- assert true, @instance.instance_variable_get(:@shared).empty?
38
+ assert_equal true, @instance.instance_variable_get(:@streams).empty?
39
39
  end
40
40
 
41
41
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: barney
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.1
5
+ version: 0.9.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Robert Gleeson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-19 00:00:00 +01:00
13
+ date: 2011-04-21 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency