Class: ProxyRb::BasicConfiguration

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/proxy_rb/basic_configuration.rb,
lib/proxy_rb/basic_configuration/option.rb,
lib/proxy_rb/basic_configuration/in_configuration_wrapper.rb

Overview

Basic Configuration

Direct Known Subclasses

Configuration

Defined Under Namespace

Classes: InConfigurationWrapper, Option

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicConfiguration

Create configuration



94
95
96
# File 'lib/proxy_rb/basic_configuration.rb', line 94

def initialize
  initialize_configuration
end

Class Method Details

.known_optionsObject



13
14
15
# File 'lib/proxy_rb/basic_configuration.rb', line 13

def known_options
  @known_options ||= {}
end

.option_accessor(name, opts = {}) ⇒ Object

Define an option reader and writer

Parameters:

  • name (Symbol)

    The name of the reader

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

    Options

  • [Class, (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/proxy_rb/basic_configuration.rb', line 58

def option_accessor(name, opts = {})
  contract = opts[:contract]
  default  = opts[:default]

  raise ArgumentError, 'Either use block or default value' if block_given? && default
  # fail ArgumentError, 'Either use block or default value' if !block_given? && default.nil? && default.to_s.empty?
  raise ArgumentError, 'contract-options is required' if contract.nil?

  # Add writer
  add_option(name, block_given? ? yield(InConfigWrapper.new(known_options)) : default)

  Contract contract
  define_method("#{name}=") { |v| find_option(name).value = v }

  # Add reader
  option_reader name, contract: { None => contract.values.first }
end

.option_reader(name, opts = {}) ⇒ Object

Define an option reader

Parameters:

  • name (Symbol)

    The name of the reader

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

    Options

  • [Class, (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/proxy_rb/basic_configuration.rb', line 30

def option_reader(name, opts = {})
  contract = opts[:contract]
  default  = opts[:default]

  raise ArgumentError, 'Either use block or default value' if block_given? && default
  raise ArgumentError, 'contract-options is required' if contract.nil?

  Contract contract
  add_option(name, block_given? ? yield(InConfigurationWrapper.new(known_options)) : default)

  define_method(name) { find_option(name).value }

  self
end

Instance Method Details

#==(other) ⇒ Object



126
127
128
# File 'lib/proxy_rb/basic_configuration.rb', line 126

def ==(other)
  local_options.values.map(&:value) == other.local_options.values.map(&:value)
end

#configure {|Configuration| ... } ⇒ Object

Yields:



101
102
103
# File 'lib/proxy_rb/basic_configuration.rb', line 101

def configure
  yield self if block_given?
end

#make_copyObject

Make deep dup copy of configuration



111
112
113
114
115
116
# File 'lib/proxy_rb/basic_configuration.rb', line 111

def make_copy
  obj = dup
  obj.local_options = Marshal.load(Marshal.dump(local_options))

  obj
end

#option?(name) ⇒ Boolean

Check if <name> is option

Parameters:

  • name (String, Symbol)

    The name of the option

Returns:

  • (Boolean)


122
123
124
# File 'lib/proxy_rb/basic_configuration.rb', line 122

def option?(name)
  local_options.any? { |_, v| v.name == name.to_sym }
end

#resetObject

Reset configuration



106
107
108
# File 'lib/proxy_rb/basic_configuration.rb', line 106

def reset
  initialize_configuration
end

#set_if_option(name, *args) ⇒ Object

Set if name is option



131
132
133
134
# File 'lib/proxy_rb/basic_configuration.rb', line 131

def set_if_option(name, *args)
  send("#{name}=".to_sym, *args) if option? name
  public_send("#{name}=".to_sym, *args) if option? name
end