interprocess_attribute 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +2 -3
- data/interprocess_attribute.gemspec +1 -1
- data/lib/interprocess_attribute.rb +27 -25
- data/test/interprocess_attribute_test.rb +8 -0
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,7 @@ __OVERVIEW__
|
|
12
12
|
__DESCRIPTION__
|
13
13
|
|
14
14
|
interprocess\_attribute persists attributes between processes. That's to say
|
15
|
-
you can
|
15
|
+
you can set the 'name' attribute to "Rob" in the child process and see that
|
16
16
|
change propagate in the parent process with very little code.
|
17
17
|
|
18
18
|
Attributes are defined through the `interprocess_attribute` method. It
|
@@ -52,8 +52,7 @@ a module you can use with extend(…).
|
|
52
52
|
require "interprocess_attribute"
|
53
53
|
class Person
|
54
54
|
extend InterProcessAttribute
|
55
|
-
interprocess_attribute :name
|
56
|
-
interprocess_attribute :age
|
55
|
+
interprocess_attribute :name,:age
|
57
56
|
end
|
58
57
|
|
59
58
|
person = Person.new
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |gem|
|
3
3
|
gem.name = "interprocess_attribute"
|
4
|
-
gem.version = "0.
|
4
|
+
gem.version = "0.2.0"
|
5
5
|
gem.authors = ["Robert Gleeson"]
|
6
6
|
gem.email = ["rob@flowof.info"]
|
7
7
|
gem.description = %q{Attributes that persist between processes}
|
@@ -1,43 +1,45 @@
|
|
1
1
|
require "ichannel"
|
2
2
|
module InterProcessAttribute
|
3
|
-
#
|
4
|
-
# @param [#to_s] name
|
5
|
-
# The name of the attribute.
|
6
|
-
#
|
7
|
-
# @param [Hash] opts
|
8
|
-
# @option opts [#to_s] :visibility
|
9
|
-
# "public", "protected", or "private"
|
10
3
|
#
|
11
4
|
# @example
|
12
5
|
# class Person
|
13
6
|
# extend InterProcessAttribute
|
14
|
-
# interprocess_attribute :name,
|
15
|
-
#
|
16
|
-
# def initialize
|
17
|
-
# self.name = "Rob"
|
18
|
-
# end
|
7
|
+
# interprocess_attribute :name, :age
|
19
8
|
# end
|
20
9
|
#
|
21
10
|
# person = Person.new
|
22
|
-
#
|
11
|
+
# pid = fork do
|
12
|
+
# person.name = "Rob"
|
13
|
+
# person.age = 27
|
14
|
+
# end
|
15
|
+
# Process.wait pid
|
23
16
|
# p person.name # => "Rob"
|
17
|
+
# p person.age # => 27
|
24
18
|
#
|
25
|
-
def interprocess_attribute(
|
19
|
+
def interprocess_attribute(*args)
|
20
|
+
Hash === args.last ? (opts = args.pop) : (opts = {visibility: "public"})
|
21
|
+
attributes = args
|
22
|
+
if attributes.empty?
|
23
|
+
raise ArgumentError,
|
24
|
+
"Wrong number of arguments (no attribute names given)"
|
25
|
+
end
|
26
26
|
class_eval do
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
attributes.each do |name|
|
28
|
+
channel = IChannel.new Marshal
|
29
|
+
define_method name do
|
30
|
+
while channel.readable?
|
31
|
+
instance_variable_set "@#{name}", channel.get
|
32
|
+
end
|
33
|
+
instance_variable_get "@#{name}"
|
31
34
|
end
|
32
|
-
|
33
|
-
end
|
34
|
-
send opts[:visibility], name.to_sym
|
35
|
+
send opts[:visibility], name.to_sym
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
define_method "#{name}=" do |value|
|
38
|
+
channel.put value
|
39
|
+
instance_variable_set "@#{name}", value
|
40
|
+
end
|
41
|
+
send opts[:visibility], "#{name}=".to_sym
|
39
42
|
end
|
40
|
-
send opts[:visibility], "#{name}=".to_sym
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
@@ -5,6 +5,9 @@ class InterProcessAttributeTest < Test::Unit::TestCase
|
|
5
5
|
interprocess_attribute :age
|
6
6
|
interprocess_attribute :height, {visibility: :private}
|
7
7
|
interprocess_attribute :weight, {visibility: :protected}
|
8
|
+
interprocess_attribute :shoe_size,
|
9
|
+
:surname,
|
10
|
+
{visibility: :private}
|
8
11
|
end
|
9
12
|
|
10
13
|
def test_defaults
|
@@ -58,4 +61,9 @@ class InterProcessAttributeTest < Test::Unit::TestCase
|
|
58
61
|
assert is_protected
|
59
62
|
end
|
60
63
|
|
64
|
+
def test_visibility_with_multiple_args
|
65
|
+
private_methods = Person.private_instance_methods
|
66
|
+
is_private = private_methods.include?(:shoe_size) && private_methods.include?(:surname)
|
67
|
+
assert is_private
|
68
|
+
end
|
61
69
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interprocess_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ichannel
|
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
segments:
|
61
61
|
- 0
|
62
|
-
hash:
|
62
|
+
hash: -2457367772035621150
|
63
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
version: '0'
|
69
69
|
segments:
|
70
70
|
- 0
|
71
|
-
hash:
|
71
|
+
hash: -2457367772035621150
|
72
72
|
requirements: []
|
73
73
|
rubyforge_project:
|
74
74
|
rubygems_version: 1.8.23
|