Sha256: e9a8606722a7f87e13000356942274acfdb39b40e75dd353a2ce9c09ad9c4742

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

module Trailblazer
  class Endpoint
    # you don't need this if you build your endpoints manually
    class Builder < Trailblazer::Activity::Railway
      # step :build_policy
      step :build_protocol_block
      step :normalize_tuple

      # def build_policy(ctx, policies:, **)
      # end

      def build_protocol_block(ctx, policy:, **)
        ctx[:protocol_block] = -> { step Subprocess(policy), id: :policy, replace: :policy, inherit: true; {} }
      end

      def normalize_tuple(ctx, protocol_block:, options_for_build: {}, **)
        ctx[:build_options] = {
          protocol_block:    protocol_block,
          options_for_build: options_for_build
        }
      end


      module DSL
        module_function
        #
        # @return endpoint_options

        def build_options_for(builder:, **options)
          signal, (ctx, _) = builder.([options])

          ctx[:build_options] # ["web:submitted?", {protocol_block: ..., options_for_build: ...}]
        end

        def endpoint_for(id:, builder:, default_options:, **config)
          options = build_options_for(builder: builder, **config)

          return id, Trailblazer::Endpoint.build(default_options.merge(options[:options_for_build]), &options[:protocol_block])
        end

        # {dsl_options} being something like
        #
        #   "api:Start.default" => {policies: []},
        #   "api:status?"       => {policies: [:user_owns_diagram]},
        #   "api:download?"     => {policies: [:user_owns_diagram]},
        #   "api:delete?"       => {policies: [:user_owns_diagram]},
        def endpoints_for(dsl_options, **options)
          endpoints = dsl_options.collect do |id, config|
            endpoint_for(id: id, **options, **config) # config is per endpoint, options are "global"
          end.to_h
        end
      end
    end # Builder

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trailblazer-endpoint-0.0.4 lib/trailblazer/endpoint/builder.rb
trailblazer-endpoint-0.0.3 lib/trailblazer/endpoint/builder.rb
trailblazer-endpoint-0.0.2 lib/trailblazer/endpoint/builder.rb
trailblazer-endpoint-0.0.1 lib/trailblazer/endpoint/builder.rb