sync_attr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2012 Clarity Services, Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ sync_attr
2
+ =========
3
+
4
+ * http://github.com/ClarityServices/sync_attr
5
+
6
+ ### Introduction
7
+
8
+ When working in a multithreaded environment it is important to ensure that
9
+ any attributes that are shared across threads are properly protected to ensure
10
+ that inconsistent data is not created.
11
+
12
+ For example, without sync_attr if two threads attempt to write to the
13
+ same attribute at the same time it is not deterministic what the results will be.
14
+ This condition is made worse when two threads attempt to initialize class variables
15
+ at the same time that could take a second or longer to complete.
16
+
17
+ ### Features
18
+
19
+ * Adds thread-safe accessors for class attributes
20
+ * Allows shared read access to class and instance attributes. This allows
21
+ multiple threads to read the attribute, but will block all reads and writes whilst
22
+ the attribute is being modified.
23
+ * Prevents attributes from being read while it is being updated or initialized for
24
+ the first time.
25
+ * Thread-safe attribute lazy initialization
26
+ Lazy initialization allows class attributes to be loaded only when first read.
27
+ As a result it's value can be read for the first time from a database or config file
28
+ once and only when needed.
29
+ * Avoids having to create yet another Rails initializer
30
+ * Avoids costly startup initialization when the initialized data may never be accessed
31
+ For example when Rake tasks are run, they may not need access to everything in
32
+ the Rails environment
33
+ * Not dependent on Rails
34
+
35
+ ### Examples
36
+
37
+ require 'sync_attr'
38
+
39
+ # Sample class with lazy initialized Synchronized Class Attributes
40
+ def Person
41
+ include SyncAttr
42
+
43
+ # Thread safe Class Attribute reader for name
44
+ # Sets :name only when it is first called
45
+ # Ideal for when name is loaded after startup from a database or config file
46
+ sync_cattr_reader :name do
47
+ "Joe Bloggs"
48
+ end
49
+
50
+ # Thread safe Class Attribute reader and writer for age
51
+ # Sets :age only when it is first called
52
+ sync_cattr_accessor :age do
53
+ 21
54
+ end
55
+ end
56
+
57
+ person = Person.new
58
+ puts "The person is #{person.name} with age #{person.age}"
59
+
60
+ person.age = 22
61
+ puts "The person is #{person.name} now has age #{person.age}"
62
+
63
+ person.age = Proc.new {|age| age += 1 }
64
+ puts "The person is #{person.name} now has age #{person.age}"
65
+
66
+ ### Install
67
+
68
+ gem install sync_attr
69
+
70
+ Meta
71
+ ----
72
+
73
+ * Code: `git clone git://github.com/ClarityServices/sync_attr.git`
74
+ * Home: <https://github.com/ClarityServices/sync_attr>
75
+ * Docs: <http://ClarityServices.github.com/sync_attr/>
76
+ * Bugs: <http://github.com/reidmorrison/sync_attr/issues>
77
+ * Gems: <http://rubygems.org/gems/sync_attr>
78
+
79
+ This project uses [Semantic Versioning](http://semver.org/).
80
+
81
+ Authors
82
+ -------
83
+
84
+ Reid Morrison :: reidmo@gmail.com :: @reidmorrison
85
+
86
+ License
87
+ -------
88
+
89
+ Copyright 2012 Clarity Services, Inc.
90
+
91
+ Licensed under the Apache License, Version 2.0 (the "License");
92
+ you may not use this file except in compliance with the License.
93
+ You may obtain a copy of the License at
94
+
95
+ http://www.apache.org/licenses/LICENSE-2.0
96
+
97
+ Unless required by applicable law or agreed to in writing, software
98
+ distributed under the License is distributed on an "AS IS" BASIS,
99
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
100
+ See the License for the specific language governing permissions and
101
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'date'
7
+ require 'sync_attr/version'
8
+
9
+ desc "Build gem"
10
+ task :gem do |t|
11
+ gemspec = Gem::Specification.new do |s|
12
+ s.name = 'sync_attr'
13
+ s.version = SyncAttr::VERSION
14
+ s.platform = Gem::Platform::RUBY
15
+ s.authors = ['Reid Morrison']
16
+ s.email = ['reidmo@gmail.com']
17
+ s.homepage = 'https://github.com/ClarityServices/sync_attr'
18
+ s.date = Date.today.to_s
19
+ s.summary = "Thread safe accessors for Ruby class and instance attributes. Supports thread safe lazy loading of attributes"
20
+ s.description = "SyncAttr is a mixin to read, write and lazy initialize both class and instance variables in a multi-threaded environment when the attribute could be modified by two threads at the same time, written in Ruby."
21
+ s.files = FileList["./**/*"].exclude('*.gem', 'nbproject').map{|f| f.sub(/^\.\//, '')}
22
+ s.has_rdoc = true
23
+ end
24
+ Gem::Builder.new(gemspec).build
25
+ end
26
+
27
+ desc "Run Test Suite"
28
+ task :test do
29
+ Rake::TestTask.new(:unit) do |t|
30
+ t.test_files = FileList['test/*_test.rb']
31
+ t.verbose = true
32
+ end
33
+
34
+ Rake::Task['unit'].invoke
35
+ end
@@ -0,0 +1,31 @@
1
+ # Class Attribute Example
2
+ #
3
+ require 'sync_attr'
4
+
5
+ # Sample class with lazy initialized Synchronized Class Attributes
6
+ def Person
7
+ include SyncAttr
8
+
9
+ # Thread safe Class Attribute reader for name
10
+ # Sets :name only when it is first called
11
+ # Ideal for when name is loaded after startup from a database or config file
12
+ sync_cattr_reader :name do
13
+ "Joe Bloggs"
14
+ end
15
+
16
+ # Thread safe Class Attribute reader and writer for age
17
+ # Sets :age only when it is first called
18
+ sync_cattr_accessor :age do
19
+ 21
20
+ end
21
+ end
22
+
23
+ person = Person.new
24
+ puts "The person is #{person.name} with age #{person.age}"
25
+
26
+ person.age = 22
27
+ puts "The person is #{person.name} now has age #{person.age}"
28
+
29
+ person.age = Proc.new {|age| age += 1 }
30
+ puts "The person is #{person.name} now has age #{person.age}"
31
+
@@ -0,0 +1,87 @@
1
+ # Synchronize access and lazy initialize one or more variables in a class
2
+ #
3
+ # Author: Reid Morrison <reidmo@gmail.com>
4
+ module SyncAttr
5
+ module ClassAttributes
6
+ module ClassMethods
7
+ # Lazy load the specific class attribute by calling the supplied block when
8
+ # the attribute is first read and then return the same value for all subsequent
9
+ # calls to the class variable
10
+ #
11
+ # An optional block can be supplied to initialize the synchronized class attribute
12
+ # when first read. Acts as a thread safe lazy initializer. The block will only
13
+ # be called once even if several threads call the reader at the same time
14
+ #
15
+ # Example:
16
+ # class MyClass
17
+ # include SyncAttr
18
+ #
19
+ # # Generates a reader for the class attribute 'hello'
20
+ # # and Lazy initializes the value to 'hello world' only on the first
21
+ # # call to the reader
22
+ # sync_cattr_reader :hello do
23
+ # 'hello world'
24
+ # end
25
+ # end
26
+ def sync_cattr_reader(*attributes, &block)
27
+ attributes.each do |attribute|
28
+ self.class.send(:define_method, attribute.to_sym) do
29
+ var_name = "@@#{attribute}".to_sym
30
+ if class_variable_defined?(var_name)
31
+ sync_attr_sync.synchronize(:SH) { class_variable_get(var_name) }
32
+ else
33
+ return nil unless block
34
+ sync_attr_sync.synchronize(:EX) do
35
+ # Now that we have exclusive access make sure that another thread has
36
+ # not just initialized this attribute
37
+ if class_variable_defined?(var_name)
38
+ class_variable_get(var_name)
39
+ else
40
+ class_variable_set(var_name, class_eval(&block))
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # Generates a writer to set a synchronized attribute
49
+ # Supply a Proc ensure an attribute is not being updated by another thread:
50
+ # MyClass.count = Proc.new {|count| (count||0) + 1}
51
+ def sync_cattr_writer(*attributes)
52
+ attributes.each do |attribute|
53
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
54
+ def self.#{attribute}=(value)
55
+ sync_attr_sync.synchronize(:EX) do
56
+ if value.is_a?(Proc)
57
+ current_value = @@#{attribute} if defined?(@@#{attribute})
58
+ @@#{attribute} = value.call(current_value)
59
+ else
60
+ @@#{attribute} = value
61
+ end
62
+ end
63
+ end
64
+ EOS
65
+ end
66
+ end
67
+
68
+ # Generate a class reader and writer for the attribute
69
+ def sync_cattr_accessor(*attributes, &block)
70
+ sync_cattr_reader(*attributes, &block)
71
+ sync_cattr_writer(*attributes)
72
+ end
73
+
74
+ private
75
+
76
+ # Give each class that this module is mixed into it's own Sync
77
+ def sync_attr_class_attr_init
78
+ @sync_attr_sync = ::Sync.new
79
+ end
80
+
81
+ def sync_attr_sync
82
+ @sync_attr_sync
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module SyncAttr #:nodoc
2
+ VERSION = "0.0.1"
3
+ end
data/lib/sync_attr.rb ADDED
@@ -0,0 +1,33 @@
1
+ # Synchronize access and lazy initialize one or more variables in a class
2
+ #
3
+ # Load configuration files and thread connection pools on demand rather than
4
+ # at start time or via a Rails initializer
5
+ #
6
+ # Locking: Shared reads and exclusive writes
7
+ # sync_attr ensures that all reads are shared, meaning that all
8
+ # reads to attributes can occur at the same time. All writes are exclusive, so
9
+ # all reads and other writes will be blocked whilst a write takes place.
10
+ #
11
+ # Example:
12
+ # class MyClass
13
+ # include SyncAttr
14
+ #
15
+ # # Create class variable @@http and initialize on the first access
16
+ # # protecting access by concurrent threads using a Semaphore
17
+ # sync_cattr_reader :http do
18
+ # PersistentHTTP.new()
19
+ # end
20
+ #
21
+ # Author: Reid Morrison <reidmo@gmail.com>
22
+ require 'sync'
23
+ require 'sync_attr/version'
24
+ require 'sync_attr/class_attributes'
25
+ #require 'sync_attr/instance_attributes'
26
+
27
+ module SyncAttr
28
+ # Add class methods and initialize mixin
29
+ def self.included(base)
30
+ base.extend(SyncAttr::ClassAttributes::ClassMethods)
31
+ base.send(:sync_attr_class_attr_init)
32
+ end
33
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ file.reference.synch_attr-examples=/Users/rmorrison/Sandbox/synch_attr/examples
2
+ file.reference.synch_attr-lib=/Users/rmorrison/Sandbox/synch_attr/lib
3
+ file.reference.synch_attr-test=/Users/rmorrison/Sandbox/synch_attr/test
4
+ platform.active=JRuby_0
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3
+ <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
4
+ </project-private>
File without changes
@@ -0,0 +1,10 @@
1
+ file.reference.synch_attr-examples=examples
2
+ file.reference.synch_attr-lib=lib
3
+ file.reference.synch_attr-test=test
4
+ javac.classpath=
5
+ main.file=
6
+ platform.active=JRuby_0
7
+ source.encoding=UTF-8
8
+ src.examples.dir=examples
9
+ src.lib.dir=lib
10
+ test.test.dir=test
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://www.netbeans.org/ns/project/1">
3
+ <type>org.netbeans.modules.ruby.rubyproject</type>
4
+ <configuration>
5
+ <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
6
+ <name>sync_attr</name>
7
+ <source-roots>
8
+ <root id="src.lib.dir" name="Source Files"/>
9
+ <root id="src.examples.dir" name="Examples"/>
10
+ </source-roots>
11
+ <test-roots>
12
+ <root id="test.test.dir" name="Test Files"/>
13
+ </test-roots>
14
+ </data>
15
+ </configuration>
16
+ </project>
@@ -0,0 +1,75 @@
1
+ # Allow examples to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'sync_attr'
8
+
9
+ class SynchAttrExample
10
+ include SyncAttr
11
+
12
+ sync_cattr_reader :test do
13
+ 'hello world'
14
+ end
15
+ sync_cattr_reader :test2
16
+ sync_cattr_accessor :test3
17
+ sync_cattr_accessor :test4 do
18
+ 'hello world 4'
19
+ end
20
+
21
+ sync_cattr_accessor :test5
22
+ end
23
+
24
+ class SynchAttrExample2
25
+ include SyncAttr
26
+
27
+ sync_cattr_reader :test do
28
+ 'hello world'
29
+ end
30
+ end
31
+
32
+ class ClassAttributesTest < Test::Unit::TestCase
33
+ context "with example" do
34
+
35
+ should 'lazy initialize class attribute' do
36
+ assert_equal 'hello world', SynchAttrExample.test
37
+ end
38
+
39
+ should 'return nil on class attribute without initializer' do
40
+ assert_nil SynchAttrExample.test2
41
+ end
42
+
43
+ should 'set and then return a value for a class attribute without an initializer' do
44
+ assert_nil SynchAttrExample.test3
45
+ assert_equal 'test3', (SynchAttrExample.test3 = 'test3')
46
+ assert_equal 'test3', SynchAttrExample.test3
47
+ end
48
+
49
+ should 'lazy initialize class attribute and also have writer' do
50
+ assert_equal 'hello world 4', SynchAttrExample.test4
51
+ assert_equal 'test4', (SynchAttrExample.test4 = 'test4')
52
+ assert_equal 'test4', SynchAttrExample.test4
53
+ end
54
+
55
+ should 'support setting a Proc within a synch block' do
56
+ assert_nil SynchAttrExample.test5
57
+
58
+ # Returns the Proc
59
+ SynchAttrExample.test5 = Proc.new {|val| (val||0) + 1}
60
+ assert_equal 1, SynchAttrExample.test5
61
+
62
+ SynchAttrExample.test5 = Proc.new {|val| (val||0) + 1}
63
+ assert_equal 2, SynchAttrExample.test5
64
+ end
65
+ end
66
+
67
+ context "with example2" do
68
+
69
+ should 'ensure that different classes have their own synch instances' do
70
+ assert ex1 = SynchAttrExample.new
71
+ assert ex2 = SynchAttrExample2.new
72
+ assert ex1.class.send(:sync_attr_sync).object_id != ex2.class.send(:sync_attr_sync).object_id
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sync_attr
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Reid Morrison
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-02-20 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: SyncAttr is a mixin to read, write and lazy initialize both class and instance variables in a multi-threaded environment when the attribute could be modified by two threads at the same time, written in Ruby.
18
+ email:
19
+ - reidmo@gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - LICENSE.txt
28
+ - Rakefile
29
+ - README.md
30
+ - examples/class_attribute.rb
31
+ - lib/sync_attr.rb
32
+ - lib/sync_attr/class_attributes.rb
33
+ - lib/sync_attr/version.rb
34
+ - nbproject/project.properties
35
+ - nbproject/project.xml
36
+ - nbproject/private/config.properties
37
+ - nbproject/private/private.properties
38
+ - nbproject/private/private.xml
39
+ - nbproject/private/rake-d.txt
40
+ - test/class_attributes_test.rb
41
+ has_rdoc: true
42
+ homepage: https://github.com/ClarityServices/sync_attr
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.5.1
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Thread safe accessors for Ruby class and instance attributes. Supports thread safe lazy loading of attributes
69
+ test_files: []
70
+