Class: Resourcerer::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/resourcerer/configuration.rb

Overview

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] }

Constant Summary collapse

OPTIONS =

Public: Available configuration options for a Resource.

[
  :assign,
  :assign?,
  :attrs,
  :build,
  :collection,
  :find,
  :find_by,
  :id,
  :model,
  :permit,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ Configuration

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/resourcerer/configuration.rb', line 26

def options
  @options
end

Instance Method Details

#apply(other_options) ⇒ Object

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

Returns the updated configuration options.



56
57
58
# File 'lib/resourcerer/configuration.rb', line 56

def apply(other_options)
  other_options.reverse_merge!(options)
end