state_machine
https://github.com/pluginaweek/state_machine
Ruby
Adds support for creating state machines for attributes on any Ruby class
StateMachine::EvalHelpers#evaluate_method
Evaluates one of several different types of methods within the context
of the given object. Methods can be one of the following types:
* Symbol
* Method / Proc
* String
== Examples
Below are examples of the various ways that a method can be evaluated
on an object:
class Person
def initialize(name)
@name = name
end
def name
@name
end
end
class PersonCallback
def self.run(person)
person.name
end
end
person = Person.new('John Smith')
evaluate_method(person, :name) # => "John Smith"
evaluate_method(person, PersonCallback.method(:run)) # => "John Smith"
evaluate_method(person, Proc.new {|person| person.name}) # => "John Smith"
evaluate_method(person, lambda {|person| person.name}) # => "John Smith"
evaluate_method(person, '@name') # => "John Smith"
== Additional arguments
Additional arguments can be passed to the methods being evaluated. If
the method defines additional arguments other than the object context,
then all arguments are required.
For example,
person = Person.new('John Smith')
evaluate_method(person, lambda {|person| person.name}, 21) # => "John Smith"
evaluate_method(person, lambda {|person, age| "#{person.name} is #{age}"}, 21) # => "John Smith is 21"
evaluate_method(person, lambda {|person, age| "#{person.name} is #{age}"}, 21, 'male') # => ArgumentError: wrong number of arguments (3 for 2)Edit
git clone [email protected]:pluginaweek/state_machine.git
cd state_machine
open lib/state_machine/eval_helpers.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-StateMachine--EvalHelpers-evaluate_method-for-pr
# Commit to gitgit add lib/state_machine/eval_helpers.rbgit commit -m "better docs for StateMachine::EvalHelpers#evaluate_method"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-StateMachine--EvalHelpers-evaluate_method-for-pr
hub pull-request
# Celebrate!