[ANN] state-0.4.2

A

ara howard

NAME
state

SYNOPSIS
platform agnostic persistent state for ruby processes based on sqlite

DESCRIPTION
state provides an extremely simple to use state mechanism for ruby
programs
which require state between invocations. the storage is based on
sqlite and
is therefore platform agnostic. using state is no more difficult
that using
a hash - only that hash will persist between invocations your ruby
program.
see the samples and specs for details.

INSTALL
gem install state

URIS
http://codeforpeople.com/lib/ruby/
http://rubyforge.org/projects/codeforpeople/

SAMPLES

<========< sample/a.rb >========>

~ > cat sample/a.rb

require 'state'

# state provides persistent state for processes. usage requires
simply
# accesses the state in a hash-like way.
#

# one process can create state
#
child do
State.clear
State['key'] = 'value'
end

# and later processes can have access to it
#
2.times do |i|
child do
value = State['key']
puts "child[#{ i }] => #{ value.inspect }"
end
end


BEGIN {
# we use fork just for demonstation, but this works on windows
too ;-)
#
def child &block
Process.waitpid fork(&block)
end
}


~ > ruby sample/a.rb

child[0] => "value"
child[1] => "value"


<========< sample/b.rb >========>

~ > cat sample/b.rb

require 'state'

# state will store it's db in a subdirectory (.state) of your
home directory,
# the default database is ~/.state/global, but you may specify a
name to
# create a new database
#

db = State.for 'foobar'

db.clear

puts db.path

10.times{|i| db = i}

puts db.keys.inspect

~ > ruby sample/b.rb

/Users/ahoward/.state/foobar
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


<========< sample/c.rb >========>

~ > cat sample/c.rb

require 'state'

# of course you can specify and absolute path
#

db = State.for :path => '/tmp/state'

db.clear

puts db.path

~ > ruby sample/c.rb

/tmp/state


<========< sample/d.rb >========>

~ > cat sample/d.rb

require 'state'

# in general the interface for state is like that of a hash, see
the specs for
# more details

db = State.for 'foobar'

db.clear

10.times{|i| db = i**2}
5.times{|i| db.delete i}

p db.keys
p db.values

# use the update method for atomic read-update of a key/val pair

db['key'] = 42

p :current => db['key']

db.update 'key' do |old|
p :eek:ld => old
new = 42.0
end

p :update => db['key']



~ > ruby sample/d.rb

[5, 6, 7, 8, 9]
[25, 36, 49, 64, 81]
{:current=>42}
{:eek:ld=>42}
{:update=>42.0}


HISTORY
0.4.2
initial version

AUTHORS
ara.t.howard




a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[ANN] terminator-0.4.2 0
[ANN] shared-0.4.2 8
[ANN] openobject-0.0.1 12
ANN main-4.4.0 0
[ANN] forkoff-1.1.0 0
[ANN] forkoff-0.0.4 0
[ANN] prototype-1.0.0 0
[ANN] main-2.8.3 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top