Commit c26d885b authored by Abhijith PA's avatar Abhijith PA

Imported Upstream version 0.4.0

parents
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
tmp
*.bundle
*.so
*.o
*.a
mkmf.log
.idea/
--color
--warnings
--require spec_helper
language: ruby
sudo: false
script: "bundle exec rake"
cache: bundler
rvm:
- 2.1
- 2.0.0
- 2.2
- jruby
- rbx-2
matrix:
allow_failures:
- rvm: rbx-2
- rvm: jruby
* Fixed inconsistent use of :use_transactions
* Namespaced integrations are not registered by default anymore
* Pass `static: false` in case you don't want initial states to be forced. e.g.
```ruby
# will set the initial machine state
@machines.initialize_states(@object)
# optionally you can pass the attributes to have that as the initial state
@machines.initialize_states(@object, {}, { state: 'finished' })
# or pass set `static` to false if you want to keep the `object.state` current value
@machines.initialize_states(@object, { static: false })
```
- Aaron Gibralter
- Aaron Pfeifer
- Abdelkader Boudih
- Akira Matsuda
- Andrea Longhi
- Brad Heller
- Brandon Dimcheff
- Casey Howard
- Chinasaur
- Daniel Huckstep
- Durran Jordan
- Gareth Adams
- Jahangir Zinedine
- Jeremy Wells
- Joe Lind
- Jon Evans
- Markus Schirp
- Michael Klishin
- Mikhail Shirkov
- Mohamed Alouane
- Nate Murray
- Nathan Long
- Nicolas Blanco
- Pawel Pierzchala
- Pete Forde
- Peter Lampesberger
- Rin Raeuber
- Robert Poor
- Rustam Zagirov
- Sandro Turriate and Tim Pope
- Sean O'Brien
- Stefan Penner
- Steve Richert
- Wojciech Wnętrzak
- @chris
- @gmitrev
- @nblumoe
- @reiner
- @sanemat
\ No newline at end of file
source 'https://rubygems.org'
gemspec
platform :mri_20, :mri_21 do
gem 'pry-byebug'
end
gem 'minitest-reporters'
\ No newline at end of file
Copyright (c) 2006-2012 Aaron Pfeifer
Copyright (c) 2014-2015 Abdelkader Boudih
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This diff is collapsed.
require 'bundler/gem_tasks'
require 'rake/testtask'
Rake::TestTask.new(:functional) do |t|
t.test_files = FileList['test/functional/*_test.rb']
end
Rake::TestTask.new(:unit) do |t|
t.test_files = FileList['test/unit/**/*_test.rb']
end
desc 'Default: run all tests.'
task default: [:unit, :functional]
\ No newline at end of file
require 'state_machines/version'
require 'state_machines/core'
require 'state_machines/core_ext'
\ No newline at end of file
class Hash
# Provides a set of helper methods for making assertions about the content
# of various objects
unless respond_to?(:assert_valid_keys)
# Validate all keys in a hash match <tt>*valid_keys</tt>, raising ArgumentError
# on a mismatch. Note that keys are NOT treated indifferently, meaning if you
# use strings for keys but assert symbols as keys, this will fail.
#
# { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
# { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
# { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
# Code from ActiveSupport
def assert_valid_keys(*valid_keys)
valid_keys.flatten!
each_key do |k|
unless valid_keys.include?(k)
raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
end
end
end
end
# Validates that the given hash only includes at *most* one of a set of
# exclusive keys. If more than one key is found, an ArgumentError will be
# raised.
#
# == Examples
#
# options = {:only => :on, :except => :off}
# options.assert_exclusive_keys(:only) # => nil
# options.assert_exclusive_keys(:except) # => nil
# options.assert_exclusive_keys(:only, :except) # => ArgumentError: Conflicting keys: only, except
# options.assert_exclusive_keys(:only, :except, :with) # => ArgumentError: Conflicting keys: only, except
def assert_exclusive_keys(*exclusive_keys)
conflicting_keys = exclusive_keys & keys
raise ArgumentError, "Conflicting keys: #{conflicting_keys.join(', ')}" unless conflicting_keys.length <= 1
end
end
module StateMachines
# Represents a set of requirements that must be met in order for a transition
# or callback to occur. Branches verify that the event, from state, and to
# state of the transition match, in addition to if/unless conditionals for
# an object's state.
class Branch
include EvalHelpers
# The condition that must be met on an object
attr_reader :if_condition
# The condition that must *not* be met on an object
attr_reader :unless_condition
# The requirement for verifying the event being matched
attr_reader :event_requirement
# One or more requirements for verifying the states being matched. All
# requirements contain a mapping of {:from => matcher, :to => matcher}.
attr_reader :state_requirements
# A list of all of the states known to this branch. This will pull states
# from the following options (in the same order):
# * +from+ / +except_from+
# * +to+ / +except_to+
attr_reader :known_states
# Creates a new branch
def initialize(options = {}) #:nodoc:
# Build conditionals
@if_condition = options.delete(:if)
@unless_condition = options.delete(:unless)
# Build event requirement
@event_requirement = build_matcher(options, :on, :except_on)
if (options.keys - [:from, :to, :on, :except_from, :except_to, :except_on]).empty?
# Explicit from/to requirements specified
@state_requirements = [{:from => build_matcher(options, :from, :except_from), :to => build_matcher(options, :to, :except_to)}]
else
# Separate out the event requirement
options.delete(:on)
options.delete(:except_on)
# Implicit from/to requirements specified
@state_requirements = options.collect do |from, to|
from = WhitelistMatcher.new(from) unless from.is_a?(Matcher)
to = WhitelistMatcher.new(to) unless to.is_a?(Matcher)
{:from => from, :to => to}
end
end
# Track known states. The order that requirements are iterated is based
# on the priority in which tracked states should be added.
@known_states = []
@state_requirements.each do |state_requirement|
[:from, :to].each {|option| @known_states |= state_requirement[option].values}
end
end
# Determines whether the given object / query matches the requirements
# configured for this branch. In addition to matching the event, from state,
# and to state, this will also check whether the configured :if/:unless
# conditions pass on the given object.
#
# == Examples
#
# branch = StateMachines::Branch.new(:parked => :idling, :on => :ignite)
#
# # Successful
# branch.matches?(object, :on => :ignite) # => true
# branch.matches?(object, :from => nil) # => true
# branch.matches?(object, :from => :parked) # => true
# branch.matches?(object, :to => :idling) # => true
# branch.matches?(object, :from => :parked, :to => :idling) # => true
# branch.matches?(object, :on => :ignite, :from => :parked, :to => :idling) # => true
#
# # Unsuccessful
# branch.matches?(object, :on => :park) # => false
# branch.matches?(object, :from => :idling) # => false
# branch.matches?(object, :to => :first_gear) # => false
# branch.matches?(object, :from => :parked, :to => :first_gear) # => false
# branch.matches?(object, :on => :park, :from => :parked, :to => :idling) # => false
def matches?(object, query = {})
!match(object, query).nil?
end
# Attempts to match the given object / query against the set of requirements
# configured for this branch. In addition to matching the event, from state,
# and to state, this will also check whether the configured :if/:unless
# conditions pass on the given object.
#
# If a match is found, then the event/state requirements that the query
# passed successfully will be returned. Otherwise, nil is returned if there
# was no match.
#
# Query options:
# * <tt>:from</tt> - One or more states being transitioned from. If none
# are specified, then this will always match.
# * <tt>:to</tt> - One or more states being transitioned to. If none are
# specified, then this will always match.
# * <tt>:on</tt> - One or more events that fired the transition. If none
# are specified, then this will always match.
# * <tt>:guard</tt> - Whether to guard matches with the if/unless
# conditionals defined for this branch. Default is true.
#
# == Examples
#
# branch = StateMachines::Branch.new(:parked => :idling, :on => :ignite)
#
# branch.match(object, :on => :ignite) # => {:to => ..., :from => ..., :on => ...}
# branch.match(object, :on => :park) # => nil
def match(object, query = {})
query.assert_valid_keys(:from, :to, :on, :guard)
if (match = match_query(query)) && matches_conditions?(object, query)
match
end
end
def draw(graph, event, valid_states)
fail NotImplementedError
end
protected
# Builds a matcher strategy to use for the given options. If neither a
# whitelist nor a blacklist option is specified, then an AllMatcher is
# built.
def build_matcher(options, whitelist_option, blacklist_option)
options.assert_exclusive_keys(whitelist_option, blacklist_option)
if options.include?(whitelist_option)
value = options[whitelist_option]
value.is_a?(Matcher) ? value : WhitelistMatcher.new(options[whitelist_option])
elsif options.include?(blacklist_option)
value = options[blacklist_option]
raise ArgumentError, ":#{blacklist_option} option cannot use matchers; use :#{whitelist_option} instead" if value.is_a?(Matcher)
BlacklistMatcher.new(value)
else
AllMatcher.instance
end
end
# Verifies that all configured requirements (event and state) match the
# given query. If a match is found, then a hash containing the
# event/state requirements that passed will be returned; otherwise, nil.
def match_query(query)
query ||= {}
if match_event(query) && (state_requirement = match_states(query))
state_requirement.merge(:on => event_requirement)
end
end
# Verifies that the event requirement matches the given query
def match_event(query)
matches_requirement?(query, :on, event_requirement)
end
# Verifies that the state requirements match the given query. If a
# matching requirement is found, then it is returned.
def match_states(query)
state_requirements.detect do |state_requirement|
[:from, :to].all? {|option| matches_requirement?(query, option, state_requirement[option])}
end
end
# Verifies that an option in the given query matches the values required
# for that option
def matches_requirement?(query, option, requirement)
!query.include?(option) || requirement.matches?(query[option], query)
end
# Verifies that the conditionals for this branch evaluate to true for the
# given object
def matches_conditions?(object, query)
query[:guard] == false ||
Array(if_condition).all? {|condition| evaluate_method(object, condition)} &&
!Array(unless_condition).any? {|condition| evaluate_method(object, condition)}
end
end
end
require 'state_machines/branch'
require 'state_machines/eval_helpers'
module StateMachines
# Callbacks represent hooks into objects that allow logic to be triggered
# before, after, or around a specific set of transitions.
class Callback
include EvalHelpers
class << self
# Determines whether to automatically bind the callback to the object
# being transitioned. This only applies to callbacks that are defined as
# lambda blocks (or Procs). Some integrations, such as DataMapper, handle
# callbacks by executing them bound to the object involved, while other
# integrations, such as ActiveRecord, pass the object as an argument to
# the callback. This can be configured on an application-wide basis by
# setting this configuration to +true+ or +false+. The default value
# is +false+.
#
# *Note* that the DataMapper and Sequel integrations automatically
# configure this value on a per-callback basis, so it does not have to
# be enabled application-wide.
#
# == Examples
#
# When not bound to the object:
#
# class Vehicle
# state_machine do
# before_transition do |vehicle|
# vehicle.set_alarm
# end
# end
#
# def set_alarm
# ...
# end
# end
#
# When bound to the object:
#
# StateMachines::Callback.bind_to_object = true
#
# class Vehicle
# state_machine do
# before_transition do
# self.set_alarm
# end
# end
#
# def set_alarm
# ...
# end
# end
attr_accessor :bind_to_object
# The application-wide terminator to use for callbacks when not
# explicitly defined. Terminators determine whether to cancel a
# callback chain based on the return value of the callback.
#
# See StateMachines::Callback#terminator for more information.
attr_accessor :terminator
end
# The type of callback chain this callback is for. This can be one of the
# following:
# * +before+
# * +after+
# * +around+
# * +failure+
attr_accessor :type
# An optional block for determining whether to cancel the callback chain
# based on the return value of the callback. By default, the callback
# chain never cancels based on the return value (i.e. there is no implicit
# terminator). Certain integrations, such as ActiveRecord and Sequel,
# change this default value.
#
# == Examples
#
# Canceling the callback chain without a terminator:
#
# class Vehicle
# state_machine do
# before_transition do |vehicle|
# throw :halt
# end
# end
# end
#
# Canceling the callback chain with a terminator value of +false+:
#
# class Vehicle
# state_machine do
# before_transition do |vehicle|
# false
# end
# end
# end
attr_reader :terminator
# The branch that determines whether or not this callback can be invoked
# based on the context of the transition. The event, from state, and
# to state must all match in order for the branch to pass.
#
# See StateMachines::Branch for more information.
attr_reader :branch
# Creates a new callback that can get called based on the configured
# options.
#
# In addition to the possible configuration options for branches, the
# following options can be configured:
# * <tt>:bind_to_object</tt> - Whether to bind the callback to the object involved.
# If set to false, the object will be passed as a parameter instead.
# Default is integration-specific or set to the application default.
# * <tt>:terminator</tt> - A block/proc that determines what callback
# results should cause the callback chain to halt (if not using the
# default <tt>throw :halt</tt> technique).
#
# More information about how those options affect the behavior of the
# callback can be found in their attribute definitions.
def initialize(type, *args, &block)
@type = type
raise ArgumentError, 'Type must be :before, :after, :around, or :failure' unless [:before, :after, :around, :failure].include?(type)
options = args.last.is_a?(Hash) ? args.pop : {}
@methods = args
@methods.concat(Array(options.delete(:do)))
@methods << block if block_given?
raise ArgumentError, 'Method(s) for callback must be specified' unless @methods.any?
options = {:bind_to_object => self.class.bind_to_object, :terminator => self.class.terminator}.merge(options)
# Proxy lambda blocks so that they're bound to the object