Class: Aruba::BasicConfiguration
- Inherits:
-
Object
- Object
- Aruba::BasicConfiguration
- Includes:
- Contracts
- Defined in:
- lib/aruba/basic_configuration.rb,
lib/aruba/basic_configuration/option.rb
Overview
Basic Configuration
Direct Known Subclasses
Defined Under Namespace
Classes: Option
Class Method Summary collapse
- .known_options ⇒ Object
-
.option_accessor(name, type:, default: nil) ⇒ Object
Define an option reader and writer.
-
.option_reader(name, type:, default: nil) ⇒ Object
Define an option reader.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#after(name) { ... } ⇒ Object
Define after-hook.
-
#before(name) { ... } ⇒ Object
Define before-hook.
- #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.
-
#run_after_hook(name, context, *args) ⇒ Object
Run after-hook.
-
#run_before_hook(name, context, *args) ⇒ Object
Run before-hook.
-
#set_if_option(name, *args) ⇒ Object
Set if name is option.
Constructor Details
#initialize ⇒ BasicConfiguration
Create configuration
82 83 84 |
# File 'lib/aruba/basic_configuration.rb', line 82 def initialize initialize_configuration end |
Class Method Details
.known_options ⇒ Object
16 17 18 |
# File 'lib/aruba/basic_configuration.rb', line 16 def @known_options ||= {} end |
.option_accessor(name, type:, default: nil) ⇒ Object
Define an option reader and writer
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/aruba/basic_configuration.rb', line 50 def option_accessor(name, type:, default: nil) raise ArgumentError, 'Either use block or default value' if block_given? && default # Add writer add_option(name, block_given? ? yield(InConfigWrapper.new()) : default) Contract type => type define_method(:"#{name}=") { |v| find_option(name).value = v } # Add reader option_reader name, type: type end |
.option_reader(name, type:, default: nil) ⇒ Object
Define an option reader
30 31 32 33 34 35 36 37 |
# File 'lib/aruba/basic_configuration.rb', line 30 def option_reader(name, type:, default: nil) raise ArgumentError, 'Either use block or default value' if block_given? && default add_option(name, block_given? ? yield(InConfigWrapper.new()) : default) Contract None => type define_method(name) { find_option(name).value } end |
Instance Method Details
#==(other) ⇒ Object
179 180 181 |
# File 'lib/aruba/basic_configuration.rb', line 179 def ==(other) .values.map(&:value) == other..values.map(&:value) end |
#after(name) { ... } ⇒ Object
Define after-hook
146 147 148 149 150 151 152 153 |
# File 'lib/aruba/basic_configuration.rb', line 146 def after(name, &block) name = format('%s_%s', 'after_', name.to_s).to_sym raise ArgumentError, 'A block is required' unless block @hooks.append(name, block) self end |
#before(name) { ... } ⇒ Object
Define before-hook
114 115 116 117 118 119 120 121 |
# File 'lib/aruba/basic_configuration.rb', line 114 def before(name, &block) name = format('%s_%s', 'before_', name.to_s).to_sym raise ArgumentError, 'A block is required' unless block @hooks.append(name, block) self end |
#configure {|Configuration| ... } ⇒ Object
89 90 91 |
# File 'lib/aruba/basic_configuration.rb', line 89 def configure yield self if block_given? end |
#make_copy ⇒ Object
Make deep dup copy of configuration
99 100 101 102 103 104 105 |
# File 'lib/aruba/basic_configuration.rb', line 99 def make_copy obj = dup obj. = Marshal.load(Marshal.dump()) obj.hooks = @hooks obj end |
#option?(name) ⇒ Boolean
Check if <name> is option
175 176 177 |
# File 'lib/aruba/basic_configuration.rb', line 175 def option?(name) .any? { |_, v| v.name == name.to_sym } end |
#reset ⇒ Object
Reset configuration
94 95 96 |
# File 'lib/aruba/basic_configuration.rb', line 94 def reset initialize_configuration end |
#run_after_hook(name, context, *args) ⇒ Object
Run after-hook
165 166 167 168 169 |
# File 'lib/aruba/basic_configuration.rb', line 165 def run_after_hook(name, context, *args) name = format('%s_%s', 'after_', name.to_s).to_sym @hooks.execute(name, context, *args) end |
#run_before_hook(name, context, *args) ⇒ Object
Run before-hook
133 134 135 136 137 |
# File 'lib/aruba/basic_configuration.rb', line 133 def run_before_hook(name, context, *args) name = format('%s_%s', 'before_', name.to_s).to_sym @hooks.execute(name, context, *args) end |
#set_if_option(name, *args) ⇒ Object
Set if name is option
184 185 186 |
# File 'lib/aruba/basic_configuration.rb', line 184 def set_if_option(name, *args) public_send(:"#{name}=", *args) if option? name end |