Skip to content

Commit

Permalink
extract config processing from internal content
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaidma committed Jan 28, 2016
1 parent 5facc13 commit c0f3477
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 70 deletions.
100 changes: 48 additions & 52 deletions lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,65 @@

var _ = require('lodash')

function Configuration (config) {
this.config = config
}

Configuration.prototype.validate = function () {
var initStateOK = false
module.exports = {
validate: function (config) {
var initStateOK = false

for (var i in this.config.states) {
if (this.config.states[i].initState) {
if (initStateOK) {
return 'One single state should have initState: true'
for (var i in config.states) {
if (config.states[i].initState) {
if (initStateOK) {
return 'One single state should have initState: true'
}
initStateOK = true
}
initStateOK = true
}
}
if (!initStateOK) {
return 'One single state should have initState: true'
}
return
}
if (!initStateOK) {
return 'One single state should have initState: true'
}
return
},

Configuration.prototype.getInitState = function () {
for (var state_name in this.config.states) {
if (this.config.states[state_name].initState) {
return state_name
getInitState: function (config) {
for (var state_name in config.states) {
if (config.states[state_name].initState) {
return state_name
}
}
}
}
},

Configuration.prototype.getCommands = function () {
var cmds = {}
for (var state_name in this.config.states) {
for (var command_name in this.config.states[state_name].commands) {
cmds[command_name] = {
// maybe I will need something here
getCommands: function (config) {
var cmds = {}
for (var state_name in config.states) {
for (var command_name in config.states[state_name].commands) {
cmds[command_name] = {
// maybe I will need something here
}
}
}
}
return cmds
}
return cmds
},

Configuration.prototype.processDefaults = function () {
for (var state_name in this.config.states) {
if (this.config.states[state_name].defaults) {
for (var command_name in this.config.states[state_name].commands) {
this.config.states[state_name].commands[command_name].pattern =
this.config.states[state_name].commands[command_name].pattern ||
this.config.states[state_name].defaults.pattern
this.config.states[state_name].commands[command_name].next = _.extend(
{},
this.config.states[state_name].defaults.next || {},
this.config.states[state_name].commands[command_name].next || {}
)
processDefaults: function (config) {
for (var state_name in config.states) {
if (config.states[state_name].defaults) {
for (var command_name in config.states[state_name].commands) {
config.states[state_name].commands[command_name].pattern =
config.states[state_name].commands[command_name].pattern ||
config.states[state_name].defaults.pattern
config.states[state_name].commands[command_name].next = _.extend(
{},
config.states[state_name].defaults.next || {},
config.states[state_name].commands[command_name].next || {}
)
}
}
}
}
}
},

Configuration.prototype.retrieve_command = function (state, cmd) {
if (this.config.states[state].commands[cmd]) {
return this.config.states[state].commands[cmd]
retrieve_command: function (config, state, cmd) {
if (config.states[state].commands[cmd]) {
return config.states[state].commands[cmd]
}
return
}
return
}

module.exports = Configuration
19 changes: 9 additions & 10 deletions lib/sm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var Configuration = require('./rules')
var ConfigurationHelper = require('./rules')
var StateHelper = require('./state')
var _ = require('lodash')

Expand All @@ -25,31 +25,30 @@ module.exports = function (options) {
}

function create (args, done) {
var rules = new Configuration(args)
var sm_name = rules.config.name
var config = args
var sm_name = config.name

if (internals[sm_name]) {
return done('SM already exists')
}

var validate_error = rules.validate()
var validate_error = ConfigurationHelper.validate(config)
if (validate_error) {
return done(validate_error)
}

var context = {
rules: rules,
config: config,
commands: {}
}

internals[sm_name] = context

context.current_status = rules.getInitState()
context.current_status = ConfigurationHelper.getInitState(config)

// now process defaults
rules.processDefaults()
ConfigurationHelper.processDefaults(config)

var cmds = rules.getCommands()
var cmds = ConfigurationHelper.getCommands(config)
for (var command_name in cmds) {
if (context.commands[command_name]) {
// this command was already registered
Expand Down Expand Up @@ -96,7 +95,7 @@ module.exports = function (options) {

function execute_state (args, done) {
var context = internals[args.role]
var command = context.rules.retrieve_command(context.current_status, args.cmd)
var command = ConfigurationHelper.retrieve_command(context.config, context.current_status, args.cmd)

delete args.role
delete args.cmd
Expand Down
16 changes: 8 additions & 8 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ suite('state-machine suite tests', function () {
verify_defaults: function (callback) {
seneca.act("role: '" + Util.config.name + "', get: 'context'", function (err, context) {
expect(err).to.not.exist()
expect(context.rules.config.states.INIT).to.exist()
expect(context.rules.config.states.INIT.commands).to.exist()
expect(context.rules.config.states.INIT.commands.execute).to.exist()
expect(context.rules.config.states.INIT.commands.execute.pattern).to.equal(config1.states.INIT.defaults.pattern)
expect(context.rules.config.states.INIT.commands.execute.next).to.exist()
expect(context.rules.config.states.INIT.commands.execute.next.success).to.exist()
expect(context.rules.config.states.INIT.commands.execute.next.error).to.exist()
expect(context.rules.config.states.INIT.commands.execute.next.error).to.equal(config1.states.INIT.defaults.next.error)
expect(context.config.states.INIT).to.exist()
expect(context.config.states.INIT.commands).to.exist()
expect(context.config.states.INIT.commands.execute).to.exist()
expect(context.config.states.INIT.commands.execute.pattern).to.equal(config1.states.INIT.defaults.pattern)
expect(context.config.states.INIT.commands.execute.next).to.exist()
expect(context.config.states.INIT.commands.execute.next.success).to.exist()
expect(context.config.states.INIT.commands.execute.next.error).to.exist()
expect(context.config.states.INIT.commands.execute.next.error).to.equal(config1.states.INIT.defaults.next.error)

callback(err)
})
Expand Down

0 comments on commit c0f3477

Please sign in to comment.