Skip to content

Commit

Permalink
consume the YAML interchange more cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewrudy committed Feb 12, 2009
1 parent 6c296a5 commit 14319d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 19 additions & 4 deletions lib/rude_q.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ module RudeQ

def self.included(mod) # :nodoc:
mod.extend(ClassMethods)
mod.send(:include, InstanceMethods)
end


module InstanceMethods
def data # :nodoc:
YAML.load(self[:data])
end
def data=(value) # :nodoc:
self[:data] = YAML.dump(value)
end
end

module ClassMethods
# Cleanup old processed items
#
Expand All @@ -36,8 +46,7 @@ def cleanup!(expiry=1.hour)
def set(queue_name, data)
queue_name = sanitize_queue_name(queue_name)

yaml_data = YAML.dump(data)
self.create!(:queue_name => queue_name, :data => yaml_data)
self.create!(:queue_name => queue_name, :data => data)
return nil # in line with Starling
end

Expand All @@ -58,7 +67,7 @@ def get(queue_name)
fetch_with_lock(qname) do |record|
if record
processed!(record)
return YAML.load(record.data)
return record.data
else
return nil # Starling waits indefinitely for a corresponding queue item
end
Expand Down Expand Up @@ -137,6 +146,12 @@ def queue_options
@queue_options ||= {:processed => :set_flag, :lock => :pessimistic}
end

def data # :nodoc:
YAML.load(self[:data])
end
def data=(value) # :nodoc:
self[:data] = YAML.dump(value)
end
private

def sanitize_queue_name(queue_name) # :nodoc:
Expand Down
6 changes: 3 additions & 3 deletions spec/rude_q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_some_noise

describe ".set" do
it "should delegate to :create!" do
ProcessQueue.should_receive(:create!).with(:queue_name => 'abcde', :data => YAML.dump(:magical_planet))
ProcessQueue.should_receive(:create!).with(:queue_name => 'abcde', :data => :magical_planet)
ProcessQueue.set('abcde', :magical_planet)
end
it "should return nil" do
Expand All @@ -107,15 +107,15 @@ def create_some_noise
# confirm the object is in the db
record = ProcessQueue.find(:first, :order => "id DESC")
record.queue_name.should == 'abcde'
record.data.should == YAML.dump(:this_will_remain_unprocessed)
record.data.should == :this_will_remain_unprocessed
record.processed?.should == false
record.token.should == nil

lambda {ProcessQueue.get('abcde')}.should raise_error(RuntimeError)

record.reload
record.queue_name.should == 'abcde'
record.data.should == YAML.dump(:this_will_remain_unprocessed)
record.data.should == :this_will_remain_unprocessed
record.processed?.should == false
record.token.should == nil
end
Expand Down

0 comments on commit 14319d2

Please sign in to comment.