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 = { nil => proc { render_standalone(controller) } } # default format
  @authorize_block = nil
end

Instance Method Details

#authorize(&block) ⇒ Object (protected)

DSL This block is expected to return true if and only if current_ability has the right to access the component over the given verb.



38
39
40
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 38

def authorize(&block)
  @authorize_block = block
end

#respond(format = nil, &block) ⇒ Object (protected)

DSL This is the last step in the life cycle. It may redirect or render. If omitted, the default is standalone_render.

Parameters:

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

    Format this block should respond to, defaults to nil which means "all other formats".



45
46
47
# File 'lib/compony/component_mixins/default/standalone/verb_dsl.rb', line 45

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

#to_confObject

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



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

def to_conf(&)
  evaluate(&) if block_given?
  return {
    verb:            @verb,
    authorize_block: @authorize_block || proc { can?(comp_name.to_sym, family_name.to_sym) },
    respond_blocks:  @respond_blocks
  }.compact
end