class Resourcerer::Configuration

Internal: Normalizes configuration options by providing common shortcuts for certain options. These shortcuts make the library easier to use.

Examples:

find_by: :name     ->(name, collection) { collection.find_by(name: name)}
assign?: :update   -> { action_name == 'update' }
id: :person_id     -> { params[:person_id] }

Constants

OPTIONS

Public: Available configuration options for a Resource.

Attributes

options[R]

Public Class Methods

new(options, &block) click to toggle source

Public: Normalizes configuration options for a Resource, ensuring every relevant option is assigned a Proc.

options - Config Hash for the new Resource. See OPTIONS. block - If supplied, the block is executed to provide options.

Returns a Hash where every value is a Proc.

# File lib/resourcerer/configuration.rb, line 35
def initialize(options, &block)
  @options = options
  instance_eval(&block) if block_given?

  assert_incompatible_options_pair :find_by, :find
  assert_incompatible_options_pair :permit, :attrs

  normalize_assign_option
  normalize_attrs_option
  normalize_find_by_option
  normalize_id_option
  normalize_model_option
  normalize_permit_option

  assert_proc_options *OPTIONS
end

Public Instance Methods

apply(other_options) click to toggle source

Public: Applies the configuration to the specified options. Does not override an option if it had previously been specified.

Returns the updated configuration options.

# File lib/resourcerer/configuration.rb, line 56
def apply(other_options)
  other_options.reverse_merge!(options)
end

Private Instance Methods

assert_incompatible_options_pair(key1, key2) click to toggle source

Internal: Performs a basic assertion to fail early if the specified options would result in undetermined behavior.

# File lib/resourcerer/configuration.rb, line 160
def assert_incompatible_options_pair(key1, key2)
  if options.key?(key1) && options.key?(key2)
    raise ArgumentError, "Using #{key1.inspect} option with #{key2.inspect} does not make sense"
  end
end
assert_proc_options(*names) click to toggle source

Internal: Asserts that the specified options are a Proc, if present.

# File lib/resourcerer/configuration.rb, line 150
def assert_proc_options(*names)
  names.each do |name|
    if options.key?(name) && !options[name].is_a?(Proc)
      raise ArgumentError, "Can't handle #{name.inspect} => #{options[name].inspect} option"
    end
  end
end
normalize_assign_option() click to toggle source

Internal: Normalizes the `assign?` option to be a Proc.

Example:

assign?: false  -> { false }
assign?: :update  -> { action_name == 'update' }
assign?: [:edit, :update]  -> { action_name.in?(['edit', 'update']) }
# File lib/resourcerer/configuration.rb, line 98
def normalize_assign_option
  bool = options[:assign?]
  options[:assign?] = -> { bool } if bool == !!bool

  option_to_proc :assign? do |*actions|
    actions = Set.new(actions.map(&:to_s))
    -> { actions.member?(action_name) }
  end
end
normalize_attrs_option() click to toggle source

Internal: Normalizes the `attrs` option to be a Proc.

Example:

attrs: :person_params  -> { person_params }
# File lib/resourcerer/configuration.rb, line 112
def normalize_attrs_option
  option_to_proc :attrs do |params_method|
    -> { send(params_method) }
  end
end
normalize_find_by_option() click to toggle source

Internal: Normalizes the `find_by` option to be a `find` Proc.

Example:

find_by: :name  ->(name, collection) { collection.find_by(name: name)}
# File lib/resourcerer/configuration.rb, line 76
def normalize_find_by_option
  if find_by = options.delete(:find_by)
    options[:find] = ->(id, scope) { scope.find_by!(find_by => id) }
  end
end
normalize_id_option() click to toggle source

Internal: Normalizes the `id` option to be a Proc.

Example:

id: :person_id  -> { params[:person_id] }
# File lib/resourcerer/configuration.rb, line 122
def normalize_id_option
  option_to_proc :id do |*ids|
    -> { ids.map { |id| params[id] }.find(&:present?) }
  end
end
normalize_model_option() click to toggle source

Internal: Normalizes the `model` option to be a Proc.

Example:

model: :electric_guitar  -> { ElectricGuitar }
# File lib/resourcerer/configuration.rb, line 132
def normalize_model_option
  option_to_proc :model do |value|
    model = case value
    when String, Symbol then value.to_s.classify.constantize
    else value
    end

    -> { model }
  end
end
normalize_permit_option() click to toggle source

Internal: Normalizes the `permit` option to be a Proc.

Example:

permit: [:name]  -> { [:name] }
# File lib/resourcerer/configuration.rb, line 86
def normalize_permit_option
  option_to_proc :permit do |*fields|
    -> { fields }
  end
end
option_to_proc(name) { |*option| ... } click to toggle source

Internal: Helper to normalize a non-proc value passed as an option.

# File lib/resourcerer/configuration.rb, line 144
def option_to_proc(name)
  return unless option = options[name]
  options[name] = yield(*option) unless option.is_a?(Proc)
end