strategy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b6959b8c7099aa8ddaf5c6a868e97ecdc3751c8c
4
+ data.tar.gz: f9999439f1e67db5a104b58f83feac6fe85db868
5
+ SHA512:
6
+ metadata.gz: 384e90d59bef7a33eb45f678d46b509dae1f3902e1c8d1fc510f72f26164c62f3338817bfabd266a879e5c8e838305d93925b8e2fa04767acebc0f391551f9b0
7
+ data.tar.gz: 7b52e70710d073c8c07e609aa7f51a5ba89402e72ae1f68b6f042802960c51b1f7fcfebc4bf9836e8e23a9fbe71d07b2c02222e207ae6267e2e9308f72718e9b
@@ -0,0 +1,2 @@
1
+ require 'strategy/step'
2
+ require 'strategy/plan'
@@ -0,0 +1,62 @@
1
+ module Strategy
2
+ # Plan is a wrapper around a set of executable steps. It maintains a runbook
3
+ # of what to do and how to do it, and provides an easy way of displaying an
4
+ # execution plan or "strategy" before executing anything. A Plan object is the
5
+ # highest-level container around an execution strategy, and may contain as few
6
+ # or as many Step objects as required.
7
+ class Plan
8
+ attr_reader :name
9
+
10
+ # Create a new plan, to which individual execution steps will be added.
11
+ #
12
+ # == Parameters:
13
+ # name::
14
+ # The name of the execution plan. This is a short sentence which
15
+ # describes what the plan will do.
16
+ #
17
+ def initialize name
18
+ @name = name
19
+ @steps = []
20
+ end
21
+
22
+ # Adds a step object to the execution plan. Each step carries its own set of
23
+ # actions, description, and so on.
24
+ #
25
+ # == Parameters:
26
+ # step::
27
+ # A Strategy::Step object encapsulating some actions
28
+ #
29
+ def add step
30
+ if !step.kind_of? Step
31
+ raise TypeError, "Expected Strategy::Step but got #{step.class}"
32
+ end
33
+ @steps << step
34
+ end
35
+
36
+ # Iterate over all steps contained in the plan and execute them. This method
37
+ # will yield the step number and description if a block is given, which
38
+ # enables one to print out a step banner just before execution if desired.
39
+ def execute!
40
+ n = 0
41
+ @steps.each do |step|
42
+ n += 1
43
+ if block_given?
44
+ yield n, step.name
45
+ end
46
+ step.execute!
47
+ end
48
+ end
49
+
50
+ # Describe the plan itself and all steps involved. This method puts together
51
+ # a textual representation of the execution plan which can be displayed to a
52
+ # user before executing anything for confirmation.
53
+ def describe
54
+ description = [@name]
55
+ n = 0
56
+ @steps.each do |step|
57
+ description << " #{n+=1}. #{step.name}"
58
+ end
59
+ description.join "\n"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,33 @@
1
+ module Strategy
2
+ # Step encapsulates a single step in a larger plan of execution. A step
3
+ # contains a set of actions, which are the actual pieces of executable
4
+ # code, as well as a high-level description of what the step accomplishes.
5
+ class Step
6
+ attr_reader :name, :actions
7
+
8
+ # Creates a new Step, which can later be added to a Plan.
9
+ #
10
+ # == Parameters:
11
+ # name::
12
+ # The name of the step. This is a high-level description of what the
13
+ # step is supposed to accomplish.
14
+ #
15
+ def initialize name
16
+ @name = name
17
+ @actions = []
18
+ end
19
+
20
+ # Adds a new action to the step. Actions are given as a code block, and
21
+ # will be executed in the order they are added (if the step is executed).
22
+ def action &block
23
+ @actions << block
24
+ end
25
+
26
+ # Execute all actions sequentially as they appear in the Step.
27
+ def execute!
28
+ @actions.each do |action|
29
+ action.call
30
+ end
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strategy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Uber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Create runnable, stepped plans to control execution
56
+ email: ru@ryanuber.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/strategy/plan.rb
62
+ - lib/strategy/step.rb
63
+ - lib/strategy.rb
64
+ homepage: https://github.com/ryanuber/strategy-gem
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '1.8'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.0.14
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Create runnable, stepped execution plans
88
+ test_files: []