Skip to content

Commit b0b8dee

Browse files
authored
Merge pull request #2574 from ericproulx/less_active_support_concern
Less active support concern
2 parents be8f382 + 0df0860 commit b0b8dee

30 files changed

+813
-788
lines changed

lib/grape/api.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class API
77
# Class methods that we want to call on the API rather than on the API object
88
NON_OVERRIDABLE = %i[call call! configuration compile! inherited recognize_path].freeze
99

10+
Helpers = Grape::DSL::Helpers::BaseHelper
11+
1012
class Boolean
1113
def self.build(val)
1214
return nil if val != true && val != false
@@ -15,10 +17,6 @@ def self.build(val)
1517
end
1618
end
1719

18-
class Instance
19-
Boolean = Grape::API::Boolean
20-
end
21-
2220
class << self
2321
extend Forwardable
2422
attr_accessor :base_instance, :instances

lib/grape/api/helpers.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/grape/api/instance.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ class API
55
# The API Instance class, is the engine behind Grape::API. Each class that inherits
66
# from this will represent a different API instance
77
class Instance
8+
extend Grape::DSL::Settings
9+
extend Grape::DSL::Desc
10+
extend Grape::DSL::Validations
11+
extend Grape::DSL::Callbacks
12+
extend Grape::DSL::Logger
13+
extend Grape::DSL::Middleware
14+
extend Grape::DSL::RequestResponse
15+
extend Grape::DSL::Routing
16+
extend Grape::DSL::Helpers
817
extend Grape::Middleware::Auth::DSL
9-
include Grape::DSL::API
18+
19+
Boolean = Grape::API::Boolean
1020

1121
class << self
1222
attr_reader :instance, :base
@@ -128,7 +138,7 @@ def evaluate_as_instance_with_configuration(block, lazy: false)
128138
def inherited(subclass)
129139
super
130140
subclass.reset!
131-
subclass.logger = logger.clone
141+
subclass.logger logger.clone
132142
end
133143

134144
def inherit_settings(other_settings)

lib/grape/dsl/api.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/grape/dsl/callbacks.rb

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,16 @@
22

33
module Grape
44
module DSL
5-
# Blocks can be executed before or after every API call, using `before`, `after`,
6-
# `before_validation` and `after_validation`.
7-
#
8-
# Before and after callbacks execute in the following order:
9-
#
10-
# 1. `before`
11-
# 2. `before_validation`
12-
# 3. _validations_
13-
# 4. `after_validation`
14-
# 5. _the API call_
15-
# 6. `after`
16-
#
17-
# Steps 4, 5 and 6 only happen if validation succeeds.
185
module Callbacks
19-
extend ActiveSupport::Concern
6+
# before: execute the given block before validation, coercion, or any endpoint
7+
# before_validation: execute the given block after `before`, but prior to validation or coercion
8+
# after_validation: execute the given block after validations and coercions, but before any endpoint code
9+
# after: execute the given block after the endpoint code has run except in unsuccessful
10+
# finally: execute the given block after the endpoint code even if unsuccessful
2011

21-
include Grape::DSL::Configuration
22-
23-
module ClassMethods
24-
# Execute the given block before validation, coercion, or any endpoint
25-
# code is executed.
26-
def before(&block)
27-
namespace_stackable(:befores, block)
28-
end
29-
30-
# Execute the given block after `before`, but prior to validation or
31-
# coercion.
32-
def before_validation(&block)
33-
namespace_stackable(:before_validations, block)
34-
end
35-
36-
# Execute the given block after validations and coercions, but before
37-
# any endpoint code.
38-
def after_validation(&block)
39-
namespace_stackable(:after_validations, block)
40-
end
41-
42-
# Execute the given block after the endpoint code has run.
43-
def after(&block)
44-
namespace_stackable(:afters, block)
45-
end
46-
47-
# Allows you to specify a something that will always be executed after a call
48-
# API call. Unlike the `after` block, this code will run even on
49-
# unsuccesful requests.
50-
# @example
51-
# class ExampleAPI < Grape::API
52-
# before do
53-
# ApiLogger.start
54-
# end
55-
# finally do
56-
# ApiLogger.close
57-
# end
58-
# end
59-
#
60-
# This will make sure that the ApiLogger is opened and closed around every
61-
# request
62-
# @param ensured_block [Proc] The block to be executed after every api_call
63-
def finally(&block)
64-
namespace_stackable(:finallies, block)
12+
%w[before before_validation after_validation after finally].each do |callback_method|
13+
define_method callback_method.to_sym do |&block|
14+
namespace_stackable(callback_method.pluralize.to_sym, block)
6515
end
6616
end
6717
end

lib/grape/dsl/configuration.rb

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/grape/dsl/desc.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
module Grape
44
module DSL
55
module Desc
6-
include Grape::DSL::Settings
7-
86
ROUTE_ATTRIBUTES = %i[
97
body_name
108
consumes

lib/grape/dsl/helpers.rb

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,76 @@
33
module Grape
44
module DSL
55
module Helpers
6-
extend ActiveSupport::Concern
7-
include Grape::DSL::Configuration
8-
9-
module ClassMethods
10-
# Add helper methods that will be accessible from any
11-
# endpoint within this namespace (and child namespaces).
12-
#
13-
# When called without a block, all known helpers within this scope
14-
# are included.
15-
#
16-
# @param [Array] new_modules optional array of modules to include
17-
# @param [Block] block optional block of methods to include
18-
#
19-
# @example Define some helpers.
20-
#
21-
# class ExampleAPI < Grape::API
22-
# helpers do
23-
# def current_user
24-
# User.find_by_id(params[:token])
25-
# end
26-
# end
27-
# end
28-
#
29-
# @example Include many modules
30-
#
31-
# class ExampleAPI < Grape::API
32-
# helpers Authentication, Mailer, OtherModule
33-
# end
34-
#
35-
def helpers(*new_modules, &block)
36-
include_new_modules(new_modules)
37-
include_block(block)
38-
include_all_in_scope if !block && new_modules.empty?
39-
end
6+
# Add helper methods that will be accessible from any
7+
# endpoint within this namespace (and child namespaces).
8+
#
9+
# When called without a block, all known helpers within this scope
10+
# are included.
11+
#
12+
# @param [Array] new_modules optional array of modules to include
13+
# @param [Block] block optional block of methods to include
14+
#
15+
# @example Define some helpers.
16+
#
17+
# class ExampleAPI < Grape::API
18+
# helpers do
19+
# def current_user
20+
# User.find_by_id(params[:token])
21+
# end
22+
# end
23+
# end
24+
#
25+
# @example Include many modules
26+
#
27+
# class ExampleAPI < Grape::API
28+
# helpers Authentication, Mailer, OtherModule
29+
# end
30+
#
31+
def helpers(*new_modules, &block)
32+
include_new_modules(new_modules)
33+
include_block(block)
34+
include_all_in_scope if !block && new_modules.empty?
35+
end
4036

41-
protected
37+
protected
4238

43-
def include_new_modules(modules)
44-
return if modules.empty?
39+
def include_new_modules(modules)
40+
return if modules.empty?
4541

46-
modules.each { |mod| make_inclusion(mod) }
47-
end
42+
modules.each { |mod| make_inclusion(mod) }
43+
end
4844

49-
def include_block(block)
50-
return unless block
45+
def include_block(block)
46+
return unless block
5147

52-
Module.new.tap do |mod|
53-
make_inclusion(mod) { mod.class_eval(&block) }
54-
end
48+
Module.new.tap do |mod|
49+
make_inclusion(mod) { mod.class_eval(&block) }
5550
end
51+
end
5652

57-
def make_inclusion(mod, &block)
58-
define_boolean_in_mod(mod)
59-
inject_api_helpers_to_mod(mod, &block)
60-
namespace_stackable(:helpers, mod)
61-
end
53+
def make_inclusion(mod, &block)
54+
define_boolean_in_mod(mod)
55+
inject_api_helpers_to_mod(mod, &block)
56+
namespace_stackable(:helpers, mod)
57+
end
6258

63-
def include_all_in_scope
64-
Module.new.tap do |mod|
65-
namespace_stackable(:helpers).each { |mod_to_include| mod.include mod_to_include }
66-
change!
67-
end
59+
def include_all_in_scope
60+
Module.new.tap do |mod|
61+
namespace_stackable(:helpers).each { |mod_to_include| mod.include mod_to_include }
62+
change!
6863
end
64+
end
6965

70-
def define_boolean_in_mod(mod)
71-
return if defined? mod::Boolean
66+
def define_boolean_in_mod(mod)
67+
return if defined? mod::Boolean
7268

73-
mod.const_set(:Boolean, Grape::API::Boolean)
74-
end
69+
mod.const_set(:Boolean, Grape::API::Boolean)
70+
end
7571

76-
def inject_api_helpers_to_mod(mod, &block)
77-
mod.extend(BaseHelper) unless mod.is_a?(BaseHelper)
78-
yield if block
79-
mod.api_changed(self)
80-
end
72+
def inject_api_helpers_to_mod(mod, &block)
73+
mod.extend(BaseHelper) unless mod.is_a?(BaseHelper)
74+
yield if block
75+
mod.api_changed(self)
8176
end
8277

8378
# This module extends user defined helpers

lib/grape/dsl/inside_route.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
module Grape
44
module DSL
55
module InsideRoute
6-
extend ActiveSupport::Concern
7-
include Grape::DSL::Settings
8-
include Grape::DSL::Headers
9-
106
# Denotes a situation where a DSL method has been invoked in a
117
# filter which it should not yet be available in
128
class MethodNotYetAvailable < StandardError; end

lib/grape/dsl/logger.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
module Grape
44
module DSL
55
module Logger
6-
include Grape::DSL::Settings
7-
8-
attr_writer :logger
9-
106
# Set or retrive the configured logger. If none was configured, this
117
# method will create a new one, logging to stdout.
128
# @param logger [Object] the new logger to use

0 commit comments

Comments
 (0)