Module: Apiwork::Configurable

Extended by:
ActiveSupport::Concern
Included in:
Adapter::Base, Adapter::Capability::Base, Export::Base
Defined in:
lib/apiwork/configurable.rb

Class Method Summary collapse

Class Method Details

.define(extends: nil, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/apiwork/configurable.rb', line 8

def define(extends: nil, &block)
  Class.new do
    include Configurable

    self.options = extends.options.dup if extends

    class_eval(&block) if block
  end
end

.option(name, type:, default: nil, enum: nil) { ... } ⇒ void

This method returns an undefined value.

Defines a configuration option.

For nested options, use ‘type: :hash` with a block. Inside the block, call `option` to define child options.

Examples:

Symbol option

option :locale, type: :symbol, default: :en

String option with enum

option :version, type: :string, default: '5', enum: %w[4 5]

Nested options

option :pagination, type: :hash do
  option :strategy, type: :symbol, default: :offset, enum: %i[offset cursor]
  option :default_size, type: :integer, default: 20
  option :max_size, type: :integer, default: 100
end

Parameters:

  • name (Symbol)

    The option name.

  • type (Symbol)
    :boolean, :hash, :integer, :string, :symbol

    The option type.

  • default (Object, nil) (defaults to: nil)

    (nil) The default value.

  • enum (Array, nil) (defaults to: nil)

    (nil) The allowed values.

Yields:

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/apiwork/configurable.rb', line 56

class_methods do
  def inherited(subclass)
    super
    subclass.options = options.dup
  end

  def option(name, default: nil, enum: nil, type:, &block)
    options[name] = Configuration::Option.new(name, type, default:, enum:, &block)
  end

  def default_options
    options.transform_values(&:effective_default).compact
  end
end