Class: Compony::ComponentMixins::Default::Standalone::VerbDsl

Inherits:
Dslblend::Base
  • Object
show all
Defined in:
lib/compony/component_mixins/default/standalone/verb_dsl.rb

Overview

DSL for speficying verb configs within a standalone config.

Direct Known Subclasses

ResourcefulVerbDsl

Constant Summary collapse

AVAILABLE_VERBS =
%i[get head post put delete connect options trace patch].freeze

Instance Method Summary collapse

Constructor Details

#initialize(component, verb) ⇒ VerbDsl

Returns a new instance of VerbDsl.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 12

def initialize(component, verb)
  super()

  verb = verb.to_sym
  fail "Unknown HTTP verb #{verb.inspect}, use one of #{AVAILABLE_VERBS.inspect}" unless AVAILABLE_VERBS.include?(verb)

  @component = component
  @verb = verb
  @respond_blocks = {}
  @authorize_block = nil
end

Instance Method Details

#authorize { ... } ⇒ void (protected)

This method returns an undefined value.

DSL Mandatory. The block must return truthy iff current_ability may access the component over this verb; a falsy result raises CanCan::AccessDenied.

Yields:

  • Runs in the component's request context; returns truthy to grant access.



43
44
45
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 43

def authorize(&block)
  @authorize_block = block
end

#default_configObject (protected)

Internal, do not use



59
60
61
62
63
64
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 59

def default_config
  return {
    authorize_block: proc { can?(comp_name.to_sym, family_name.to_sym) },
    respond_blocks:  { nil => proc { render_standalone(controller) } }
  }
end

#respond(format = nil) { ... } ⇒ void (protected)

This method returns an undefined value.

DSL Last step in the lifecycle. May redirect or render. If omitted, the default is render_standalone. NOTE: overriding respond replaces the default, which is where authorize is evaluated - re-check authorization yourself.

Parameters:

  • format (String, Symbol, nil) (defaults to: nil)

    Format this block responds to; nil means "all other formats".

Yields:

  • Runs in the component's request context; renders or redirects.



54
55
56
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 54

def respond(format = nil, &block)
  @respond_blocks[format&.to_sym] = block
end

#to_conf(provide_defaults:) ⇒ Object

For internal usage only, processes the block and returns a config hash.



25
26
27
28
29
30
31
32
33
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 25

def to_conf(provide_defaults:, &)
  evaluate(&) if block_given?
  base_config = provide_defaults ? default_config : {}
  return base_config.deep_merge({
    verb:            @verb,
    authorize_block: @authorize_block,
    respond_blocks:  @respond_blocks
  }.compact)
end