Class: Sinclair::Options

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
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'

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

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.

Parameters:

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

    hash with options (see options, with_options)



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

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

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

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

returns if other equals to self

Parameters:

  • other (Object)

    object to be compared

Returns:

  • (TrueClass, FalseClass)


67
68
69
70
71
72
73
# File 'lib/sinclair/options.rb', line 67

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

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

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


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

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