class Redress::Utils::ParseAttributesFromParams

Public Class Methods

new(klass, params, options = nil) click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 8
def initialize(klass, params, options = nil)
  @klass = klass
  @params = convert_params_to_hash(params)
  @options = (options || {})
end

Public Instance Methods

attributes() click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 14
def attributes
  @attributes ||= extract_attributes
end
Also aliased as: to_h
full_prefix() click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 29
def full_prefix
  @full_prefix ||= "#{prefix}_"
end
model_attributes() click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 19
def model_attributes
  @params.fetch(@klass.mimicked_model_name, {})
end
prefix() click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 23
def prefix
  return if @options[:prefix].blank?

  @prefix ||= @options[:prefix].to_s
end
to_h()
Alias for: attributes

Private Instance Methods

convert_params_to_hash(params) click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 60
def convert_params_to_hash(params)
  if params.respond_to?(:to_unsafe_h)
    params.to_unsafe_h
  else
    params
  end
end
extract_attributes() click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 35
def extract_attributes
  hash = model_attributes.merge!(prefix_attibutes)
  declared_keys = @klass.schema.keys.map(&:name)

  AttributesHash.new(hash).extract!(*declared_keys)
end
prefix_attibutes() click to toggle source
# File lib/redress/utils/parse_attributes_from_params.rb, line 42
def prefix_attibutes
  return {} if prefix.nil?

  hash = {}

  @params.each do |key, value|
    new_key = key.to_s
    next unless new_key.start_with?(prefix)

    new_key = new_key.dup if new_key.frozen?
    new_key.slice!(full_prefix)

    hash[new_key] = value
  end

  hash
end