Class: ProxyRb::BasicConfiguration
- Inherits:
-
Object
- Object
- ProxyRb::BasicConfiguration
- 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
Defined Under Namespace
Classes: InConfigurationWrapper, Option
Class Method Summary collapse
- .known_options ⇒ Object
-
.option_accessor(name, opts = {}) ⇒ Object
Define an option reader and writer.
-
.option_reader(name, opts = {}) ⇒ Object
Define an option reader.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #configure {|Configuration| ... } ⇒ Object
-
#initialize ⇒ BasicConfiguration
constructor
Create configuration.
-
#make_copy ⇒ Object
Make deep dup copy of configuration.
-
#option?(name) ⇒ Boolean
Check if <name> is option.
-
#reset ⇒ Object
Reset configuration.
-
#set_if_option(name, *args) ⇒ Object
Set if name is option.
Constructor Details
#initialize ⇒ BasicConfiguration
Create configuration
94 95 96 |
# File 'lib/proxy_rb/basic_configuration.rb', line 94 def initialize initialize_configuration end |
Class Method Details
.known_options ⇒ Object
13 14 15 |
# File 'lib/proxy_rb/basic_configuration.rb', line 13 def @known_options ||= {} end |
.option_accessor(name, opts = {}) ⇒ Object
Define an option reader and writer
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()) : 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
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()) : 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) .values.map(&:value) == other..values.map(&:value) end |
#configure {|Configuration| ... } ⇒ Object
101 102 103 |
# File 'lib/proxy_rb/basic_configuration.rb', line 101 def configure yield self if block_given? end |
#make_copy ⇒ Object
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. = Marshal.load(Marshal.dump()) obj end |
#option?(name) ⇒ Boolean
Check if <name> is option
122 123 124 |
# File 'lib/proxy_rb/basic_configuration.rb', line 122 def option?(name) .any? { |_, v| v.name == name.to_sym } end |
#reset ⇒ Object
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 |