Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
When should a resource be immutable?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Zhi-Qiang Lei  
View profile  
 More options Jan 10 2011, 10:23 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Mon, 10 Jan 2011 23:23:52 +0800
Local: Mon, Jan 10 2011 10:23 am
Subject: When should a resource be immutable?
Dear All,

Could anyone answer me when a resource should be immutable? And why? Thanks.

Best regards,
Zhi-Qiang Lei
zhiqiang....@gmail.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piotr Solnica  
View profile  
 More options Jan 10 2011, 10:32 am
From: Piotr Solnica <piotr.soln...@gmail.com>
Date: Mon, 10 Jan 2011 07:32:45 -0800 (PST)
Local: Mon, Jan 10 2011 10:32 am
Subject: Re: When should a resource be immutable?
Hey,

A resource becomes immutable after you destroy it. This prevents from
any further attempts on saving the resource or modifying its
attributes.

Cheers

# solnic

On Jan 10, 4:23 pm, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ted Han  
View profile  
 More options Jan 10 2011, 10:34 am
From: Ted Han <t...@knowtheory.net>
Date: Mon, 10 Jan 2011 10:34:12 -0500
Local: Mon, Jan 10 2011 10:34 am
Subject: Re: [DataMapper] When should a resource be immutable?

Objects are immutable when they've been deleted or frozen for some reason.
 Deleting a record from your data store will always result in that record
being frozen.

Is there a specific problem that you're having?

-T

On Mon, Jan 10, 2011 at 10:23 AM, Zhi-Qiang Lei <zhiqiang....@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Stott  
View profile  
 More options Jan 10 2011, 10:39 am
From: Jonathan Stott <jonathan.st...@gmail.com>
Date: Mon, 10 Jan 2011 15:39:36 +0000
Local: Mon, Jan 10 2011 10:39 am
Subject: Re: [DataMapper] When should a resource be immutable?
Also, you will retreive an immutable resource if you define a Model
without a key.

To check for this (and to initialize relationships), you should always
run DataMapper.finalize after defining all your models.

Regards
Jon

On 10 January 2011 15:23, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Gamsjaeger  
View profile  
 More options Jan 10 2011, 10:52 am
From: Martin Gamsjaeger <gamsnj...@gmail.com>
Date: Mon, 10 Jan 2011 16:52:53 +0100
Local: Mon, Jan 10 2011 10:52 am
Subject: Re: [DataMapper] When should a resource be immutable?
To add to what Jonathan said, if you *retrieve* a resource without
it's key, you get back an immutable resource too.

So if you have a Person model with a Serial id and you do something like:

Person.all(:job => 'coder', :fields => [ :name ])

you will get back a Collection of immutable resources, because you've
told it specifically to not include the key property (:id)

cheers
snusnu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Gamsjaeger  
View profile  
 More options Jan 10 2011, 10:58 am
From: Martin Gamsjaeger <gamsnj...@gmail.com>
Date: Mon, 10 Jan 2011 16:58:07 +0100
Local: Mon, Jan 10 2011 10:58 am
Subject: Re: [DataMapper] When should a resource be immutable?
I forgot to give an explanation of why DM behaves like that. It's
probably trivial, but why not add it here for completeness.

If you have a resource in memory, but you don't know it's key, there's
no way to persist it back to the datastore.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zhi-Qiang Lei  
View profile  
 More options Jan 10 2011, 11:42 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Tue, 11 Jan 2011 00:42:11 +0800
Local: Mon, Jan 10 2011 11:42 am
Subject: Re: [DataMapper] When should a resource be immutable?

Hi,

I have some code as follow.

class Loan
  include DataMapper::Resource
  belongs_to :loaner, Person, :key => true
  belongs_to :loanee, Person, :key => true
  property :currency, Enum[*CURRENCY_CODES], :key => true
  property :amount, Decimal, :scale => 2, :default => 0

  def self.count!(money_flow)
    loan = get(money_flow.currency, money_flow.giver_id, money_flow.receiver_id)
    loan.amount += money_flow.amount
  end

  # more code...
end

class MoneyFlow
  include DataMapper::Resource
  belongs_to :giver, Person
  belongs_to :receiver, Person

  property :id, Serial
  property :amount, Decimal, :scale => 2, :min => 0.01
  property :currency, Enum[*CURRENCY_CODES], :default => :CNY

  # more code...
end

When I test the "count!" class method as follow:

  describe "#count!" do
    subject { lambda { Loan.count!(money_flow) } }
    context "when a loan exists from giver to receiver" do
      let!(:loan) { Loan.gen }
      let(:money_flow) { MoneyFlow.gen(:giver => loan.loaner, :receiver => loan.loanee, :currency => loan.currency) }
      it { should_not change(Loan, :count) }
    end
  end

It tells me I got a Immutable Error. I feel strange on that. They have keys, and I didn't destroy the record. Can you see why? Thanks.

  1) Loan#count! when a loan exists from giver to receiver
     Failure/Error: subject { lambda { Loan.count!(money_flow) } }
     DataMapper::ImmutableError:
       Immutable resource cannot be modified
     # ./lib/models.rb:49:in `count!'
     # ./spec/models_spec.rb:59:in `block (4 levels) in <top (required)>'
     # ./spec/models_spec.rb:63:in `block (4 levels) in <top (required)>'

On Jan 10, 2011, at 11:34 PM, Ted Han wrote:

Best regards,
Zhi-Qiang Lei
zhiqiang....@gmail.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RipTheJacker  
View profile  
 More options Jan 10 2011, 6:05 pm
From: RipTheJacker <kab...@gmail.com>
Date: Mon, 10 Jan 2011 15:05:54 -0800 (PST)
Local: Mon, Jan 10 2011 6:05 pm
Subject: Re: When should a resource be immutable?
You should probably just change that spec. The element that you want
to test is a proc (you hope) getting invoked inside of the subject
block, which is a weird pattern, and you don't need the #let blocks
really, since you only use those objects once.

Try:

 describe "#count!" do
    specify "when a loan exists from giver to receiver" do
        loan = Loan.gen
        money_flow = MoneyFlow.gen(:giver => loan.loaner, :receiver =>
loan.loanee, :currency => loan.currency)
      expect do
        Loan.count!(money_flow)
      end.to_not change(Loan, :count)
    end
  end

I'm assuming you are using rspec 2, though it will work in rspec 1.x
with a syntax change. If you still get that error, post the new
backtrace here. At the very least the spec and the backtrace will be
more meaningful this way, and may be easier to debug (I hope).

On Jan 10, 10:42 am, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zhi-Qiang Lei  
View profile  
 More options Jan 11 2011, 12:22 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Tue, 11 Jan 2011 13:22:40 +0800
Local: Tues, Jan 11 2011 12:22 am
Subject: Re: [DataMapper] Re: When should a resource be immutable?
Hi,

I still get the error with your spec code, here is the backtrace.

Failures:

  1) Loan#count! when a loan exists from giver to receiver
     Failure/Error: Loan.count!(money_flow)
     DataMapper::ImmutableError:
       Immutable resource cannot be modified
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/dm-core-1.0.2/lib/dm-core/r esource/state/immutable.rb:16:in `set'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/dm-core-1.0.2/lib/dm-core/m odel/property.rb:251:in `amount='
     # ./lib/models.rb:49:in `count!'
     # ./spec/models_spec.rb:64:in `block (4 levels) in <top (required)>'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-expectations-2.4.0/li b/rspec/matchers/change.rb:17:in `call'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-expectations-2.4.0/li b/rspec/matchers/change.rb:17:in `matches?'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-expectations-2.4.0/li b/rspec/expectations/handler.rb:34:in `handle_matcher'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-expectations-2.4.0/li b/rspec/expectations/extensions/kernel.rb:50:in `should_not'
     # ./spec/models_spec.rb:63:in `block (3 levels) in <top (required)>'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:49:in `instance_eval'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:49:in `block (2 levels) in run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:98:in `with_around_hooks'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:46:in `block in run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:91:in `block in with_pending_capture'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:90:in `catch'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:90:in `with_pending_capture'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example.rb:45:in `run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:261:in `block in run_examples'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:257:in `map'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:257:in `run_examples'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:231:in `run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:232:in `block in run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:232:in `map'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/example_group.rb:232:in `run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/command_line.rb:27:in `block (2 levels) in run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/command_line.rb:27:in `map'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/command_line.rb:27:in `block in run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/reporter.rb:12:in `report'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/command_line.rb:24:in `run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/runner.rb:55:in `run_in_process'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/runner.rb:46:in `run'
     # /Users/siegfried/.rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/ core/runner.rb:10:in `block in autorun'

On Jan 11, 2011, at 7:05 AM, RipTheJacker wrote:

Best regards,
Zhi-Qiang Lei
zhiqiang....@gmail.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RipTheJacker  
View profile  
 More options Jan 11 2011, 2:15 pm
From: RipTheJacker <kab...@gmail.com>
Date: Tue, 11 Jan 2011 11:15:47 -0800 (PST)
Local: Tues, Jan 11 2011 2:15 pm
Subject: Re: When should a resource be immutable?
Ok. It's because of this:

  property :currency, Enum[*CURRENCY_CODES], :key => true

You can't have :key => true on an Enum property since it is not
unique.

On Jan 10, 11:22 pm, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zhi-Qiang Lei  
View profile  
 More options Jan 12 2011, 1:28 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Wed, 12 Jan 2011 14:28:49 +0800
Local: Wed, Jan 12 2011 1:28 am
Subject: Re: [DataMapper] Re: When should a resource be immutable?
Hi,

This model also has two more keys, they make a composite keys. This will also make it immutable?

>>>>   belongs_to :loaner, Person, :key => true
>>>>   belongs_to :loanee, Person, :key => true

On Jan 12, 2011, at 3:15 AM, RipTheJacker wrote:

Best regards,
Zhi-Qiang Lei
zhiqiang....@gmail.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RipTheJacker  
View profile  
 More options Jan 12 2011, 2:17 pm
From: RipTheJacker <kab...@gmail.com>
Date: Wed, 12 Jan 2011 11:17:27 -0800 (PST)
Local: Wed, Jan 12 2011 2:17 pm
Subject: Re: When should a resource be immutable?
Those won't make it immutable, but the key does need to be unique. So,
unless you can only have ONE Loan per Person you should probably
change those too. Rather than making them keys you probably mean to
have them as indexes, which I DataMapper does for you, and add an :id
Serial primary key to the model, the same as it is in your MoneyFlow
model.

On Jan 12, 12:28 am, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zhi-Qiang Lei  
View profile  
 More options Jan 13 2011, 2:39 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Thu, 13 Jan 2011 15:39:37 +0800
Local: Thurs, Jan 13 2011 2:39 am
Subject: Re: [DataMapper] Re: When should a resource be immutable?
OK, thanks. I will make it in that way.

On Jan 13, 2011, at 3:17 AM, RipTheJacker wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zhi-Qiang Lei  
View profile  
 More options Jan 13 2011, 2:53 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Thu, 13 Jan 2011 15:53:48 +0800
Local: Thurs, Jan 13 2011 2:53 am
Subject: Re: [DataMapper] Re: When should a resource be immutable?
Excuse me, a little more discussion please. In my past design, there should be ONLY ONE Loan record between two people in a specific currency. The composite keys match this requirement, don't they? But to make it immutable.

On Jan 13, 2011, at 3:17 AM, RipTheJacker wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RipTheJacker  
View profile  
 More options Jan 13 2011, 6:39 pm
From: RipTheJacker <kab...@gmail.com>
Date: Thu, 13 Jan 2011 15:39:13 -0800 (PST)
Local: Thurs, Jan 13 2011 6:39 pm
Subject: Re: When should a resource be immutable?
Yes, the composite keys match your requirement, so you can leave them
for the belongs_to associations. And I'm starting to think this may
really be a bug. See if this works:

remove the :key => true from :currency and add this:
validates_uniqueness_of :currency, :scope => [:loaner_id, :loanee_id]

On Jan 13, 1:53 am, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zhi-Qiang Lei  
View profile  
 More options Jan 14 2011, 1:08 am
From: Zhi-Qiang Lei <zhiqiang....@gmail.com>
Date: Fri, 14 Jan 2011 14:08:23 +0800
Local: Fri, Jan 14 2011 1:08 am
Subject: Re: [DataMapper] Re: When should a resource be immutable?
Removing the key on currency only will lead to only one Loan record between two people, and they cannot have other Loan in different currency. So I remove all of them (keys), and add a id Serial property. With this validation, not ImmutableError recurs yet.
Do you think I should submit a ticket for this?

On Jan 14, 2011, at 7:39 AM, RipTheJacker wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RipTheJacker  
View profile  
 More options Jan 14 2011, 4:07 pm
From: RipTheJacker <kab...@gmail.com>
Date: Fri, 14 Jan 2011 13:07:44 -0800 (PST)
Local: Fri, Jan 14 2011 4:07 pm
Subject: Re: When should a resource be immutable?
A ticket would be great, and please include a link to this discussion
in there as well.

On Jan 14, 12:08 am, Zhi-Qiang Lei <zhiqiang....@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »