Author: assaf
Date: Wed May 14 02:39:43 2008
New Revision: 656186

URL: http://svn.apache.org/viewvc?rev=656186&view=rev
Log:
Some fixes to how task data is handled in memory.

Modified:
    ode/sandbox/singleshot/app/models/task.rb
    ode/sandbox/singleshot/spec/models/task_spec.rb

Modified: ode/sandbox/singleshot/app/models/task.rb
URL: 
http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/task.rb?rev=656186&r1=656185&r2=656186&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/task.rb (original)
+++ ode/sandbox/singleshot/app/models/task.rb Wed May 14 02:39:43 2008
@@ -28,8 +28,8 @@
     super
     self.description ||= ''
     self.state = 'reserved'
-    self.access_key = MD5.hexdigest(OpenSSL::Random.random_bytes(128))
     self.data ||= {}
+    self.access_key = MD5.hexdigest(OpenSSL::Random.random_bytes(128))
   end
 
   def to_param #:nodoc:
@@ -114,6 +114,33 @@
   # -- View and perform ---
 
 
+  # --- Task data ---
+
+  def data
+    return self[:data] if Hash === self[:data]
+    self[:data] = ActiveSupport::JSON.decode(self[:data] || '')
+  end
+
+  def data=(data)
+    raise ArgumentError, 'Must be a hash or nil' unless Hash === data || 
data.nil?
+    self[:data] = data || {}
+  end
+
+  before_save do |task|
+    task[:data] = task[:data].to_json if task[:data]
+  end
+
+
+  # --- Stakeholders ---
+  
+  # Stakeholders and people (as stakeholders) associated with this task.
+  has_many :stakeholders, :include=>:person, :dependent=>:delete_all
+  attr_protected :stakeholders
+ 
+  include Stakeholder::Accessors
+  include Stakeholder::Validation
+
+ 
   # --- Completion and cancellation ---
 
   validates_url :outcome_url, :if=>:outcome_url
@@ -155,33 +182,6 @@
   end
 
 
-  # --- Task data ---
-
-  def data
-    @data ||= (ActiveSupport::JSON.decode(self[:data] || '') || 
{}).with_indifferent_access
-  end
-
-  def data=(data)
-    raise ArgumentError, 'Must be a hash or nil' unless Hash === data || 
data.nil?
-    @data = data
-  end
-
-  before_save :serialize_data
-  def serialize_data
-    self[:data], @data = @data.to_json, nil if @data
-  end
-  private :serialize_data
-
-
-  # --- Stakeholders ---
-
-  # Stakeholders and people (as stakeholders) associated with this task.
-  has_many :stakeholders, :include=>:person, :dependent=>:delete_all
-  attr_protected :stakeholders
- 
-  include Stakeholder::Accessors
-  include Stakeholder::Validation
-
 
   # --- Access control ---
 

Modified: ode/sandbox/singleshot/spec/models/task_spec.rb
URL: 
http://svn.apache.org/viewvc/ode/sandbox/singleshot/spec/models/task_spec.rb?rev=656186&r1=656185&r2=656186&view=diff
==============================================================================
--- ode/sandbox/singleshot/spec/models/task_spec.rb (original)
+++ ode/sandbox/singleshot/spec/models/task_spec.rb Wed May 14 02:39:43 2008
@@ -395,39 +395,31 @@
   include Specs::Tasks
 
   before :each do
-    @task = Task.create!(default_task)
+  #  @task = Task.create!(default_task)
   end
 
-  it 'should return hash' do
-    @task.data.should be_kind_of(Hash)
+  it 'should be empty hash by default' do
+    Task.new.data.should == {}
   end
 
-  it 'should accept hash' do
-    @task.data = { }
+  it 'should return nothing for new task' do
+    Task.create default_task.except(:data)
+    Task.first.data.should == {}
   end
 
-  it 'should accept nil' do
-    @task.data = nil
-    @task.data.should == {}
+  it 'should accept argument from mass assignment' do
+    Task.create default_task
+    lambda { Task.first.update_attributes :data=>{'foo'=>'bar'} }.should 
change { Task.first.data }.to('foo'=>'bar')
   end
 
-  it 'should be hash with indifferent access' do
-    @task.update_attributes! :data=>{ :foo=>1 }
-    Task.find(@task.id).data[:foo].should be(1)
-    Task.find(@task.id).data['foo'].should be(1)
+  it 'should accept nil' do
+    Task.create default_task.merge(:data=>{'foo'=>'bar'})
+    lambda { Task.first.update_attributes :data=>nil }.should change { 
Task.first.data }.to({})
   end
 
   it 'should reject any other value' do
-    lambda { @task.data = [] }.should raise_error(ArgumentError)
-    lambda { @task.data = 'string' }.should raise_error(ArgumentError)
-  end
-
-  it 'should store changes' do
-    lambda { @task.update_attributes! :data=>{:foo=>1} }.should change { 
@task.id && Task.find(@task.id).data }.to('foo'=>1)
-    lambda do
-      @task.data[:bar] = 2
-      @task.save
-    end.should change { @task.id && Task.find(@task.id).data.to_hash 
}.to('foo'=>1, 'bar'=>2)
+    lambda { Task.create default_task.merge(:data=>[]) }.should 
raise_error(ArgumentError)
+    lambda { Task.create default_task.merge(:data=>'string') }.should 
raise_error(ArgumentError)
   end
 
 end


Reply via email to