Module: ChainOptions::Integration::ClassMethods

Defined in:
lib/chain_options/integration.rb

Instance Method Summary collapse

Instance Method Details

#available_chain_optionsObject



59
60
61
# File 'lib/chain_options/integration.rb', line 59

def available_chain_options
  @available_chain_options ||= {}
end

#chain_option(name, **options) ⇒ Object

Generates a combined getter and setter method for the option with the given name.

Parameters:

  • name (String, Symbol)
  • options (Hash)

Options Hash (**options):

  • :default (Object, Proc) — default: nil

    Sets the value which should be used whenever no custom value was set for this option

  • :invalid (Symbol) — default: :raise

    Sets the behaviour when an invalid value is given. If set to ‘:raise`, an ArgumentError is raised if an option validation fails, if set to `:default`, the default value is used instead of the invalid value

  • :validate (Proc) — default: nil

    Sets up a validation proc for the option value. See :invalid for information about what happens when a validation fails

  • :filter (Proc, Symbol) — default: nil

    An optional filter method to reject certain values. See ChainOptions::OptionSet#filter_value for more information

  • :transform (Proc, Symbol) — default: nil

    An optional transformation that transforms the given value. See ChainOptions::OptionSet#transform_value for more information.

  • :incremental (Boolean) — default: false

    If set to true, overriding an option value will no longer be possible. Instead, the whole option value is treated as an Array with each new value simply being appended to it.

    user.favourite_books('Momo', 'Neverending Story').favourite_books('Lord of the Rings', 'The Hobbit')
    => [["Momo", "Neverending Story"], ["Lord of the Rings", "The Hobbit"]]
    
  • :allow_block (Boolean) — default: false

    Sets whether the option value may be a proc object given through a block. If set to true, the following statement would result in ‘block` being saved as option value:

    instance.my_option(&block)
    


49
50
51
52
53
54
55
56
57
# File 'lib/chain_options/integration.rb', line 49

def chain_option(name, **options)
  available_chain_options[name.to_sym] = options

  ChainOptions::OptionSet.handle_warnings(name, **options)

  define_method(name) do |*args, &block|
    chain_option_set.handle_option_call(name, *args, &block)
  end
end