ichannel 2.0.0 → 3.0.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 ADDED
@@ -0,0 +1,4 @@
1
+ -m markdown -M redcarpet
2
+ --
3
+ README.md
4
+ LICENSE.txt
data/README.md CHANGED
@@ -17,8 +17,7 @@ This works across any Ruby process & its subprocesses, though, which is why it
17
17
  can be useful. You could describe putting and getting objects to/from the channel
18
18
  as message passing but the message could potentially be any Ruby object,
19
19
  although I think sending small messages(as a Hash) works very well for most
20
- scenarios. You have the potential to serialize any object that `Marshal` can
21
- dump+load, though.
20
+ scenarios.
22
21
 
23
22
  __EXAMPLES__
24
23
 
@@ -1,3 +1,3 @@
1
1
  class IChannel
2
- VERSION = "2.0.0"
2
+ VERSION = "3.0.0"
3
3
  end
data/lib/ichannel.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'socket'
2
2
  class IChannel
3
3
  #
4
- # @param [#dump,#load}] serializer
4
+ # @param [#dump,#load] serializer
5
5
  # Any object that implements dump, & load.
6
6
  #
7
7
  def initialize(serializer)
@@ -33,39 +33,82 @@ class IChannel
33
33
  #
34
34
  # Add an object to the channel.
35
35
  #
36
- # @raise [IOError]
37
- # When the channel cannot be written to.
36
+ # @raise [IOError]
37
+ # When the channel is closed.
38
38
  #
39
39
  # @param [Object] object
40
40
  # An object to add to the channel.
41
41
  #
42
42
  def write(object)
43
+ if @writer.closed?
44
+ raise IOError, 'The channel cannot be written to (closed).'
45
+ end
46
+ _, writable, _ = IO.select [], [@writer], []
47
+ writable[0].send @serializer.dump(object), 0
48
+ end
49
+ alias_method :put, :write
50
+
51
+ #
52
+ # Add an object to the channel.
53
+ #
54
+ # Unlike {#put}, which waits indefinitely until the channel becomes writable,
55
+ # this method will raise an IOError if 0.1 seconds elapse and the channel
56
+ # remains unwritable.
57
+ #
58
+ # @raise [IOError]
59
+ # When 0.1 seconds elapse and the channel remains unwritable.
60
+ #
61
+ # @param (see IChannel#put).
62
+ #
63
+ def write!(object)
43
64
  _, writable, _ = IO.select [], [@writer], [], 0.1
44
65
  if writable
45
- @writer.send @serializer.dump(object), 0
46
- else
47
- raise IOError, 'The channel cannot be written to.'
66
+ writable[0].send @serializer.dump(@object), 0
48
67
  end
49
68
  end
50
- alias_method :put, :write
69
+ alias_method :put!, :write
51
70
 
52
71
  #
53
72
  # Receive a object from the channel.
54
73
  #
55
74
  # @raise [IOError]
56
- # When the channel cannot be read from.
75
+ # When the channel is closed.
57
76
  #
58
77
  # @return [Object]
59
- # The object added to the channel.
60
- #
78
+ # The object read from the channel.
79
+ #
61
80
  def recv
62
- readable, _ = IO.select [@reader], [], [], 0.1
81
+ if @reader.closed?
82
+ raise IOError, 'The channel cannot be read from (closed).'
83
+ end
84
+ readable, _ = IO.select [@reader], [], []
85
+ msg, _ = readable[0].recvmsg
86
+ @serializer.load msg
87
+ end
88
+ alias_method :get, :recv
89
+
90
+ #
91
+ # Receive a object from the channel.
92
+ #
93
+ # Unlike {#get}, which waits indefinitely until the channel becomes readable,
94
+ # this method will raise an IOError if 0.1 seconds elapse and the channel
95
+ # remains unreadable.
96
+ #
97
+ # @raise [IOError]
98
+ # When 0.1 seconds elapse and the channel remains unreadable.
99
+ #
100
+ # @return [Object]
101
+ # The object read from the channel.
102
+ #
103
+ def recv!
104
+ readble, _ = IO.select [@reader], [], []
63
105
  if readable
64
- msg, _ = @reader.recvmsg
106
+ msg, _ = readable[0].recvmsg
65
107
  @serializer.load msg
66
108
  else
67
- raise IOError, 'The channel cannot be read from.'
109
+ raise IOError, 'The channel cannot be read from.'
68
110
  end
69
111
  end
70
- alias_method :get, :recv
112
+ alias_method :get!, :recv!
113
+
71
114
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'setup'
2
2
  class IChannelTest < Test::Unit::TestCase
3
3
  def setup
4
- @channel = IChannel.new Marshal
4
+ @channel = IChannel.new [YAML, Marshal, JSON].sample
5
5
  end
6
6
 
7
7
  def teardown
data/test/setup.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  require 'ichannel'
2
+ require 'yaml'
3
+ require 'json'
2
4
  require 'test/unit'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ichannel
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
22
  - .travis.yml
23
+ - .yardopts
23
24
  - Gemfile
24
25
  - LICENSE.txt
25
26
  - README.md
@@ -43,7 +44,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
44
  version: '0'
44
45
  segments:
45
46
  - 0
46
- hash: -795267943345486764
47
+ hash: 3779638339953442798
47
48
  required_rubygems_version: !ruby/object:Gem::Requirement
48
49
  none: false
49
50
  requirements:
@@ -52,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
53
  version: '0'
53
54
  segments:
54
55
  - 0
55
- hash: -795267943345486764
56
+ hash: 3779638339953442798
56
57
  requirements: []
57
58
  rubyforge_project:
58
59
  rubygems_version: 1.8.23