mbulat/plutus — pull request #17 by willglynn (2013-02-21 02:40:52+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`](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-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 `Transaction.new` is used to instantiate a new `Transaction`.

We can then add a version of `#account=` that supports Strings, delegating to `#account_name=` after emitting a deprecation warning, and a similar `Transaction.build` function. This lets us ultimately support the original syntax again with only minimal compatibility code:

Transaction.build(credits: [{account: "Foo", amount: 50}])
DEPRECATION WARNING: Plutus::Transaction.build() is deprecated (use new instead)
DEPRECATION WARNING: 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.