Module: Railsmith::BaseService::InputDsl::ClassMethods

Defined in:
lib/railsmith/base_service/input_dsl.rb

Overview

Class-level DSL macros for declaring inputs on a service.

Instance Method Summary collapse

Instance Method Details

#filter_inputs(value = :__unset__) ⇒ Object

Controls whether undeclared params are dropped after resolution. Pass false to disable filtering (opt-out). Called with no argument returns the current setting.



49
50
51
52
53
54
55
# File 'lib/railsmith/base_service/input_dsl.rb', line 49

def filter_inputs(value = :__unset__)
  if value == :__unset__
    instance_variable_defined?(:@filter_inputs) ? @filter_inputs : true
  else
    @filter_inputs = value
  end
end

#inherited(subclass) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/railsmith/base_service/input_dsl.rb', line 57

def inherited(subclass)
  super
  subclass.instance_variable_set(:@input_registry, input_registry.dup)
  # Propagate filter_inputs setting only if explicitly set on this class.
  return unless instance_variable_defined?(:@filter_inputs)

  subclass.instance_variable_set(:@filter_inputs, @filter_inputs)
end

#input(name, type, **options) ⇒ Object

Declare an input parameter.

Parameters:

  • name (Symbol, String)

    parameter key

  • type (Class, Symbol)

    expected type; use :boolean for booleans

  • options (Hash)

    supported keys: :required, :default, :in, :transform



32
33
34
35
36
37
38
39
# File 'lib/railsmith/base_service/input_dsl.rb', line 32

def input(name, type, **options)
  input_registry.register(
    InputDefinition.new(
      name, type,
      **normalize_input_options(options)
    )
  )
end

#input_registryObject

Returns the InputRegistry for this class.



42
43
44
# File 'lib/railsmith/base_service/input_dsl.rb', line 42

def input_registry
  @input_registry ||= InputRegistry.new
end

#normalize_input_options(options) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/railsmith/base_service/input_dsl.rb', line 66

def normalize_input_options(options)
  {
    required: options.fetch(:required, false),
    default: options.fetch(:default, InputDefinition::UNSET),
    in: options[:in],
    transform: options[:transform]
  }
end