0.0
No commit activity in last 3 years
No release in over 3 years
Enhances `attr_accessor` and `attr_writer` to allow the specification of a callback to be invoked when an attribute value is changed. Ideal for cascading updates or deployment scenarios where it is necessary to keep legacy values in sync until a time that they can be safely removed.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 0.10
~> 10.4
~> 3.1
 Project Readme

BetterAttrs

Build Status

Enhances attr_accessor and attr_writer to allow the specification of a callback to be invoked when an attribute value is changed.

Ideal for cascading updates or deployment scenarios where it is necessary to keep legacy values in sync until a time that they can be safely removed.

Installation

Add this line to your application's Gemfile:

gem 'better_attrs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install better_attrs

Usage

The callback key is the attribute name, suffixed by either _changed or _updated (both behave identically).

In the examples below the callback (specified as :foo_changed) will be invoked when the value of foo changes.

A callback can be:

  • a lambda
  • a proc
  • a String or Symbol (referring to a method)

In all cases the callback must be callable and must accept two arguments (the old_value and the new_value of the related attribute).

Adding a callback to an attr_accessor in new or existing class:

class AnyClass
  attr_accessor :foo, :foo_changed => proc { |old_value, new_value| }
end

Adding a callback to an attr_writer in a new or existing class:

class AnyClass
  attr_writer :foo, :foo_changed => proc { |old_value, new_value| }
end

Contributing

  1. Fork it (http://github.com/jzaleski/better_attrs/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request