Module: Sinclair::Options::ClassMethods

Included in:
Sinclair::Options
Defined in:
lib/sinclair/options/class_methods.rb

Overview

Class Methods for Sinclair::Options

Instance Method Summary collapse

Instance Method Details

#allow(name) ⇒ Set<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Allow new option

This does not create the method

Parameters:

  • name (String, Symbol)

    options to be allowed

Returns:

  • (Set<Symbol>)


27
28
29
# File 'lib/sinclair/options/class_methods.rb', line 27

def allow(name)
  allowed_options << name.to_sym
end

#allowed_optionsSet<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Options allowed when initializing options

Returns:

  • (Set<Symbol>)


37
38
39
# File 'lib/sinclair/options/class_methods.rb', line 37

def allowed_options
  @allowed_options ||= superclass.try(:allowed_options).dup || Set.new
end

#invalid_options_in(names) ⇒ Array<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

returns invalid options

Returns:

  • (Array<Symbol>)


14
15
16
# File 'lib/sinclair/options/class_methods.rb', line 14

def invalid_options_in(names)
  names.map(&:to_sym) - allowed_options.to_a
end

#skip_validationTrueClass

Changes class to skip attributes validation

when initializing options, options will accept any arguments when validation is skipped

Examples:

class BuilderOptions < Sinclair::Options
  with_options :name

  skip_validation
end
options = BuilderOptions.new(name: 'Joe', age: 10)

options.name      # returns 'Joe'
options.try(:age) # returns nil

Returns:

  • (TrueClass)


90
91
92
# File 'lib/sinclair/options/class_methods.rb', line 90

def skip_validation
  @skip_validation = true
end

#skip_validation?TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

checks if class skips initialization validation

Returns:

  • (TrueClass, FalseClass)


46
47
48
# File 'lib/sinclair/options/class_methods.rb', line 46

def skip_validation?
  @skip_validation ||= superclass.try(:skip_validation?) || false
end

#with_options(*options) ⇒ Array<MethodDefinition> #with_options(*options, **defaults) ⇒ Array<MethodDefinition>

Add available options

Examples:

Options usage

class ConnectionOptions < Sinclair::Options
  with_options :timeout, :retries, port: 443, protocol: 'https'
end

options = ConnectionOptions.new(retries: 10, port: 8080)

options.timeout  # returns nil
options.retries  # returns 10
options.port     # returns 8080
options.protocol # returns 'https'

Overloads:

  • #with_options(*options) ⇒ Array<MethodDefinition>

    Parameters:

    • options (Array<Symbol>)

      list of accepted options

  • #with_options(*options, **defaults) ⇒ Array<MethodDefinition>

    Parameters:

    • options (Array<Symbol>)

      list of accepted options

    • defaults (Hash<Symbol,Object>)

      default options hash

Returns:



66
67
68
# File 'lib/sinclair/options/class_methods.rb', line 66

def with_options(*options)
  Builder.new(self, *options).build
end