Class: Grape::Util::InheritableSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/util/inheritable_setting.rb

Overview

The per-scope settings registry behind the Grape DSL. The semantic accessors below — grouped by concern — are the supported API: add_* writers stack one registration per call (read back outermost scope first), plain = writers are nearest-wins scalar overrides, and +!+/+?+ pairs are scope flags. Deep-merged readers return nil when nothing is registered; plain stack readers return a frozen empty Array. The backing stores — a per-scope Hash per kind of state, each holding only what that scope itself set — and their keys are internal.

Settings instances form a chain: a scope inherits its parent's values (see #inherit_from), and endpoints snapshot the chain with #point_in_time_copy_for_endpoint. Nothing is copied down the chain when a scope is created; every reader resolves against #parent on demand, so a value an enclosing scope gains later is visible through scopes already nested inside it.

Defined Under Namespace

Classes: PathSettings

Constant Summary collapse

CALLBACK_STORE_KEYS =

Maps the callbacks DSL method names to their pluralized namespace-stackable storage keys (see #callbacks / #add_callback).

{
  before: :befores,
  before_validation: :before_validations,
  after_validation: :after_validations,
  after: :afters,
  finally: :finallies
}.freeze
EMPTY_STACK =

Shared empty result for #stacked / #stacked_keys when nothing is registered anywhere in the chain, so neither hands out a mutable Array.

[].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInheritableSetting

Instantiate a new settings instance, with blank values. The fresh instance can then be set to inherit from an existing instance (see #inherit_from).



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/grape/util/inheritable_setting.rb', line 61

def initialize
  @route = {}
  # Namespace settings are scope-local by design: nothing ever layers
  # them over a parent's (see #inherit_from, and the nesting behaviour
  # DSL::Settings#namespace_setting is specified to have), so a plain
  # Hash is the whole store.
  @namespace = {}
  # This scope's own inheritable overrides. Like @stackable_values it
  # stays nil until the first write, and inheritance is resolved by
  # walking #parent (see #inheritable) rather than by keeping a second
  # chain of stores alongside it.
  @namespace_inheritable = nil
  # This scope's own stackable registrations, one Array per key. Stays
  # nil until the first registration so scopes that only inherit don't
  # each carry an empty Hash.
  @stackable_values = nil
  @parent = nil
  @point_in_time_copies = nil
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



35
36
37
# File 'lib/grape/util/inheritable_setting.rb', line 35

def namespace
  @namespace
end

#parentObject (readonly)

Returns the value of attribute parent.



35
36
37
# File 'lib/grape/util/inheritable_setting.rb', line 35

def parent
  @parent
end

#routeObject (readonly)

Returns the value of attribute route.



35
36
37
# File 'lib/grape/util/inheritable_setting.rb', line 35

def route
  @route
end

Class Method Details

.globalObject

Retrieve global settings.



47
48
49
# File 'lib/grape/util/inheritable_setting.rb', line 47

def self.global
  @global ||= {}
end

.reset_global!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

only for testing

Clear all global settings.



54
55
56
# File 'lib/grape/util/inheritable_setting.rb', line 54

def self.reset_global!
  @global = {}
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/grape/util/inheritable_setting.rb', line 215

def ==(other)
  return true if equal?(other)
  return false unless other.is_a?(self.class)

  # Endpoint copies are siblings of the scope they were forked from, not
  # children of it (see #point_in_time_copy), so every endpoint of an API
  # hangs off the same parent object — which is the case the duplicate
  # check in DSL::Routing#route runs on. Both sides then inherit the same
  # values, so comparing own state decides it without serializing either
  # chain; #global is class-level and identical for both either way.
  # Stacks concatenate and #stack never records an empty one, so matching
  # own stacks means matching resolved ones; the rescue handler maps
  # merge, where a scope can restate an inherited mapping, so those are
  # compared resolved.
  return to_hash == other.to_hash unless parent.equal?(other.parent)

  same_own_store?(@stackable_values, other.stackable_values) &&
    route == other.route &&
    @namespace_inheritable == other.namespace_inheritable &&
    namespace == other.namespace &&
    rescue_handlers == other.rescue_handlers &&
    base_only_rescue_handlers == other.base_only_rescue_handlers
end

#add_all_rescue_handler(handler) ⇒ Object

Meta-selector registrations from rescue_from :all, :grape_exceptions and :internal_grape_exceptions (see DSL::RequestResponse#rescue_from): each records its handler (nil to use the built-in one) and flips the flags the error middleware reads through #rescue_all? / #rescue_grape_exceptions?; the backing store is an internal detail.



336
337
338
339
# File 'lib/grape/util/inheritable_setting.rb', line 336

def add_all_rescue_handler(handler)
  set_inheritable(:rescue_all, true)
  set_inheritable(:all_rescue_handler, handler)
end

#add_callback(callback_name, block) ⇒ Object



313
314
315
# File 'lib/grape/util/inheritable_setting.rb', line 313

def add_callback(callback_name, block)
  stack(CALLBACK_STORE_KEYS.fetch(callback_name), block)
end

#add_content_type(format, content_type) ⇒ Object



402
403
404
# File 'lib/grape/util/inheritable_setting.rb', line 402

def add_content_type(format, content_type)
  stack(:content_types, { format => content_type })
end

#add_contract_key_map(key_map) ⇒ Object



520
521
522
# File 'lib/grape/util/inheritable_setting.rb', line 520

def add_contract_key_map(key_map)
  stack(:contract_key_map, key_map)
end

#add_declared_params(params) ⇒ Object



268
269
270
# File 'lib/grape/util/inheritable_setting.rb', line 268

def add_declared_params(params)
  stack(:declared_params, params)
end

#add_error_formatter(format, formatter) ⇒ Object



426
427
428
# File 'lib/grape/util/inheritable_setting.rb', line 426

def add_error_formatter(format, formatter)
  stack(:error_formatters, { format => formatter })
end

#add_formatter(content_type, formatter) ⇒ Object



410
411
412
# File 'lib/grape/util/inheritable_setting.rb', line 410

def add_formatter(content_type, formatter)
  stack(:formatters, { content_type => formatter })
end

#add_grape_exceptions_rescue_handler(handler) ⇒ Object



341
342
343
344
345
# File 'lib/grape/util/inheritable_setting.rb', line 341

def add_grape_exceptions_rescue_handler(handler)
  set_inheritable(:rescue_all, true)
  set_inheritable(:rescue_grape_exceptions, true)
  set_inheritable(:grape_exceptions_rescue_handler, handler)
end

#add_helper(mod) ⇒ Object



463
464
465
# File 'lib/grape/util/inheritable_setting.rb', line 463

def add_helper(mod)
  stack(:helpers, mod)
end

#add_internal_grape_exceptions_rescue_handler(handler) ⇒ Object



347
348
349
# File 'lib/grape/util/inheritable_setting.rb', line 347

def add_internal_grape_exceptions_rescue_handler(handler)
  set_inheritable(:internal_grape_exceptions_rescue_handler, handler)
end

#add_middleware(operation_with_arguments) ⇒ Object



452
453
454
# File 'lib/grape/util/inheritable_setting.rb', line 452

def add_middleware(operation_with_arguments)
  stack(:middleware, operation_with_arguments)
end

#add_mount_path(mount_path) ⇒ Object



500
501
502
# File 'lib/grape/util/inheritable_setting.rb', line 500

def add_mount_path(mount_path)
  stack(:mount_path, mount_path)
end

#add_named_params(named_params) ⇒ Object



300
301
302
# File 'lib/grape/util/inheritable_setting.rb', line 300

def add_named_params(named_params)
  stack(:named_params, named_params)
end

#add_namespace(namespace) ⇒ Object



476
477
478
# File 'lib/grape/util/inheritable_setting.rb', line 476

def add_namespace(namespace)
  stack(:namespace, namespace)
end

#add_params_documentation(documented_attrs) ⇒ Object



281
282
283
# File 'lib/grape/util/inheritable_setting.rb', line 281

def add_params_documentation(documented_attrs)
  stack(:params, documented_attrs)
end

#add_parser(content_type, parser) ⇒ Object



418
419
420
# File 'lib/grape/util/inheritable_setting.rb', line 418

def add_parser(content_type, parser)
  stack(:parsers, { content_type => parser })
end

#add_representation(model_class, entity_class) ⇒ Object



439
440
441
# File 'lib/grape/util/inheritable_setting.rb', line 439

def add_representation(model_class, entity_class)
  stack(:representations, { model_class => entity_class })
end

#add_rescue_handlers(mapping, subclasses:) ⇒ Object

An exception class registered twice in the same scope keeps its first handler, and keeps the position it was first registered at.



384
385
386
387
388
389
# File 'lib/grape/util/inheritable_setting.rb', line 384

def add_rescue_handlers(mapping, subclasses:)
  @rescue_handler_maps ||= {}
  own = (@rescue_handler_maps[subclasses ? :rescue_handlers : :base_only_rescue_handlers] ||= {})
  ShadowedRescueHandlers.warn_about(own, mapping) if subclasses
  own.merge!(mapping) { |_klass, registered, _new| registered }
end

#add_rescue_options(options) ⇒ Object



326
327
328
# File 'lib/grape/util/inheritable_setting.rb', line 326

def add_rescue_options(options)
  stack(:rescue_options, options)
end

#add_route_renamed_param(path, new_name) ⇒ Object



160
161
162
# File 'lib/grape/util/inheritable_setting.rb', line 160

def add_route_renamed_param(path, new_name)
  (@route[:renamed_params] ||= {})[path] = new_name
end

#add_validation(validator) ⇒ Object



257
258
259
# File 'lib/grape/util/inheritable_setting.rb', line 257

def add_validation(validator)
  stack(:validations, validator)
end

#all_rescue_handlerObject



359
360
361
# File 'lib/grape/util/inheritable_setting.rb', line 359

def all_rescue_handler
  inheritable(:all_rescue_handler)
end

#authObject

The authentication configuration Hash recorded by the auth DSL (see Middleware::Auth::DSL): proc:, **options. Nearest-wins scalar; nil when no authenticator is declared — Endpoint uses that to warn about unauthenticated bare Rack mounts; the backing store is an internal detail.



668
669
670
# File 'lib/grape/util/inheritable_setting.rb', line 668

def auth
  inheritable(:auth)
end

#auth=(auth_options) ⇒ Object



672
673
674
# File 'lib/grape/util/inheritable_setting.rb', line 672

def auth=(auth_options)
  set_inheritable(:auth, auth_options)
end

#base_only_rescue_handlersObject



378
379
380
# File 'lib/grape/util/inheritable_setting.rb', line 378

def base_only_rescue_handlers
  merged_rescue_handlers(:base_only_rescue_handlers)
end

#build_params_withObject

The params-builder strategy set by build_with (both the API-level DSL::Routing#build_with and the params-block DSL::Parameters#build_with write it), consumed when the endpoint builds its Grape::Request. Nearest-wins scalar; nil when never set; the backing store is an internal detail.



655
656
657
# File 'lib/grape/util/inheritable_setting.rb', line 655

def build_params_with
  inheritable(:build_params_with)
end

#build_params_with=(strategy) ⇒ Object



659
660
661
# File 'lib/grape/util/inheritable_setting.rb', line 659

def build_params_with=(strategy)
  set_inheritable(:build_params_with, strategy)
end

#callbacksObject

Filter blocks registered by the callbacks DSL (see DSL::Callbacks), as a callback-name => blocks Array Hash keyed by the DSL method names (+:before+, :before_validation, :after_validation, :after, :finally), outermost scope first. Record them with #add_callback; the backing store is an internal detail.



309
310
311
# File 'lib/grape/util/inheritable_setting.rb', line 309

def callbacks
  CALLBACK_STORE_KEYS.transform_values { |store_key| stacked(store_key) }
end

#cascadeObject

Cascade flag assigned by the cascade DSL. An explicit nil is meaningful and distinct from never-set (the backing store is key-presence based), so #cascade_defined? reports whether any scope assigned it — Grape::API::Instance#cascade? falls back to the version options' cascade, then to true, when it was never assigned.



601
602
603
# File 'lib/grape/util/inheritable_setting.rb', line 601

def cascade
  inheritable(:cascade)
end

#cascade=(value) ⇒ Object



605
606
607
# File 'lib/grape/util/inheritable_setting.rb', line 605

def cascade=(value)
  set_inheritable(:cascade, value)
end

#cascade_defined?Boolean

Returns:

  • (Boolean)


609
610
611
# File 'lib/grape/util/inheritable_setting.rb', line 609

def cascade_defined?
  inheritable?(:cascade)
end

#content_typesObject

Content negotiation registries recorded by the request/response DSL (see DSL::RequestResponse): the content-type registry (+content_type+ and format), and the formatter, parser and error-formatter handler maps. Each registration stacks one single-entry Hash, deep-merged on read so a nested scope's registration wins; readers return nil when nothing is registered. Record entries with the corresponding add_* writer; the backing store is an internal detail.



398
399
400
# File 'lib/grape/util/inheritable_setting.rb', line 398

def content_types
  namespace_stackable_with_hash(:content_types)
end

#contract_key_mapsObject

Dry::Schema key maps registered by contract blocks (see Validations::ContractScope), one per contract, outermost scope first; declared uses them to write coerced params back under their declared keys. Record them with #add_contract_key_map; the backing store is an internal detail.



516
517
518
# File 'lib/grape/util/inheritable_setting.rb', line 516

def contract_key_maps
  stacked(:contract_key_map)
end

#declared_paramsObject

Declared-params entries registered by params blocks, one Array per scope, outermost scope first. Record them with #add_declared_params; the backing store is an internal detail.



264
265
266
# File 'lib/grape/util/inheritable_setting.rb', line 264

def declared_params
  stacked(:declared_params)
end

#default_error_formatterObject



550
551
552
# File 'lib/grape/util/inheritable_setting.rb', line 550

def default_error_formatter
  inheritable(:default_error_formatter)
end

#default_error_formatter=(formatter) ⇒ Object



554
555
556
# File 'lib/grape/util/inheritable_setting.rb', line 554

def default_error_formatter=(formatter)
  set_inheritable(:default_error_formatter, formatter)
end

#default_error_statusObject



558
559
560
# File 'lib/grape/util/inheritable_setting.rb', line 558

def default_error_status
  inheritable(:default_error_status)
end

#default_error_status=(status) ⇒ Object



562
563
564
# File 'lib/grape/util/inheritable_setting.rb', line 562

def default_error_status=(status)
  set_inheritable(:default_error_status, status)
end

#default_formatObject



542
543
544
# File 'lib/grape/util/inheritable_setting.rb', line 542

def default_format
  inheritable(:default_format)
end

#default_format=(default_format) ⇒ Object



546
547
548
# File 'lib/grape/util/inheritable_setting.rb', line 546

def default_format=(default_format)
  set_inheritable(:default_format, default_format)
end

#do_not_document!Object



634
635
636
# File 'lib/grape/util/inheritable_setting.rb', line 634

def do_not_document!
  set_inheritable(:do_not_document, true)
end

#do_not_document?Boolean

Returns:

  • (Boolean)


638
639
640
# File 'lib/grape/util/inheritable_setting.rb', line 638

def do_not_document?
  inheritable(:do_not_document) == true
end

#do_not_route_head!Object

Scope flags flipped by the routing DSL's bang methods (see DSL::Routing#do_not_route_head! and friends; Validations::OneofCollector also flips do_not_document!): once set in a scope they apply to it and everything nested under it. Readers return false when never set; the backing store is an internal detail.



618
619
620
# File 'lib/grape/util/inheritable_setting.rb', line 618

def do_not_route_head!
  set_inheritable(:do_not_route_head, true)
end

#do_not_route_head?Boolean

Returns:

  • (Boolean)


622
623
624
# File 'lib/grape/util/inheritable_setting.rb', line 622

def do_not_route_head?
  inheritable(:do_not_route_head) == true
end

#do_not_route_options!Object



626
627
628
# File 'lib/grape/util/inheritable_setting.rb', line 626

def do_not_route_options!
  set_inheritable(:do_not_route_options, true)
end

#do_not_route_options?Boolean

Returns:

  • (Boolean)


630
631
632
# File 'lib/grape/util/inheritable_setting.rb', line 630

def do_not_route_options?
  inheritable(:do_not_route_options) == true
end

#error_formattersObject



422
423
424
# File 'lib/grape/util/inheritable_setting.rb', line 422

def error_formatters
  namespace_stackable_with_hash(:error_formatters)
end

#formatObject

Serialization and error-response defaults recorded by the request/response DSL's get-or-set methods (see DSL::RequestResponse): format is the enforced API format, default_format the fallback used when a request doesn't specify one, and default_error_formatter / default_error_status shape error responses. Nearest-wins scalars — a nested scope's assignment overrides an inherited one, hence plain = writers rather than the add_* writers used for stackable registrations. Readers return nil when never set (Endpoint applies the request-serving fallbacks); the backing store is an internal detail.



534
535
536
# File 'lib/grape/util/inheritable_setting.rb', line 534

def format
  inheritable(:format)
end

#format=(format) ⇒ Object



538
539
540
# File 'lib/grape/util/inheritable_setting.rb', line 538

def format=(format)
  set_inheritable(:format, format)
end

#formattersObject



406
407
408
# File 'lib/grape/util/inheritable_setting.rb', line 406

def formatters
  namespace_stackable_with_hash(:formatters)
end

#globalObject

Return the class-level global properties.



82
83
84
# File 'lib/grape/util/inheritable_setting.rb', line 82

def global
  self.class.global
end

#grape_exceptions_rescue_handlerObject



363
364
365
# File 'lib/grape/util/inheritable_setting.rb', line 363

def grape_exceptions_rescue_handler
  inheritable(:grape_exceptions_rescue_handler)
end

#hashObject

Keyed on the fully resolved state, because #== accepts two instances whose own stores differ as long as their chains resolve alike (the #to_hash path above) — hashing own state would tell those apart. The same-parent fast path implies equal resolved state, so it agrees. This is the cold path: nothing in Grape uses a setting as a Hash key or in a Set, and #== keeps avoiding #to_hash wherever it can.



246
247
248
# File 'lib/grape/util/inheritable_setting.rb', line 246

def hash
  to_hash.hash
end

#helpersObject

Helper modules registered by helpers blocks and modules (see DSL::Helpers), outermost scope first. Record them with #add_helper; the backing store is an internal detail.



459
460
461
# File 'lib/grape/util/inheritable_setting.rb', line 459

def helpers
  stacked(:helpers)
end

#inherit_from(parent) ⇒ Object

Inherit from the given parent: its values resolve behind ours from now on, including any it gains later. Also re-parents any settings instances which were forked from us.

Parameters:



90
91
92
93
94
95
96
97
98
# File 'lib/grape/util/inheritable_setting.rb', line 90

def inherit_from(parent)
  return if parent.nil?

  @parent = parent

  @route = parent.route.merge(route)

  @point_in_time_copies&.each { |cloned_one| cloned_one.inherit_from parent }
end

#inherit_route_params(parent) ⇒ Object

Fold a mounting parent scope's accumulated validations and declared params into this endpoint copy's per-route snapshots (see Endpoint#inherit_settings). Both are appended, so the parent's entries follow the ones already seeded from the surrounding scopes.



194
195
196
197
198
199
200
# File 'lib/grape/util/inheritable_setting.rb', line 194

def inherit_route_params(parent)
  parent_validations = parent.validations
  route_validations.concat(parent_validations) if parent_validations.any?

  parent_declared_params = parent.declared_params
  route_declared_params.concat(parent_declared_params.flatten) if parent_declared_params.any?
end

#internal_grape_exceptions_rescue_handlerObject



367
368
369
# File 'lib/grape/util/inheritable_setting.rb', line 367

def internal_grape_exceptions_rescue_handler
  inheritable(:internal_grape_exceptions_rescue_handler)
end

#lint!Object



642
643
644
# File 'lib/grape/util/inheritable_setting.rb', line 642

def lint!
  set_inheritable(:lint, true)
end

#lint?Boolean

Returns:

  • (Boolean)


646
647
648
# File 'lib/grape/util/inheritable_setting.rb', line 646

def lint?
  inheritable(:lint) == true
end

#middlewareObject

Middleware specs recorded by the middleware DSL (+use+, insert, insert_before, insert_after; see DSL::Middleware), one [operation, *arguments] Array per registration, outermost scope first. Record them with #add_middleware; the backing store is an internal detail.



448
449
450
# File 'lib/grape/util/inheritable_setting.rb', line 448

def middleware
  stacked(:middleware)
end

#mount_pathObject

The path a Grape API is mounted under, recorded on the mounted API's top-level settings by mount (see DSL::Routing). Reading returns the outermost mount path — nil when the API is not mounted; the backing store is an internal detail.



496
497
498
# File 'lib/grape/util/inheritable_setting.rb', line 496

def mount_path
  stacked(:mount_path).first
end

#mount_pathsObject

The full mount-path stack — one entry per mount level, outermost first; what Router::Pattern::Path joins into a route's origin (see #path_settings).



507
508
509
# File 'lib/grape/util/inheritable_setting.rb', line 507

def mount_paths
  stacked(:mount_path)
end

#named_paramsObject

Reusable params :name do ... end blocks defined in helpers, as one name => block Hash per scope, deep-merged on read; nil when none are defined. Consumed by use. Record entries with #add_named_params; the backing store is an internal detail.



296
297
298
# File 'lib/grape/util/inheritable_setting.rb', line 296

def named_params
  namespace_stackable_with_hash(:named_params)
end

#namespace_pathObject

The normalized path prefix formed by joining every registered namespace's space (see Grape::Namespace.joined_space_path).



482
483
484
# File 'lib/grape/util/inheritable_setting.rb', line 482

def namespace_path
  Grape::Namespace.joined_space_path(namespaces)
end

#namespace_requirementsObject

The param requirements declared by registered namespaces, outermost scope first.



488
489
490
# File 'lib/grape/util/inheritable_setting.rb', line 488

def namespace_requirements
  namespaces.filter_map(&:requirements)
end

#namespace_stackableObject

A StackableValues view of this scope's registrations, rebuilt on each call. Public for ecosystem compatibility only — grape-swagger reads it directly and walks its inherited_values chain — and read-only: it is a view, not the store, so writing to it registers nothing. Every semantic key has a dedicated accessor below; new code should use those.



42
43
44
# File 'lib/grape/util/inheritable_setting.rb', line 42

def namespace_stackable
  StackableValues.new(@stackable_values, parent&.namespace_stackable || {})
end

#namespacesObject

Grape::Namespace objects registered by the namespace DSL and its aliases (group, resource, resources, segment; see DSL::Routing), outermost scope first. Not to be confused with the #namespace values store. Record them with #add_namespace; the backing store is an internal detail.



472
473
474
# File 'lib/grape/util/inheritable_setting.rb', line 472

def namespaces
  stacked(:namespace)
end

#params_documentationObject

Param documentation recorded by params blocks (see Validations::ParamsDocumentation) as one attribute-name => details Hash per scope, deep-merged on read; nil when nothing is documented. Record entries with #add_params_documentation; the backing store is an internal detail.



277
278
279
# File 'lib/grape/util/inheritable_setting.rb', line 277

def params_documentation
  namespace_stackable_with_hash(:params)
end

#parsersObject



414
415
416
# File 'lib/grape/util/inheritable_setting.rb', line 414

def parsers
  namespace_stackable_with_hash(:parsers)
end

#path_settingsObject

Builds a PathSettings snapshot for Router::Pattern::Path (see Endpoint#to_routes). mount_path is the full stack — one entry per mount level, outermost first — unlike #mount_path, which returns only the outermost entry; content_types is the raw registration stack, because Path counts registrations rather than distinct formats. Unset members are nil.



690
691
692
693
694
695
696
697
698
699
# File 'lib/grape/util/inheritable_setting.rb', line 690

def path_settings
  PathSettings.new(
    mount_path: mount_paths.presence,
    root_prefix:,
    format:,
    content_types: stacked(:content_types).presence,
    version:,
    version_options:
  )
end

#point_in_time_copyObject

Create a point-in-time copy of this settings instance, with clones of all our values. Note that, should this instance's parent be set or changed via #inherit_from, it will copy that inheritence to any copies which were made.



104
105
106
107
108
109
110
# File 'lib/grape/util/inheritable_setting.rb', line 104

def point_in_time_copy
  new_setting = self.class.new
  (@point_in_time_copies ||= []) << new_setting
  new_setting.copy_state_from(self)
  new_setting.inherit_from(parent)
  new_setting
end

#point_in_time_copy_for_endpointObject

Fork a point-in-time copy prepared for a freshly-built endpoint: the declared params and validations accumulated by the surrounding scopes are snapshotted into the copy's per-route settings, since the namespace stacks are wiped between routes (see #reset_validations!), and request-serving defaults are applied.



117
118
119
120
121
122
123
# File 'lib/grape/util/inheritable_setting.rb', line 117

def point_in_time_copy_for_endpoint
  copy = point_in_time_copy
  copy.route_declared_params = copy.declared_params.flatten
  copy.route_validations = copy.validations.dup
  copy.default_error_status ||= 500
  copy
end

#representationsObject

Model-class => entity-class registrations from represent (see DSL::RequestResponse), one single-entry Hash per registration, deep-merged on read so a nested scope's registration wins; nil when none are registered. Record them with #add_representation; the backing store is an internal detail.



435
436
437
# File 'lib/grape/util/inheritable_setting.rb', line 435

def representations
  namespace_stackable_with_hash(:representations)
end

#rescue_all?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'lib/grape/util/inheritable_setting.rb', line 351

def rescue_all?
  inheritable(:rescue_all) == true
end

#rescue_grape_exceptions?Boolean

Returns:

  • (Boolean)


355
356
357
# File 'lib/grape/util/inheritable_setting.rb', line 355

def rescue_grape_exceptions?
  inheritable(:rescue_grape_exceptions) == true
end

#rescue_handlersObject

Rescue-handler maps registered by rescue_from, keyed by exception class and merged so a nested scope's handler wins. Record them with #add_rescue_handlers; the backing store is an internal detail.



374
375
376
# File 'lib/grape/util/inheritable_setting.rb', line 374

def rescue_handlers
  merged_rescue_handlers(:rescue_handlers)
end

#rescue_optionsObject

Response-shaping options recorded by rescue_from (see DSL::RescueOptions): every rescue_from stacks one entry and the nearest scope's latest registration wins on read; nil when rescue_from was never called. Record them with #add_rescue_options; the backing store is an internal detail.



322
323
324
# File 'lib/grape/util/inheritable_setting.rb', line 322

def rescue_options
  stacked(:rescue_options).last
end

#reset_validations!Object

Drops this scope's own validations, declared params and params documentation once an endpoint has consumed them (see reset_validations! in DSL::Validations). Inherited entries are kept.



288
289
290
# File 'lib/grape/util/inheritable_setting.rb', line 288

def reset_validations!
  unstack(:declared_params, :params, :validations)
end

#root_prefixObject



588
589
590
# File 'lib/grape/util/inheritable_setting.rb', line 588

def root_prefix
  inheritable(:root_prefix)
end

#root_prefix=(prefix) ⇒ Object



592
593
594
# File 'lib/grape/util/inheritable_setting.rb', line 592

def root_prefix=(prefix)
  set_inheritable(:root_prefix, prefix)
end

#route_declared_paramsObject



145
146
147
# File 'lib/grape/util/inheritable_setting.rb', line 145

def route_declared_params
  @route[:declared_params]
end

#route_declared_params=(declared_params) ⇒ Object



149
150
151
# File 'lib/grape/util/inheritable_setting.rb', line 149

def route_declared_params=(declared_params)
  @route[:declared_params] = declared_params
end

#route_descriptionObject

Endpoint description recorded by desc (see DSL::Desc), consumed by route. An empty Hash when desc was never called.



166
167
168
# File 'lib/grape/util/inheritable_setting.rb', line 166

def route_description
  @route[:description] || {}
end

#route_description=(description) ⇒ Object



170
171
172
# File 'lib/grape/util/inheritable_setting.rb', line 170

def route_description=(description)
  @route[:description] = description
end

#route_endObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the instance store of per-route settings.



127
128
129
# File 'lib/grape/util/inheritable_setting.rb', line 127

def route_end
  @route = {}
end

#route_renamed_paramsObject

Path => renamed-name map recorded by as: (see ParamsScope), consumed by #declared. Record entries with #add_route_renamed_param; an empty Hash when nothing was renamed.



156
157
158
# File 'lib/grape/util/inheritable_setting.rb', line 156

def route_renamed_params
  @route[:renamed_params] || {}
end

#route_setting(key, value = nil) ⇒ Object

Read (when value is nil) or write an arbitrary route-scoped setting. This is the open store behind the route_setting DSL; the known keys have the dedicated accessors above.



184
185
186
187
188
# File 'lib/grape/util/inheritable_setting.rb', line 184

def route_setting(key, value = nil)
  return @route[key] if value.nil?

  @route[key] = value
end

#route_settingsObject

The route-scope settings handed to each Grape::Router::Route: every route_setting registration plus the description, minus the internal param snapshots (#route_validations / #route_declared_params).



177
178
179
# File 'lib/grape/util/inheritable_setting.rb', line 177

def route_settings
  route.except(:declared_params, :validations)
end

#route_validationsObject

Validator instances and declared-params entries for the route currently being built. Unlike the same-named namespace stacks (#validations / #declared_params), these are flat per-route snapshots: seeded when an endpoint copy is forked (see #point_in_time_copy_for_endpoint), topped up from mounting parents (see Endpoint#inherit_settings), and read back by run_validators / #declared.



137
138
139
# File 'lib/grape/util/inheritable_setting.rb', line 137

def route_validations
  @route[:validations]
end

#route_validations=(validations) ⇒ Object



141
142
143
# File 'lib/grape/util/inheritable_setting.rb', line 141

def route_validations=(validations)
  @route[:validations] = validations
end

#to_hashObject

Return a serializable hash of our values.



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/grape/util/inheritable_setting.rb', line 203

def to_hash
  {
    global: global.clone,
    route: route.clone,
    namespace: namespace.dup,
    namespace_inheritable: inheritable_values,
    namespace_stackable: stacked_keys.to_h { |key| [key, stacked(key)] },
    rescue_handlers:,
    base_only_rescue_handlers:
  }
end

#validationsObject

Validator instances registered by params and contract blocks, outermost scope first. Record them with #add_validation; the backing store is an internal detail.



253
254
255
# File 'lib/grape/util/inheritable_setting.rb', line 253

def validations
  stacked(:validations)
end

#versionObject

Versioning state recorded by the routing DSL (see DSL::Routing): version holds the Array of version strings registered by the version DSL method, version_options its DSL::VersionOptions value object, and root_prefix the path prefix set by prefix. Nearest-wins scalars with plain += writers; readers return nil when never set; the backing store is an internal detail.



572
573
574
# File 'lib/grape/util/inheritable_setting.rb', line 572

def version
  inheritable(:version)
end

#version=(versions) ⇒ Object



576
577
578
# File 'lib/grape/util/inheritable_setting.rb', line 576

def version=(versions)
  set_inheritable(:version, versions)
end

#version_optionsObject



580
581
582
# File 'lib/grape/util/inheritable_setting.rb', line 580

def version_options
  inheritable(:version_options)
end

#version_options=(options) ⇒ Object



584
585
586
# File 'lib/grape/util/inheritable_setting.rb', line 584

def version_options=(options)
  set_inheritable(:version_options, options)
end