README.md in sync_attr-0.1.1 vs README.md in sync_attr-1.0.0
- old
+ new
@@ -1,8 +1,10 @@
sync_attr
=========
+Thread-safe Ruby class variables with lazy loaded default values and initializers
+
* http://github.com/ClarityServices/sync_attr
### Introduction
When working in a multithreaded environment it is important to ensure that
@@ -10,11 +12,11 @@
that inconsistent data is not created.
For example, without sync_attr if two threads attempt to write to the
same attribute at the same time it is not deterministic what the results will be.
This condition is made worse when two threads attempt to initialize class variables
-at the same time that could take a second or longer to complete.
+at the same time that could take a second or longer to complete.
### Features
* Adds thread-safe accessors for class attributes
* Allows shared read access to class and instance attributes. This allows
@@ -30,32 +32,61 @@
* Avoids costly startup initialization when the initialized data may never be accessed
For example when Rake tasks are run, they may not need access to everything in
the Rails environment
* Not dependent on Rails
-### Examples
+### Synchronized Class Attribute example
require 'sync_attr'
# Sample class with lazy initialized Synchronized Class Attributes
- def Person
+ class Person
include SyncAttr
# Thread safe Class Attribute reader for name
- # Sets :name only when it is first called
+ # with a default value
# Ideal for when name is loaded after startup from a database or config file
sync_cattr_reader :name do
"Joe Bloggs"
end
# Thread safe Class Attribute reader and writer for age
- # Sets :age only when it is first called
+ # with a default value
sync_cattr_accessor :age do
21
end
end
+ puts "The person is #{Person.name} with age #{Person.age}"
+
+ Person.age = 22
+ puts "The person is #{Person.name} now has age #{Person.age}"
+
+ Person.age = Proc.new {|age| age += 1 }
+ puts "The person is #{Person.name} now has age #{Person.age}"
+
+### Synchronized Instance Attribute example
+
+ require 'sync_attr'
+
+ # Sample class with lazy initialized Synchronized Class Attributes
+ class Person
+ include SyncAttr
+
+ # Thread safe Attribute reader for name
+ # with a default value
+ sync_attr_reader :name do
+ "Joe Bloggs"
+ end
+
+ # Thread safe Attribute reader and writer for age
+ # with a default value
+ sync_attr_accessor :age do
+ 21
+ end
+ end
+
person = Person.new
puts "The person is #{person.name} with age #{person.age}"
person.age = 22
puts "The person is #{person.name} now has age #{person.age}"
@@ -70,10 +101,9 @@
Meta
----
* Code: `git clone git://github.com/ClarityServices/sync_attr.git`
* Home: <https://github.com/ClarityServices/sync_attr>
-* Docs: <http://ClarityServices.github.com/sync_attr/>
* Bugs: <http://github.com/reidmorrison/sync_attr/issues>
* Gems: <http://rubygems.org/gems/sync_attr>
This project uses [Semantic Versioning](http://semver.org/).