Skip to content

Commit

Permalink
CompositeStorage rework (Thanks Greg Lazarev)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Apr 16, 2011
1 parent 362b190 commit a1f36b4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -4,6 +4,7 @@

== ruote - 2.2.1 not yet released

- CompositeStorage#delete_schedule fix (Thanks Greg)
- read 'http://'|'file', :to => 'f:a' / :to => 'v:b'
- save :to => 'f:a' / :to => 'v:b'
- pause(fei/wfid), resume(fei/wfid)
Expand Down
1 change: 1 addition & 0 deletions CREDITS.txt
Expand Up @@ -44,6 +44,7 @@ Richard Jennings

== Feedback

Greg Lazarev - composite storage issues
Jan Topiński - https://github.com/simcha
Iuri Gagnidze - ProcessStatus#definition_name issues, Engine#leftovers
John Le - https://github.com/sandbox
Expand Down
95 changes: 41 additions & 54 deletions lib/ruote/storage/composite_storage.rb
Expand Up @@ -51,58 +51,55 @@ def initialize(default_storage, storages)

@default_storage = default_storage
@storages = storages

prepare_base_methods
end

def put(doc, opts={})

storage(doc['type']).put(doc, opts)
end

def get(type, key)

storage(type).get(type, key)
end

def delete(doc)

storage(doc['type']).delete(doc)
end

def get_many(type, key=nil, opts={})

storage(type).get_many(type, key, opts)
end

def ids(type)

storage(type).ids(type)
end

def purge!
# A class method 'delegate', to tell this storage how to deal with
# each method composing a storage.
#
# Followed by a list of 'delegations'.
#
def self.delegate(method_name, type=nil)

TYPES.collect { |t| storage(t) }.uniq.each { |s| s.purge! }
if type == nil
define_method(method_name) do |*args|
storage(args.first['type']).send(method_name, *args)
end
elsif type.is_a?(Fixnum)
define_method(method_name) do |*args|
storage(args[type]).send(method_name, *args)
end
else
type = type.to_s
define_method(method_name) do |*args|
storage(type).send(method_name, *args)
end
end
end

def purge_type!(type)

storage(type).purge_type!(type)
end
delegate :put
delegate :get, 0
delegate :get_many, 0
delegate :delete

delegate :reserve
delegate :ids, 0
delegate :purge_type!, 0
delegate :empty?, 0

delegate :put_msg, :msgs
delegate :get_msgs, :msgs
delegate :put_schedule, :schedules
delegate :get_schedules, :schedules
delegate :delete_schedule, :schedules
delegate :find_root_expression, :expressions
delegate :expression_wfids, :expressions
delegate :get_trackers, :variables
delegate :get_engine_variable, :variables
delegate :put_engine_variable, :variables

#def add_type (type)
#end

protected

STORAGE_BASE_METHODS = {
'put_msg' => 'msgs',
'get_msgs' => 'msgs',
'find_root_expression' => 'expressions',
'get_schedules' => 'schedules',
'put_schedule' => 'schedules'
}

TYPES = %w[
variables
msgs
Expand All @@ -113,17 +110,7 @@ def purge_type!(type)
workitems
]

def prepare_base_methods

singleton = class << self; self; end

STORAGE_BASE_METHODS.each do |method, type|

singleton.send(:define_method, method) do |*args|
storage(type).send(method, *args)
end
end
end
protected

def storage(type)

Expand Down
26 changes: 26 additions & 0 deletions test/unit/ut_20_composite_storage.rb
Expand Up @@ -44,5 +44,31 @@ def test_delete
assert_nil r
assert_equal 0, @default.h['msgs'].size
end

class TracingStorage
attr_reader :trace
def initialize
@trace = []
end
def method_missing(m, *args)
@trace << [ m, *args ]
end
end

def test_special_methods

default = TracingStorage.new

cs = Ruote::CompositeStorage.new(default, {})

cs.delete_schedule('x') # schedule id
cs.reserve('type' => 'schedules', '_id' => 'nada')

assert_equal([
[ :delete_schedule, 'x' ],
[ :reserve, { 'type' => 'schedules', '_id' => 'nada' } ]
],
default.trace)
end
end

0 comments on commit a1f36b4

Please sign in to comment.