Module: Sinclair::Options::ClassMethods

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

Overview

Class Methods for Sinclai::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



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



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



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


94
95
96
# File 'lib/sinclair/options/class_methods.rb', line 94

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



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'


69
70
71
# File 'lib/sinclair/options/class_methods.rb', line 69

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