action-hero 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dccea77bf4d490c1e635c2bd7f0590cad241242b
4
- data.tar.gz: 4a3152a4ae1a68ab1344c5b65241bfdf786cce2c
3
+ metadata.gz: 37e67b912e6e03b11924e6a3f09d897843090e21
4
+ data.tar.gz: 723acc43431200114f1b60e8e8d449da08740724
5
5
  SHA512:
6
- metadata.gz: a3f0e104b1317acdb58a9a73a25b8062d7696463cec588cc1153ee21289e1c391ce772a3a5c306c650d012d777fe488fb6a376fa3c7da539ee836695a849b6a4
7
- data.tar.gz: 1798d0126def710fa8f35898a626e08c345c8aeb2db08229e037b909d6eab2048defc0ff644a270de099e0b08f0794837bf9fe81cb0c8963e08f7a61082738d0
6
+ metadata.gz: 14d67b3b8ace6f3b69795e38749f75fdf02fb1118b30d5bc9f2f473c1950bd98a18fbf101a04e0437bf5b8f25a53c4f7f38c6323fbbb0cd3ad660c56bdf6d4fe
7
+ data.tar.gz: 6bfba48f4158314467ee2aff99aebc713a15b5de70d4d19c2f993bee07085e8fdac5fd1b933ce89bae4f586f1800dc8180e88e2944cdf82a53d5db103c2cc32c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActionHero
2
2
 
3
- Move actions from methods in Rails controllers to action classes.
3
+ Move actions from methods in Rails controllers and Grape endoints to action classes.
4
4
 
5
5
 
6
6
  ## Motiviation
@@ -29,15 +29,15 @@ Or install it yourself as:
29
29
  $ gem install action-hero
30
30
 
31
31
 
32
- ## Usage
32
+ ## Usage: Rails
33
33
 
34
34
  ### Simplest Case
35
35
 
36
- Include the ActionHero::Controller module in a controller.
36
+ Include the ActionHero::Rails::Controller module in a controller.
37
37
 
38
38
  # app/controllers/things_controller.rb
39
39
  class ThingsController < ApplicationController
40
- include ActionHero::Controller
40
+ include ActionHero::Rails::Controller
41
41
  end
42
42
 
43
43
  Define the action class.
@@ -45,7 +45,7 @@ Define the action class.
45
45
  # app/actions/things/index.rb
46
46
  module Things
47
47
  class Index
48
- include ActionHero::Action
48
+ include ActionHero::Rails::Action
49
49
 
50
50
  def call
51
51
  expose :things, Thing.all
@@ -100,7 +100,7 @@ Ensure an implicit controller is defined. Without configuration, the implicit c
100
100
 
101
101
  # app/controllers/implicit_controller.rb
102
102
  class ImplicitController < ApplicationController
103
- include ActionHero::Controller
103
+ include ActionHero::Rails::Controller
104
104
  end
105
105
 
106
106
 
@@ -113,12 +113,12 @@ Define the implicit controllers.
113
113
 
114
114
  # app/controllers/implicit_controller.rb
115
115
  class ImplicitController < ApplicationController
116
- include ActionHero::Controller
116
+ include ActionHero::Rails::Controller
117
117
  end
118
118
 
119
119
  # app/controllers/api/v1/implicit_controller.rb
120
120
  class Api::V1::ImplicitController < ApplicationController
121
- include ActionHero::Controller
121
+ include ActionHero::Rails::Controller
122
122
  end
123
123
 
124
124
  Configure the usage. The configuration uses regexs to match against the #controller_name and will use the first one
@@ -136,14 +136,14 @@ matched, so order matters.
136
136
 
137
137
  You can mix and match usage of ActionHero action classes and standard Rails controller action methods.
138
138
 
139
- In order to unobtrusively tie in to the controller, ActionHero::Controller implements the #action_missing
139
+ In order to unobtrusively tie in to the controller, ActionHero::Rails::Controller implements the #action_missing
140
140
  method. Thus, if you implement both an aciton class and an action method, the action method will win.
141
141
 
142
142
  Define a controller with a show action method and define an action class for the index action.
143
143
 
144
144
  # app/controllers/things_controller.rb
145
145
  class ThingsController < ApplicationController
146
- include ActionHero::Controller
146
+ include ActionHero::Rails::Controller
147
147
 
148
148
  def show
149
149
  ...
@@ -153,7 +153,7 @@ Define a controller with a show action method and define an action class for the
153
153
  # app/actions/things/index.rb
154
154
  module Things
155
155
  class Index
156
- include ActionHero::Action
156
+ include ActionHero::Rails::Action
157
157
 
158
158
  def call
159
159
  ...
@@ -193,3 +193,60 @@ aid in debugging, etc.
193
193
  Started GET "/" for 127.0.0.1 at 2013-09-08 12:38:26 -0500 │
194
194
  [Action Hero] No explicit DashboardController defined, using ImplicitController
195
195
  Processing by ImplicitController#show as HTML
196
+
197
+
198
+ ## Usage: Grape
199
+
200
+ ### Simplest Case
201
+
202
+ Unlike the ActionHero::Rails::Controller module that must be included in each controller, the
203
+ ActionHero::Grape::Endpoint is automatically included in the Grape::Endpoint when the action-hero
204
+ gem is required.
205
+
206
+ Define the action class.
207
+
208
+ # app/actions/api/v1/things/index.rb
209
+ module Api
210
+ module V1
211
+ module Things
212
+ class Index
213
+ include ActionHero::Grape::Action
214
+
215
+ def call
216
+ expose Thing.all
217
+ end
218
+ end
219
+ end
220
+ end
221
+ end
222
+
223
+ Use the action class in an endpoint.
224
+
225
+ module SomeApplication
226
+ class ApiV1
227
+ class ThingsEndpoint < Grape::API
228
+
229
+ get do
230
+ ::Api::V1::Things::Index.new( self ).call
231
+ end
232
+
233
+ end
234
+ end
235
+ end
236
+
237
+
238
+ ### Endpoint Methods Available in Action Class
239
+
240
+ The following methods forward from the the action class to the endpoint.
241
+
242
+ * content_type
243
+ * cookies
244
+ * error!
245
+ * headers
246
+ * params
247
+ * redirect
248
+ * route
249
+ * routes
250
+ * status
251
+ * version
252
+
@@ -1,16 +1,18 @@
1
1
  require "action_hero/version"
2
- require "dispatcher_ext"
2
+ require "dispatcher_ext" # TODO require for Rails only
3
3
 
4
4
  module ActionHero
5
5
 
6
6
  class ActionNotFound < StandardError #:nodoc:
7
7
  end
8
8
 
9
- autoload :Action, 'action_hero/action'
10
9
  autoload :ActionResolver, 'action_hero/action_resolver'
11
10
  autoload :Configuration, 'action_hero/configuration'
12
- autoload :Controller, 'action_hero/controller'
11
+ autoload :Grape, 'action_hero/grape'
13
12
  autoload :LogSubscriber, 'action_hero/log_subscriber'
13
+ autoload :MockController, 'action_hero/mock_controller'
14
+ autoload :ParamReader, 'action_hero/param_reader'
15
+ autoload :Rails, 'action_hero/rails'
14
16
 
15
17
  def self.configuration
16
18
  @configuration ||= Configuration.new
@@ -22,4 +24,5 @@ module ActionHero
22
24
 
23
25
  end
24
26
 
27
+ # TODO for Rails only
25
28
  ActionHero::LogSubscriber.attach_to :action_controller
@@ -21,7 +21,7 @@ module ActionHero
21
21
  end
22
22
 
23
23
  def action_class_file_exists?
24
- File.exists?( Rails.root.join( 'app', 'actions', "#{action_class_name.underscore}.rb" ))
24
+ File.exists?( ::Rails.root.join( 'app', 'actions', "#{action_class_name.underscore}.rb" ))
25
25
  end
26
26
 
27
27
  protected
@@ -0,0 +1,8 @@
1
+ module ActionHero
2
+ module Grape
3
+
4
+ autoload :Action, 'action_hero/grape/action'
5
+ autoload :Endpoint, 'action_hero/grape/endpoint'
6
+
7
+ end
8
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_support/concern'
2
+ require 'forwardable'
3
+
4
+ module ActionHero
5
+ module Grape
6
+ module Action
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+
12
+ extend Forwardable
13
+ extend ActionHero::ParamReader
14
+
15
+ attr_reader :endpoint
16
+ protected :endpoint
17
+
18
+ def_delegators :endpoint, :content_type,
19
+ :cookies,
20
+ :error!,
21
+ :headers,
22
+ :params,
23
+ :redirect,
24
+ :route,
25
+ :routes,
26
+ :status,
27
+ :version
28
+
29
+ end
30
+
31
+ def initialize( endpoint )
32
+ @endpoint = endpoint
33
+ end
34
+
35
+ module ClassMethods
36
+
37
+ def endpoint_delegations( *method_names )
38
+ def_delegators :endpoint, *method_names
39
+ end
40
+
41
+ end
42
+
43
+ protected
44
+
45
+ def expose( *args )
46
+ value = args.last
47
+
48
+ if args.size > 1
49
+ name = args.first.is_a?( Symbol ) ? args.first : (raise NotImplementedError)
50
+ instance_variable_set "@#{name}", value
51
+ self.class.send( :define_method, name, lambda { instance_variable_get "@#{name}" } )
52
+ end
53
+
54
+ endpoint.send :expose, *args
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,18 @@
1
+ module ActionHero
2
+ module Grape
3
+ module Endpoint
4
+
5
+ def expose( *args )
6
+ args.last.tap do |value|
7
+ if args.size > 1
8
+ name = args.first.is_a?( Symbol ) ? args.first : (raise NotImplementedError)
9
+ self.class.send( :define_method, name, lambda { value } )
10
+ end
11
+
12
+ body( value )
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module ActionHero
2
+ module MockController
3
+
4
+ include Controller
5
+
6
+ def action_name( *args ); end
7
+ def env( *args ); end
8
+ def flash( *args ); end
9
+ def formats( *args ); end
10
+ def head( *args ); end
11
+ def headers( *args ); end
12
+ def params( *args ); end
13
+ def redirect_to( *args ); end
14
+ def render( *args ); end
15
+ def render_to_string( *args ); end
16
+ def request( *args ); end
17
+ def reset_session( *args ); end
18
+ def respond_with( *args ); end
19
+ def response( *args ); end
20
+ def session( *args ); end
21
+ def url_for( *args ); end
22
+ def url_options( *args ); end
23
+
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module ActionHero
2
+ module ParamReader
3
+
4
+ def param_reader( *param_names )
5
+ param_names.each do |param_name|
6
+ define_method param_name do
7
+ params[param_name]
8
+ end
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module ActionHero
2
+ module Rails
3
+
4
+ autoload :Action, 'action_hero/rails/action'
5
+ autoload :Controller, 'action_hero/rails/controller'
6
+
7
+ end
8
+ end
@@ -0,0 +1,68 @@
1
+ require 'active_support/concern'
2
+ require 'forwardable'
3
+
4
+ module ActionHero
5
+ module Rails
6
+ module Action
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ include ::Rails.application.routes.url_helpers
11
+
12
+ included do
13
+
14
+ extend Forwardable
15
+ extend ActionHero::ParamReader
16
+
17
+ attr_reader :controller
18
+ protected :controller
19
+
20
+ def_delegators :controller, :action_name,
21
+ :env,
22
+ :flash,
23
+ :formats,
24
+ :head,
25
+ :headers,
26
+ :params,
27
+ :redirect_to,
28
+ :render,
29
+ :render_to_string,
30
+ :request,
31
+ :reset_session,
32
+ :respond_with,
33
+ :response,
34
+ :session,
35
+ :url_for,
36
+ :url_options
37
+
38
+ end
39
+
40
+ def initialize( controller )
41
+ @controller = controller
42
+ end
43
+
44
+ module ClassMethods
45
+
46
+ def controller_delegations( *method_names )
47
+ def_delegators :controller, *method_names
48
+ end
49
+
50
+ end
51
+
52
+ protected
53
+
54
+ def expose( *args )
55
+ name = args.first.is_a?( Symbol ) ? args.first : (raise NotImplementedError)
56
+ value = args.last
57
+ instance_variable_set "@#{name}", value
58
+ self.class.send( :define_method, name, lambda { instance_variable_get "@#{name}" } )
59
+ controller.send :expose, *args
60
+ end
61
+
62
+ def request_parameters
63
+ request.request_parameters
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,70 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActionHero
4
+ module Rails
5
+ module Controller
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ protected
10
+
11
+ def expose( *args )
12
+ name = args.first.is_a?( Symbol ) ? args.first : (raise NotImplementedError)
13
+ value = args.last
14
+ data.merge!( name => value )
15
+ self.class.send( :define_method, name, lambda { data[name] } )
16
+ self.class.helper_method name
17
+ value
18
+ end
19
+
20
+ def action_missing( name, *args, &block )
21
+ begin
22
+ action_class
23
+ rescue NoMethodError
24
+ # protect against misidentification due to
25
+ # NoMethodError being descendent of NameError
26
+ raise
27
+ rescue NameError => ex
28
+ raise ActionNotFound,
29
+ "The action #{action_class_name} nor #{self.class.name}##{name} could be found",
30
+ ex.backtrace
31
+ end
32
+ prepend_view_path "#{::Rails.root}/app/views/#{params[:controller]}"
33
+ execute_action
34
+ end
35
+
36
+ # override _normalize_render in order to insert implied controller's view prefix
37
+ def _normalize_render(*args, &block)
38
+ options = _normalize_args(*args, &block)
39
+ _normalize_options(options)
40
+ unless options[:prefixes].include?( params[:controller] )
41
+ options[:prefixes].insert( 0, params[:controller] )
42
+ end
43
+ options
44
+ end
45
+
46
+ def data
47
+ @data ||= {}.with_indifferent_access
48
+ end
49
+
50
+ def execute_action
51
+ action = action_class.new( self )
52
+ action.call
53
+ end
54
+
55
+ def resolver
56
+ @resolver ||= ActionResolver.new( :controller_const => "#{params[:controller].classify}Controller",
57
+ :action_name => action_name )
58
+ end
59
+
60
+ def action_class
61
+ resolver.action_class
62
+ end
63
+
64
+ def action_class_name
65
+ resolver.action_class_name
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -1,5 +1,5 @@
1
1
  module ActionHero
2
2
 
3
- VERSION = "0.3.1"
3
+ VERSION = "0.4.0"
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action-hero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - C. Jason Harrelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-16 00:00:00.000000000 Z
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,11 +69,17 @@ files:
69
69
  - action-hero.gemspec
70
70
  - lib/action-hero.rb
71
71
  - lib/action_hero.rb
72
- - lib/action_hero/action.rb
73
72
  - lib/action_hero/action_resolver.rb
74
73
  - lib/action_hero/configuration.rb
75
- - lib/action_hero/controller.rb
74
+ - lib/action_hero/grape.rb
75
+ - lib/action_hero/grape/action.rb
76
+ - lib/action_hero/grape/endpoint.rb
76
77
  - lib/action_hero/log_subscriber.rb
78
+ - lib/action_hero/mock_controller.rb
79
+ - lib/action_hero/param_reader.rb
80
+ - lib/action_hero/rails.rb
81
+ - lib/action_hero/rails/action.rb
82
+ - lib/action_hero/rails/controller.rb
77
83
  - lib/action_hero/version.rb
78
84
  - lib/dispatcher_ext.rb
79
85
  homepage: ''
@@ -1,53 +0,0 @@
1
- require 'active_support/concern'
2
- require 'forwardable'
3
-
4
- module ActionHero
5
- module Action
6
-
7
- extend ActiveSupport::Concern
8
-
9
- include Rails.application.routes.url_helpers
10
-
11
- included do
12
-
13
- extend Forwardable
14
-
15
- attr_reader :controller
16
- protected :controller
17
-
18
- def_delegators :controller, :action_name,
19
- :env,
20
- :flash,
21
- :formats,
22
- :head,
23
- :headers,
24
- :params,
25
- :redirect_to,
26
- :render,
27
- :render_to_string,
28
- :request,
29
- :reset_session,
30
- :respond_with,
31
- :response,
32
- :session,
33
- :url_for,
34
- :url_options
35
-
36
- end
37
-
38
- def initialize( controller )
39
- @controller = controller
40
- end
41
-
42
- protected
43
-
44
- def expose( *args )
45
- name = args.first.is_a?( Symbol ) ? args.first : (raise NotImplementedError)
46
- value = args.last
47
- instance_variable_set "@#{name}", value
48
- self.class.send( :define_method, name, lambda { instance_variable_get "@#{name}" } )
49
- controller.send :expose, *args
50
- end
51
-
52
- end
53
- end
@@ -1,68 +0,0 @@
1
- require 'active_support/concern'
2
-
3
- module ActionHero
4
- module Controller
5
-
6
- extend ActiveSupport::Concern
7
-
8
- protected
9
-
10
- def expose( *args )
11
- name = args.first.is_a?( Symbol ) ? args.first : (raise NotImplementedError)
12
- value = args.last
13
- data.merge!( name => value )
14
- self.class.send( :define_method, name, lambda { data[name] } )
15
- self.class.helper_method name
16
- value
17
- end
18
-
19
- def action_missing( name, *args, &block )
20
- begin
21
- action_class
22
- rescue NoMethodError
23
- # protect against misidentification due to
24
- # NoMethodError being descendent of NameError
25
- raise
26
- rescue NameError => ex
27
- raise ActionNotFound,
28
- "The action #{action_class_name} nor #{self.class.name}##{name} could be found",
29
- ex.backtrace
30
- end
31
- prepend_view_path "#{Rails.root}/app/views/#{params[:controller]}"
32
- execute_action
33
- end
34
-
35
- # override _normalize_render in order to insert implied controller's view prefix
36
- def _normalize_render(*args, &block)
37
- options = _normalize_args(*args, &block)
38
- _normalize_options(options)
39
- unless options[:prefixes].include?( params[:controller] )
40
- options[:prefixes].insert( 0, params[:controller] )
41
- end
42
- options
43
- end
44
-
45
- def data
46
- @data ||= {}.with_indifferent_access
47
- end
48
-
49
- def execute_action
50
- action = action_class.new( self )
51
- action.call
52
- end
53
-
54
- def resolver
55
- @resolver ||= ActionResolver.new( :controller_const => "#{params[:controller].classify}Controller",
56
- :action_name => action_name )
57
- end
58
-
59
- def action_class
60
- resolver.action_class
61
- end
62
-
63
- def action_class_name
64
- resolver.action_class_name
65
- end
66
-
67
- end
68
- end