Sha256: 47985c375242f1aeffcccf2eb73f58922252298ffdf3ca6da56a494f543c3845

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

module Compony
  module ComponentMixins
    module Default
      module Standalone
        # @api description
        # DSL for speficying verb configs within a standalone config.
        # @see Compony::ComponentMixins::Default::Standalone::VerbDsl for the verb DSL for resourceful components
        # @see Compony::ComponentMixins::Default::Standalone::StandaloneDsl
        class VerbDsl < Dslblend::Base
          AVAILABLE_VERBS = %i[get head post put delete connect options trace patch].freeze

          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

          # For internal usage only, processes the block and returns a config hash.
          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

          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.
          def authorize(&block)
            @authorize_block = block
          end

          # DSL
          # This is the last step in the life cycle. It may redirect or render. If omitted, the default is standalone_render.
          # @param format [String, Symbol] Format this block should respond to, defaults to `nil` which means "all other formats".
          def respond(format = nil, &block)
            @respond_blocks[format&.to_sym] = block
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
compony-0.2.0 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.1.1 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.1.0 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.9 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.8 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.7 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.6 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.5 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.4 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.3 lib/compony/component_mixins/default/standalone/verb_dsl.rb
compony-0.0.2 lib/compony/component_mixins/default/standalone/verb_dsl.rb