Class: Sinclair::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/sinclair/options.rb,
lib/sinclair/options/builder.rb

Overview

Base options class

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'

Author:

  • Darthjee

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of 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'

Parameters:

  • options (Hash) (defaults to: {})

    hash with options (see options, with_options)



84
85
86
87
88
89
90
# File 'lib/sinclair/options.rb', line 84

def initialize(options = {})
  check_options(options)

  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Class 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>)


44
45
46
# File 'lib/sinclair/options.rb', line 44

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>)


54
55
56
# File 'lib/sinclair/options.rb', line 54

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>)


31
32
33
# File 'lib/sinclair/options.rb', line 31

def invalid_options_in(names)
  names.map(&:to_sym) - allowed_options.to_a
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:



77
78
79
# File 'lib/sinclair/options.rb', line 77

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

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

returns if other equals to self

Parameters:

  • other (Object)

    object to be compared

Returns:

  • (TrueClass, FalseClass)


97
98
99
100
101
102
103
# File 'lib/sinclair/options.rb', line 97

def ==(other)
  return false unless self.class == other.class

  self.class.allowed_options.all? do |name|
    public_send(name) == other.public_send(name)
  end
end