Class: Pastore::Params::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/pastore/params/settings.rb

Overview

Implements the logic for params settings storage for a controller.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(superklass) ⇒ Settings

Returns a new instance of Settings.



12
13
14
15
# File 'lib/pastore/params/settings.rb', line 12

def initialize(superklass)
  @super_params = superklass.pastore_params if superklass.respond_to?(:pastore_params)
  reset!
end

Instance Attribute Details

#invalid_params_cbkObject



26
27
28
# File 'lib/pastore/params/settings.rb', line 26

def invalid_params_cbk
  @invalid_params_cbk || @super_params&.invalid_params_cbk
end

#response_statusObject



30
31
32
# File 'lib/pastore/params/settings.rb', line 30

def response_status
  @response_status || @super_params&.response_status || :unprocessable_entity
end

Instance Method Details

#add(name, **options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pastore/params/settings.rb', line 46

def add(name, **options)
  options = { scope: @scope }.merge(options.symbolize_keys)

  raise ParamAlreadyDefinedError, "Param #{name} already defined" if @buffer.any? { |p| p.name == name }

  if @scope.present? && options[:scope].present? && @scope != options[:scope]
    error = "Scope overwrite attempt detected (current_scope: #{@scope}, param_scope: #{options[:scope]}) for param #{name}"
    raise ScopeConflictError, error
  end

  param = ActionParam.new(name, **options)

  @buffer << param
end

#reset!Object



17
18
19
20
21
22
23
24
# File 'lib/pastore/params/settings.rb', line 17

def reset!
  @actions = {}
  @invalid_params_cbk = nil
  @response_status = nil

  reset_buffer!
  reset_scope!
end

#reset_buffer!Object



34
35
36
# File 'lib/pastore/params/settings.rb', line 34

def reset_buffer!
  @buffer = []
end

#reset_scope!Object



42
43
44
# File 'lib/pastore/params/settings.rb', line 42

def reset_scope!
  @scope = nil
end

#save_for(action_name) ⇒ Object



61
62
63
64
# File 'lib/pastore/params/settings.rb', line 61

def save_for(action_name)
  @actions[action_name.to_sym] = @buffer
  reset_buffer!
end

#set_scope(*keys) ⇒ Object



38
39
40
# File 'lib/pastore/params/settings.rb', line 38

def set_scope(*keys)
  @scope = [keys].flatten.compact.map(&:to_sym)
end

#validate(params, action_name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pastore/params/settings.rb', line 66

def validate(params, action_name)
  action_params = @actions[action_name.to_sym]
  return {} if action_params.blank?

  action_params.each_with_object({}) do |validator, errors|
    value = safe_dig(params, *validator.scope, validator.name)
    validation = validator.validate(value)

    if validation.valid?
      update_param_value!(params, validator, validation)

      next if validation.errors.empty?
    end

    errors[validator.name_with_scope] = validation.errors
  end
end