lnagel/plutus — pull request #1 by lnagel (2013-11-29 09:36:11+00:00)
Title: Deprecate Plutus::Transaction.build

ActiveRecord already has conventions for constructing objects from hashes.
Plutus can and should use them, rather than reinventing this functionality
with a custom .build method.

Starting with:
Transaction.build(credits: [{account: "Foo", amount: 50}])

Using accepts_nested_attributes_for and attr_accessible, this becomes:
foo = Account.find_by_name("Foo")
Transaction.new(credit_amounts_attributes: [{account: foo, amount: 50}])

As a next step, we can alias :credits= to :credit_amounts_attributes=:
foo = Account.find_by_name("Foo")
Transaction.new(credits: [{account: foo, amount: 50}])

Then, we can define a Plutus::Amount#account_name= method, which performs
this lookup and sets #account:
Transaction.new(credits: [{account_name: "Foo", amount: 50}])

This is the preferred syntax. account_name accepts a String (while account
accepts an Account), and .new is used to instantiate a new Transaction.

We can then add a version of #account= that delegates to #account_name= after
emitting a deprecation warning, and a similar Transaction.build function,
which lets us ultimately support the original syntax again with only minimal
compatibility code:
Transaction.build(credits: [{account: "Foo", amount: 50}])
DEPRECATION: Plutus::Transaction.build() is deprecated (use new instead)
DEPRECATION: Plutus was given an :account String (use account_name instead)

This changeset thus supports the previous build method, but with deprecation
warnings, since .build is no longer needed and ActiveRecord (sensibly) expects
account= to be given an Account. Still, this is an interface change. Since the
existing syntax still works, I chose a minor version bump, with the intention
of removing support for the deprecated syntax at a later date.