Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Add this line to your init.rb:

property :id, Serial

is :state_machine, :initial => :green, :column => :color do
is :state_machine, :initial => :green, :property => :color do
state :green
state :yellow
state :red, :enter => :red_hook
Expand Down
8 changes: 3 additions & 5 deletions lib/dm-is-state_machine/is/data/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class Machine

# The property of the DM resource that will hold this Machine's
# state.
#
# TODO: change :column to :property
attr_accessor :column
attr_accessor :property

# The initial value of this Machine's state
attr_accessor :initial
Expand All @@ -27,8 +25,8 @@ class Machine

attr_accessor :states

def initialize(column, initial)
@column, @initial = column, initial
def initialize(property, initial)
@property, @initial = property, initial
@events, @states = [], []
@current_state_name = initial
end
Expand Down
6 changes: 3 additions & 3 deletions lib/dm-is-state_machine/is/dsl/event_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module EventDsl
# class TrafficLight
# include DataMapper::Resource
# property :id, Serial
# is :state_machine, :initial => :green, :column => :color do
# is :state_machine, :initial => :green, :property => :color do
# # state definitions go here...
#
# event :forward do
Expand Down Expand Up @@ -51,9 +51,9 @@ def event(name, &block)
#
# self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def #{name}!
# machine.current_state_name = __send__(:"#{column}")
# machine.current_state_name = __send__(:"#{property}")
# machine.fire_event(name, self)
# __send__(:"#{column}="), machine.current_state_name
# __send__(:"#{property}="), machine.current_state_name
# end
# RUBY

Expand Down
28 changes: 14 additions & 14 deletions lib/dm-is-state_machine/is/state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ class DuplicateStates < RuntimeError; end
class NoInitialState < RuntimeError; end

##
# Makes a column ('state' by default) act as a state machine. It will
# Makes a property ('state' by default) act as a state machine. It will
# define the property if it does not exist.
#
# @example [Usage]
# is :state_machine
# is :state_machine, :initial => :internal
# is :state_machine, :column => :availability
# is :state_machine, :column => :availability, :initial => :external
# is :state_machine, :property => :availability
# is :state_machine, :property => :availability, :initial => :external
#
# @param options<Hash> a hash of options
#
# @option :column<Symbol> the name of the custom column
# @option :property<Symbol> the name of the custom property
#
def is_state_machine(options = {}, &block)
extend DataMapper::Is::StateMachine::EventDsl
extend DataMapper::Is::StateMachine::StateDsl
include DataMapper::Is::StateMachine::InstanceMethods

# ===== Setup context =====
options = { :column => :state, :initial => nil }.merge(options)
column = options[:column]
options = { :property => :state, :initial => nil }.merge(options)
property = options[:property]
initial = options[:initial].to_s
unless properties.detect { |p| p.name == column }
property column, String, :default => initial
unless properties.detect { |p| p.name == property }
property property, String, :default => initial
end
machine = Data::Machine.new(column, initial)
machine = Data::Machine.new(property, initial)
@is_state_machine = { :machine => machine }

class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{column}=(value)
def #{property}=(value)
value = value.to_s if value.kind_of?(Symbol)
attribute_set(#{column.inspect}, value)
attribute_set(#{property.inspect}, value)
end
RUBY

Expand Down Expand Up @@ -119,10 +119,10 @@ def run_hook_if_present(hook)

def transition!(event_name)
machine = model.instance_variable_get(:@is_state_machine)[:machine]
column = machine.column
machine.current_state_name = __send__(column)
property = machine.property
machine.current_state_name = __send__(property)
machine.fire_event(event_name, self)
__send__("#{column}=", machine.current_state_name)
__send__("#{property}=", machine.current_state_name)
end

end # InstanceMethods
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/slot_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SlotMachine
property :id, Serial
property :power_on, Boolean, :default => false

is :state_machine, :initial => :off, :column => :mode do
is :state_machine, :initial => :off, :property => :mode do
state :off,
:enter => :power_down,
:exit => :power_up
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/traffic_light.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class TrafficLight

property :id, Serial # see note 1

is :state_machine, :initial => :green, :column => :color do
is :state_machine, :initial => :green, :property => :color do
state :green, :enter => Proc.new { |o| o.log << "G" }
state :yellow, :enter => Proc.new { |o| o.log << "Y" }
state :red, :enter => Proc.new { |o| o.log << "R" }
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/slot_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
@sm = SlotMachine.new
end

it "should have a 'mode' column" do
it "should have a 'mode' property" do
@sm.attributes.should have_key(:mode)
end

it "should have a 'power_on' column" do
it "should have a 'power_on' property" do
@sm.attributes.should have_key(:power_on)
end

it "should not have a 'state' column" do
it "should not have a 'state' property" do
@sm.attributes.should_not have_key(:state)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/integration/traffic_light_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
@t = TrafficLight.new
end

it "should have a 'color' column" do
it "should have a 'color' property" do
@t.attributes.should have_key(:color)
end

it "should not have a 'state' column" do
it "should not have a 'state' property" do
@t.attributes.should_not have_key(:state)
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/data/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def new_event(name, machine)
@machine = new_machine(:power, :off)
end

it "#column should work" do
@machine.column.should == :power
it "#property should work" do
@machine.property.should == :power
end

it "#initial should work" do
Expand Down Expand Up @@ -58,8 +58,8 @@ def new_event(name, machine)
@turn_on.stub!(:transitions).and_return([{ :from => :off, :to => :on }])
end

it "#column should work" do
@machine.column.should == :power
it "#property should work" do
@machine.property.should == :power
end

it "#initial should work" do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/dsl/event_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Earth
stub!(:push_state_machine_context)
stub!(:pop_state_machine_context)
end
machine = mock("machine", :events => [], :column => :state)
machine = mock("machine", :events => [], :property => :state)
Earth.instance_variable_set(:@is_state_machine, { :machine => machine })
end

Expand All @@ -35,7 +35,7 @@ class Earth
stub!(:pop_state_machine_context)
end

machine = mock("machine", :events => [], :column => :state)
machine = mock("machine", :events => [], :property => :state)
event = mock("sunrise_event")
event.stub!(:add_transition)
Earth.instance_variable_set(:@is_state_machine, {
Expand Down