Module: Railsmith::BaseService::InputDsl

Included in:
Railsmith::BaseService
Defined in:
lib/railsmith/base_service/input_dsl.rb

Overview

Adds the class-level ‘input` DSL to BaseService and its subclasses.

Usage:

class UserService < Railsmith::BaseService
  model User
  domain :identity

  input :email,    String,   required: true
  input :age,      Integer,  default: nil
  input :role,     String,   in: %w[admin member guest], default: "member"
  input :active,   :boolean, default: true
  input :metadata, Hash,     default: -> { {} }
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
# File 'lib/railsmith/base_service/input_dsl.rb', line 21

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#resolve_inputsObject

Instance-level helper: run the resolver against the appropriate param slice. Returns a Result. On success, updates @params in place with the resolved hash. Called by BaseService#call before action dispatch when inputs are declared.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/railsmith/base_service/input_dsl.rb', line 79

def resolve_inputs
  registry = self.class.input_registry
  return Railsmith::Result.success(value: @params) unless registry.any?

  resolver = InputResolver.new(registry, filter: self.class.filter_inputs)

  # When a model is declared, inputs describe the attributes hash; otherwise raw params.
  if attributes_params?
    resolve_attribute_inputs(resolver)
  else
    resolve_raw_inputs(resolver)
  end
end