Skip to content

Commit

Permalink
added _ to the start of private methods
Browse files Browse the repository at this point in the history
Changed name space so that a new state machine can be created with new StateMachine()
  • Loading branch information
kernow committed May 15, 2009
1 parent 19dd2ee commit 87e1e93
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 56 deletions.
6 changes: 5 additions & 1 deletion lib/state_machine.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* State machine namespace
* @class
*/
var SM = {};

// options: initial - the initial state
// action - a callback to run on every transition

SM.StateMachine = function (name, object, options, block) {
StateMachine = function (name, object, options, block) {
return new SM.Machine(name, object, options, block);
};
1 change: 1 addition & 0 deletions lib/state_machine/callback.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.Callback = function(options, machine, block) {
this.options = options;
this.machine = machine;
Expand Down
1 change: 1 addition & 0 deletions lib/state_machine/callback_collection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.CallbackCollection = function() {
this.callbacks = [];
this.callbacks.before = [];
Expand Down
5 changes: 3 additions & 2 deletions lib/state_machine/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SM.Event.prototype = {
return this.guards.match(this.name, this.machine.state(), params) ? true : false;
},
fire: function(params){
var transition = this.transition_for(params);
var transition = this._transition_for(params);
if( transition ){
return transition.perform();
}
Expand All @@ -26,7 +26,8 @@ SM.Event.prototype = {
return false;
}
},
transition_for: function(params){
/* Private methods */
_transition_for: function(params){
if(this.can_fire(params)){
var from = this.machine.state();
var to = this.guards.find_to_state(this.name, from, params);
Expand Down
1 change: 1 addition & 0 deletions lib/state_machine/event_collection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.EventCollection = function(){
this.events = [];
return this;
Expand Down
1 change: 1 addition & 0 deletions lib/state_machine/guard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.Guard = function(name, object, options){
this.name = name;
this.object = object;
Expand Down
1 change: 1 addition & 0 deletions lib/state_machine/guard_collection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.GuardsCollection = function() {
this.guards = [];
this.last_match = null;
Expand Down
31 changes: 16 additions & 15 deletions lib/state_machine/machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,40 @@ SM.Machine = function(name, object, options, block) {
this.machine_name = name;

this.internal_state = options && options.initial ? options.initial : '';
this.add_methods_to_object(name, object);
this._add_methods_to_object(name, object);

if(block){ block(this); }
return this;
};

SM.Machine.prototype = {

add_methods_to_object: function(name, object){
object[name] = this.state();
object[name+'_events'] = this.events.all();
object[name+'_states'] = this.states.all();
event: function(name, options, block) {
var event = this.events.add(name, this, options);
this._add_event_methods(name, this.object, event);
if(block){ block(event); }
return event;
},
before_transition: function(options, block){
var callback = this.callbacks.add('before', options, this, block);
},
after_transition: function(options, block){
var callback = this.callbacks.add('after', options, this, block);
},
event: function(name, options, block) {
var event = this.events.add(name, this, options);
this.add_event_methods(name, this.object, event);
if(block){ block(event); }
return event;
state: function(){
return this.internal_state;
},
/* Private method */
_add_methods_to_object: function(name, object){
object[name] = this.state();
object[name+'_events'] = this.events.all();
object[name+'_states'] = this.states.all();
},
add_event_methods: function(name, object, event) {
_add_event_methods: function(name, object, event) {
object[name] = function(){ return event.fire(arguments); };
object['can_'+name] = function(){ return event.can_fire(); };
},
state: function(){
return this.internal_state;
},
set_state: function(state){
_set_state: function(state){
this.internal_state = state;
this.object[this.machine_name] = state;
}
Expand Down
1 change: 1 addition & 0 deletions lib/state_machine/state_collection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.StateCollection = function() {
this.states = [];
return this;
Expand Down
5 changes: 3 additions & 2 deletions lib/state_machine/transition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/* Private class */
SM.Transition = function(machine, event, from, to, params){
this.machine = machine;
this.event = event;
Expand All @@ -11,7 +12,7 @@ SM.Transition = function(machine, event, from, to, params){
SM.Transition.prototype = {
perform: function() {
this.before();
this.machine.set_state(this.to);
this.machine._set_state(this.to);
this.after();
return true;
},
Expand All @@ -22,6 +23,6 @@ SM.Transition.prototype = {
this.machine.callbacks.run('after', this.from, this.to, this.event, this.params);
},
rollback: function() {
this.machine.set_state(this.from);
this.machine._set_state(this.from);
}
};
24 changes: 12 additions & 12 deletions test/js/callback_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('turn_on_radio');

new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){

machine.before_transition({ to: 'idling', run: machine.object.turn_on_radio });

Expand All @@ -33,7 +33,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('check_mirror');

new SM.StateMachine('state', this.car, { initial: 'idling' }, function(machine){
new StateMachine('state', this.car, { initial: 'idling' }, function(machine){

machine.before_transition({ from: 'idling', run: machine.object.check_mirror });

Expand All @@ -48,7 +48,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('check_mirror');

new SM.StateMachine('state', this.car, { initial: 'idling' }, function(machine){
new StateMachine('state', this.car, { initial: 'idling' }, function(machine){

machine.before_transition({ on: 'gear_up', run: machine.object.check_mirror });

Expand All @@ -63,7 +63,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('open_sunroof');

new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){

machine.after_transition({ to: 'idling', run: machine.object.open_sunroof });

Expand All @@ -78,7 +78,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('light_cigarette');

new SM.StateMachine('state', this.car, { initial: 'idling' }, function(machine){
new StateMachine('state', this.car, { initial: 'idling' }, function(machine){

machine.before_transition({ from: 'idling', run: machine.object.light_cigarette });

Expand All @@ -93,7 +93,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('light_cigarette');

new SM.StateMachine('state', this.car, { initial: 'idling' }, function(machine){
new StateMachine('state', this.car, { initial: 'idling' }, function(machine){

machine.before_transition({ on: 'gear_up', run: machine.object.light_cigarette });

Expand All @@ -109,7 +109,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
this.car.expects('check_mirror');
this.car.expects('light_cigarette').never();

new SM.StateMachine('state', this.car, { initial: 'idling' }, function(machine){
new StateMachine('state', this.car, { initial: 'idling' }, function(machine){

machine.before_transition({ from: 'idling', to: 'first_gear', run: machine.object.check_mirror });
machine.before_transition({ from: 'first_gear', to: 'second_gear', run: machine.object.light_cigarette });
Expand All @@ -130,7 +130,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
this.car.expects('turn_on_radio');
this.car.expects('open_sunroof');

new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){

machine.before_transition({to: 'idling', run: machine.object.turn_on_radio });
machine.after_transition({to: 'idling', run: machine.object.open_sunroof });
Expand All @@ -149,7 +149,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
this.car.expects('open_sunroof');
this.car.expects('close_sunroof').never();

new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){

machine.before_transition({to: 'idling', run: machine.object.turn_on_radio });
machine.before_transition({to: 'parked', run: machine.object.turn_off_radio });
Expand All @@ -174,7 +174,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
this.car.expects('open_sunroof');
this.car.expects('close_sunroof');

new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){

machine.before_transition({to: 'idling', run: machine.object.turn_on_radio });
machine.before_transition({to: 'parked', run: machine.object.turn_off_radio });
Expand All @@ -201,7 +201,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('close_sunroof');

new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){

machine.before_transition({to: 'idling'}, function(){
callback_checker.turn_on_radio();
Expand Down Expand Up @@ -242,7 +242,7 @@ jsStateMachineTests.CallbackTests = function(Y) {
new Mock(this.car);
this.car.expects('tune_radio_if_passed').passing('radio 1');

new SM.StateMachine('radio_state', this.car, { initial: 'off' }, function(machine){
new StateMachine('radio_state', this.car, { initial: 'off' }, function(machine){

machine.after_transition({ to: 'on', run: machine.object.tune_radio_if_passed }, function(){
Y.Assert.areEqual('radio 1', arguments[0]);
Expand Down
6 changes: 3 additions & 3 deletions test/js/event_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jsStateMachineTests.EventTests = function(Y) {
},

testCanCheckIfEventCanBeFired : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start', {}, function(event){
event.transition({ from: 'parked', to: 'idling' });
});
Expand All @@ -28,7 +28,7 @@ jsStateMachineTests.EventTests = function(Y) {
},

testCanFireAndEvent : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start', {}, function(event){
event.transition({ from: 'parked', to: 'idling' });
});
Expand All @@ -42,7 +42,7 @@ jsStateMachineTests.EventTests = function(Y) {
Y.Assert.isTrue(this.car.can_stop());
},
testCanSupportMultipleFromStatesAsArray : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start', {}, function(event){
event.transition({ from: 'parked', to: 'idling' });
});
Expand Down
30 changes: 15 additions & 15 deletions test/js/state_machine_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ jsStateMachineTests.StateMachineTests = function(Y) {
},

testShouldSetTheInitialStateAsBlank : function () {
new SM.StateMachine('state', this.car);
new StateMachine('state', this.car);
Y.Assert.areEqual('', this.car.state);
},
testShouldSetTheInitialState : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' });
new StateMachine('state', this.car, { initial: 'parked' });
Y.Assert.areEqual('parked', this.car.state);
},
testShouldAddNewEventMethods : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start');
});
Y.Assert.isFunction(this.car.start);
Y.Assert.isFunction(this.car.can_start);
},
testCanGetListOfEvents : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' });
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' });
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start');
machine.event('gear_up');
machine.event('gear_down');
Expand All @@ -53,7 +53,7 @@ jsStateMachineTests.StateMachineTests = function(Y) {
Y.Assert.isTrue(contains(this.car.state_events, 'gear_down'));
},
testCanGetListOfStates : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start', {}, function(event){
event.transition({from: 'parked', to: 'idling'});
});
Expand All @@ -69,7 +69,7 @@ jsStateMachineTests.StateMachineTests = function(Y) {
Y.Assert.isTrue(contains(this.car.state_states, 'first_gear'));
},
testCanGetListOfStatesUsingFromStatesAsArray : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start', {}, function(event){
event.transition({ from: 'parked', to: 'idling' });
});
Expand Down Expand Up @@ -103,23 +103,23 @@ jsStateMachineTests.StateMachineTests = function(Y) {
},

testShouldSetTheInitialStateAsBlank : function () {
new SM.StateMachine('state', this.car);
new SM.StateMachine('alarm_state', this.car);
new StateMachine('state', this.car);
new StateMachine('alarm_state', this.car);
Y.Assert.areEqual('', this.car.state);
Y.Assert.areEqual('', this.car.alarm_state);
},
testShouldSetTheInitialState : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' });
new SM.StateMachine('alarm_state', this.car, { initial: 'active' });
new StateMachine('state', this.car, { initial: 'parked' });
new StateMachine('alarm_state', this.car, { initial: 'active' });
Y.Assert.areEqual('parked', this.car.state);
Y.Assert.areEqual('active', this.car.alarm_state);
},
testShouldAddNewEventMethods : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start');
});

new SM.StateMachine('alarm_state', this.car, { initial: 'active' }, function(machine){
new StateMachine('alarm_state', this.car, { initial: 'active' }, function(machine){
machine.event('disable');
});
Y.Assert.isFunction(this.car.start);
Expand All @@ -128,12 +128,12 @@ jsStateMachineTests.StateMachineTests = function(Y) {
Y.Assert.isFunction(this.car.can_disable);
},
testCanGetListOfEvents : function () {
new SM.StateMachine('state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('state', this.car, { initial: 'parked' }, function(machine){
machine.event('start');
machine.event('gear_up');
machine.event('gear_down');
});
new SM.StateMachine('alarm_state', this.car, { initial: 'parked' }, function(machine){
new StateMachine('alarm_state', this.car, { initial: 'parked' }, function(machine){
machine.event('enable');
machine.event('disable');
machine.event('alarm');
Expand Down
Loading

0 comments on commit 87e1e93

Please sign in to comment.