Class: Sinclair::Options

Inherits:
Object show all
Extended by:
ClassMethods
Includes:
Comparable
Defined in:
lib/sinclair/options.rb,
lib/sinclair/options/builder.rb,
lib/sinclair/options/class_methods.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

Direct Known Subclasses

Model::BuilderOptions

Defined Under Namespace

Modules: ClassMethods Classes: Builder

Instance Method Summary collapse

Methods included from ClassMethods

allow, allowed_options, invalid_options_in, skip_validation, skip_validation?, with_options

Methods included from Comparable

#==

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)



31
32
33
34
35
36
37
# File 'lib/sinclair/options.rb', line 31

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

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

Instance Method Details

#to_hHash

Returns a hash with the current options

Examples:

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

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

options.to_h # returns
             # {
             #   port: 8080,
             #   retries: 10,
             #   timeout: nil,
             #   protocol: 'https'
             # }

Returns:

  • (Hash)


57
58
59
60
61
# File 'lib/sinclair/options.rb', line 57

def to_h
  allowed_options.inject({}) do |hash, option|
    hash.merge(option => public_send(option))
  end
end