action_command 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87d1121215dc01fba0fddbb6f7f33e6465e59c47
4
- data.tar.gz: ff9229f8fbafcc73acd0c9f787b7ff0d6b9fa1d5
3
+ metadata.gz: 0ff531e1eca24b2ebde5421dc5bfb145a3060658
4
+ data.tar.gz: 157affdb256a84d6a8afd79ef73b2dbf7b684917
5
5
  SHA512:
6
- metadata.gz: f650e9313684e1bf93c798a4f28ab86479d711645f6a69a716fa7d4cf3f0d5f390eb9283d2a0af9242e04363cc7d0137863ec4e5806cafaba4df122001b71404
7
- data.tar.gz: 596f8d569c692586c246ab41a863aa2a803bfbb244eea42617f2a3f10783af8f4b0d15c79e82a214159dcd475a842a3133ad3b53cbb7825f070eb4a8db4a4562
6
+ metadata.gz: a23a99e55f8fbd985631f5e55f983f0f6bb8f7fa76f96b81c371bfade67f76628dbfa8e949e72a8eae87df7d9555d8259d6be7c9467e9a2ab67750d5118dbeb9
7
+ data.tar.gz: 107d8d93f7d3877f171b0bafc7b605e4bd49f251e1d6681d0fdc830701c9e574bd2b8fb2187eef120919b87ea83b66c9d2643acf76fe1a7c3cf361187d7ebb66
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- action_command (0.1.0)
4
+ action_command (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -25,7 +25,112 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- TODO: Write usage instructions here
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
 
data/Rakefile CHANGED
@@ -4,6 +4,8 @@ require 'rubocop/rake_task'
4
4
  require 'yard'
5
5
  require 'rake_command_filter'
6
6
 
7
+ load './spec/test_rake/helloworld.rake'
8
+
7
9
  RSpec::Core::RakeTask.new(:spec)
8
10
  RuboCop::RakeTask.new(:rubocop)
9
11
  YARD::Rake::YardocTask.new(:yard)