1 | /**
|
2 | * Dependencies
|
3 | */
|
4 |
|
5 | import Promise from 'bluebird';
|
6 |
|
7 | /**
|
8 | * Private
|
9 | */
|
10 |
|
11 | function noop() {
|
12 | // No operation performed.
|
13 | }
|
14 |
|
15 | /**
|
16 | * Interface
|
17 | */
|
18 |
|
19 | export default class Step {
|
20 | constructor(pattern, fn, args = []) {
|
21 | this.pattern = pattern;
|
22 | this.fn = fn;
|
23 | this.args = args;
|
24 | }
|
25 |
|
26 | match(line) {
|
27 | return line.match(this.pattern);
|
28 | }
|
29 |
|
30 | runWith(context, line, cb = noop) {
|
31 | const args = (!!line) ? this.match(line).slice(1) : this.args;
|
32 | return Promise.try(() => (
|
33 | this.fn.apply(context, args)
|
34 | )).asCallback(cb);
|
35 | }
|
36 | }
|