Module: Trailblazer::Endpoint::Normalizer

Defined in:
lib/trailblazer/endpoint/options.rb

Overview

Options

Class Method Summary collapse

Class Method Details

.add(normalizer, directive_name, options) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/trailblazer/endpoint/options.rb', line 70

def self.add(normalizer, directive_name, options)
  Class.new(normalizer) do
    options.collect do |callable|
      step task: Normalizer.CallDirective(callable, directive_name), id: "#{directive_name}=>#{callable}"
    end
  end
end

.add_normalizer!(target, normalizer, config) ⇒ Object



64
65
66
67
68
# File 'lib/trailblazer/endpoint/options.rb', line 64

def self.add_normalizer!(target, normalizer, config)
  normalizer = Normalizer.add(normalizer, target, config) # add configure steps for {subclass} to the _new_ normalizer.
  target.instance_variable_set(:@normalizer, normalizer)
  target.instance_variable_set(:@config, config)
end

.CallDirective(callable, option_name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/trailblazer/endpoint/options.rb', line 78

def self.CallDirective(callable, option_name)
  ->((ctx, flow_options), *) {
    config = callable.(ctx, **ctx) # e.g. ApplicationController.options_for_endpoint

    # ctx[option_name] = ctx[option_name].merge(config)
    config.each do |k, v|
      ctx[k] = v
    end

    return Trailblazer::Activity::Right, [ctx, flow_options]
  }
end

.DefaultToEmptyHash(config_name) ⇒ Object



60
61
62
# File 'lib/trailblazer/endpoint/options.rb', line 60

def self.DefaultToEmptyHash(config_name)
  -> (ctx, **) { ctx[config_name] ||= {} }
end

.Options(directive_name, *callables, base_class: Trailblazer::Activity::Path) ⇒ Object



53
54
55
56
57
58
# File 'lib/trailblazer/endpoint/options.rb', line 53

def self.Options(directive_name, *callables, base_class: Trailblazer::Activity::Path)
  normalizer = Class.new(base_class) do
  end

  Normalizer.add(normalizer, directive_name, callables)
end