module Subroutine::Fields

Public Class Methods

action_controller_params_loaded?() click to toggle source
# File lib/subroutine/fields.rb, line 25
def self.action_controller_params_loaded?
  defined?(::ActionController::Parameters)
end
allowed_input_classes() click to toggle source
# File lib/subroutine/fields.rb, line 17
def self.allowed_input_classes
  @allowed_input_classes ||= begin
    out = [Hash]
    out << ActionController::Parameters if action_controller_params_loaded?
    out
  end
end

Public Instance Methods

all_default_params() click to toggle source
# File lib/subroutine/fields.rb, line 188
def all_default_params
  get_param_group(:default)
end
Also aliased as: defaults
all_params() click to toggle source
# File lib/subroutine/fields.rb, line 183
def all_params
  get_param_group(:all)
end
Also aliased as: params
all_params_with_defaults() click to toggle source
# File lib/subroutine/fields.rb, line 193
def all_params_with_defaults
  all_default_params.merge(all_params)
end
Also aliased as: params_with_defaults
clear_field(name) click to toggle source
# File lib/subroutine/fields.rb, line 231
def clear_field(name)
  each_param_group_for_field(name) do |h|
    h.delete(name)
  end
end
defaults()
Alias for: all_default_params
field_provided?(key) click to toggle source

check if a specific field was provided

# File lib/subroutine/fields.rb, line 211
def field_provided?(key)
  !!@provided_fields[key]
end
fields_in_group(group_name) click to toggle source
# File lib/subroutine/fields.rb, line 237
def fields_in_group(group_name)
  self.class.fields_in_group(group_name)
end
get_field(name) click to toggle source
# File lib/subroutine/fields.rb, line 215
def get_field(name)
  field_provided?(name) ? all_params[name] : all_default_params[name]
end
get_field_config(field_name) click to toggle source
# File lib/subroutine/fields.rb, line 206
def get_field_config(field_name)
  self.class.get_field_config(field_name)
end
get_param_group(name) click to toggle source
# File lib/subroutine/fields.rb, line 171
def get_param_group(name)
  param_groups[name.to_sym]
end
original_params() click to toggle source
# File lib/subroutine/fields.rb, line 175
def original_params
  get_param_group(:original)
end
param_groups() click to toggle source
# File lib/subroutine/fields.rb, line 167
def param_groups
  @param_groups ||= Hash.new { |h, k| h[k] = {}.with_indifferent_access }
end
params()
Alias for: all_params
params_with_defaults()
set_field(name, value, track_provided: true) click to toggle source
# File lib/subroutine/fields.rb, line 219
def set_field(name, value, track_provided: true)
  config = get_field_config(name)
  @provided_fields[name] = true if track_provided
  value = attempt_cast(value, config) do |e|
    "Error during assignment of field `#{name}`: #{e}"
  end
  each_param_group_for_field(name) do |h|
    h[name] = value
  end
  value
end
setup_fields(inputs = {}) click to toggle source
# File lib/subroutine/fields.rb, line 158
def setup_fields(inputs = {})
  if ::Subroutine::Fields.action_controller_params_loaded? && inputs.is_a?(::ActionController::Parameters)
    inputs = inputs.to_unsafe_h if inputs.respond_to?(:to_unsafe_h)
  end
  @provided_fields = {}.with_indifferent_access
  param_groups[:original] = inputs.with_indifferent_access
  mass_assign_initial_params
end
ungrouped_defaults() click to toggle source
# File lib/subroutine/fields.rb, line 198
def ungrouped_defaults
  default_params.slice(*ungrouped_fields.keys)
end
ungrouped_fields() click to toggle source
# File lib/subroutine/fields.rb, line 241
def ungrouped_fields
  fields.select { |f| f.groups.empty? }.each_with_object({}) do |f, h|
    h[f.name] = f
  end
end
ungrouped_params() click to toggle source
# File lib/subroutine/fields.rb, line 179
def ungrouped_params
  get_param_group(:ungrouped)
end
ungrouped_params_with_defaults() click to toggle source
# File lib/subroutine/fields.rb, line 202
def ungrouped_params_with_defaults
  ungrouped_defaults.merge(ungrouped_params)
end

Protected Instance Methods

attempt_cast(value, config) { |e| ... } click to toggle source
# File lib/subroutine/fields.rb, line 269
def attempt_cast(value, config)
  ::Subroutine::TypeCaster.cast(value, config)
rescue ::Subroutine::TypeCaster::TypeCastError => e
  message = block_given? ? yield(e) : e.to_s
  raise ::Subroutine::TypeCaster::TypeCastError, message, e.backtrace
end
each_param_group_for_field(name) { |all_params| ... } click to toggle source
# File lib/subroutine/fields.rb, line 276
def each_param_group_for_field(name)
  config = get_field_config(name)
  yield all_params

  if config.groups.empty?
    yield ungrouped_params
  else
    config.groups.each do |group_name|
      yield param_groups[group_name]
    end
  end
end
mass_assign_initial_params() click to toggle source
# File lib/subroutine/fields.rb, line 249
def mass_assign_initial_params
  field_configurations.each_pair do |field_name, config|
    if !config.mass_assignable? && original_params.key?(field_name)
      raise ::Subroutine::Fields::MassAssignmentError, field_name
    end

    if original_params.key?(field_name)
      set_field(field_name, original_params[field_name])
    end

    next unless config.has_default?

    value = attempt_cast(config.get_default, config) do |e|
      "Error for default `#{field}`: #{e}"
    end

    param_groups[:default][field_name] = value
  end
end