action_command 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +106 -1
- data/Rakefile +2 -0
- data/doc/ActionCommand/Executable.html +452 -29
- data/doc/ActionCommand/InputOutput.html +559 -158
- data/doc/ActionCommand/Result.html +321 -32
- data/doc/ActionCommand.html +319 -65
- data/doc/_index.html +16 -1
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +101 -2
- data/doc/index.html +101 -2
- data/doc/method_list.html +118 -16
- data/doc/top-level-namespace.html +1 -1
- data/lib/action_command/executable.rb +75 -0
- data/lib/action_command/input_output.rb +149 -0
- data/lib/action_command/result.rb +76 -0
- data/lib/action_command/utils.rb +21 -0
- data/lib/action_command/version.rb +1 -1
- data/lib/action_command.rb +35 -186
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ff531e1eca24b2ebde5421dc5bfb145a3060658
|
4
|
+
data.tar.gz: 157affdb256a84d6a8afd79ef73b2dbf7b684917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a23a99e55f8fbd985631f5e55f983f0f6bb8f7fa76f96b81c371bfade67f76628dbfa8e949e72a8eae87df7d9555d8259d6be7c9467e9a2ab67750d5118dbeb9
|
7
|
+
data.tar.gz: 107d8d93f7d3877f171b0bafc7b605e4bd49f251e1d6681d0fdc830701c9e574bd2b8fb2187eef120919b87ea83b66c9d2643acf76fe1a7c3cf361187d7ebb66
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,112 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
|
28
|
+
`action_command` is designed to help you centralize logic which might otherwise end up in a controller or model, and easily invoke it from a controller, a test,
|
29
|
+
or a rake command. I read placing logic in 'actions' rather than models in the book [Rails 4 Test Prescriptions](http://www.amazon.com/Rails-Test-Prescriptions-Healthy-Codebase/dp/1941222196)
|
30
|
+
and liked it.
|
31
|
+
|
32
|
+
### HelloWorld
|
33
|
+
|
34
|
+
You can declare an action with inputs and outputs
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class HelloWorldCommand < ActionCommand::Executable
|
38
|
+
|
39
|
+
# You need to declare an attr_accessor for each named parameter
|
40
|
+
attr_accessor :name
|
41
|
+
|
42
|
+
# You can optional describe the input and output of the command,
|
43
|
+
# the text is used to provide help if you create a rake version of the command.
|
44
|
+
def self.describe_io
|
45
|
+
# the text in here is only
|
46
|
+
return ActionCommand.describe_io(self, 'Say hello to someone') do |io|
|
47
|
+
io.input(:name, 'Name of person to say hello to')
|
48
|
+
io.output(:greeting, 'Greeting for the person')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
# Override the execute internal method to provide logic for your action, and
|
55
|
+
# assign results to the result. You can also use methods like result.fail or
|
56
|
+
# result.info.
|
57
|
+
def execute_internal(result)
|
58
|
+
result[:greeting] = "Hello #{@name}" unless no_output
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
#### HelloWorld: Execute from Rails
|
64
|
+
|
65
|
+
You can execute it from rails:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
result = ActionCommand.execute_rails(HelloWorldCommand, { name: 'Chris' })
|
69
|
+
```
|
70
|
+
|
71
|
+
#### HelloWorld: Execute from Rake
|
72
|
+
|
73
|
+
When building a system, I find it useful to be able to easily run my actions from
|
74
|
+
the command-line as well. In rails, you can create a lib/tasks/my_rake.task, and
|
75
|
+
configure your actions as task with one line:
|
76
|
+
|
77
|
+
```
|
78
|
+
namespace :my_namespace do
|
79
|
+
|
80
|
+
# use [:initialize] as the last parameter if you want to do things that require
|
81
|
+
# rails startup in your command, like connecting to your database.
|
82
|
+
ActionCommand.install_rake(self, :hello_world, HelloWorldCommand, [])
|
83
|
+
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
You can always invoke your rake task with [help] to see help on the input and output
|
88
|
+
of the action.
|
89
|
+
|
90
|
+
#### HelloWorld: Execute from rspec/etc
|
91
|
+
|
92
|
+
Or, you can execute it from a testing framework.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
it 'says hello world' do
|
96
|
+
result = ActionCommand.execute_test(self, HelloWorldCommand, name: 'Chris')
|
97
|
+
expect(result).to be_ok
|
98
|
+
expect(result[:greeting]).to eq('Hello Chris')
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
If your command does a lot, you might like to do some internal verifications during the testing process to aid
|
103
|
+
debugging. Inside a command's execute_internal method, you can use a block like this
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
def execute_internal(result)
|
107
|
+
# ... do some logic
|
108
|
+
|
109
|
+
# t is the parameter you passed as the first argument to execute_test.
|
110
|
+
# so, if you are using rspec, this code block will only be executed when you are
|
111
|
+
# running in a testing context.
|
112
|
+
testing do |t|
|
113
|
+
t.expect(my_val).to t.eq(10)
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
### Child Actions
|
119
|
+
|
120
|
+
Actions can execute their own child actions. Within an action's execute_internal method
|
121
|
+
you should call additional actions via:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
def execute_internal
|
125
|
+
@names.each_with_index do |name, i|
|
126
|
+
# the i parameter will cause the result of the child command to be nested
|
127
|
+
# in the result under that value. For example, here I would expect
|
128
|
+
# result[i][:greeting] to contain the greeting for each subcommand after
|
129
|
+
# execution.
|
130
|
+
ActionCommand.execute_child(self, HelloWorldCommand, result, i, name: name)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
29
134
|
|
30
135
|
## Development
|
31
136
|
|