Class: BothIsGood::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/both_is_good/configuration.rb

Direct Known Subclasses

LocalConfiguration

Constant Summary collapse

DEFAULTS =
{
  rate: 1.0,
  switch: nil,
  on_mismatch: nil,
  on_compare: nil,
  on_primary_error: nil,
  on_secondary_error: nil,
  on_hook_error: nil
}.freeze
ATTRIBUTES =
DEFAULTS.keys.freeze
UNSUPPLIED =
Object.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supplied_base = UNSUPPLIED, **overrides) ⇒ Configuration

Returns a new instance of Configuration.



22
23
24
25
26
# File 'lib/both_is_good/configuration.rb', line 22

def initialize(supplied_base = UNSUPPLIED, **overrides)
  base = base_config(supplied_base)
  apply_initial_values(base, **overrides)
  @comparators = base ? base.comparators.dup : Comparators::DEFAULT_COMPARATORS.dup
end

Class Method Details

.globalObject



18
19
20
# File 'lib/both_is_good/configuration.rb', line 18

def self.global
  @global ||= new(nil, **DEFAULTS)
end

Instance Method Details

#dupObject



28
# File 'lib/both_is_good/configuration.rb', line 28

def dup = self.class.new(self)

#on_compare=(value) ⇒ Object



59
60
61
62
# File 'lib/both_is_good/configuration.rb', line 59

def on_compare=(value)
  validate_hook!(:on_compare, value, [1])
  @on_compare = value
end

#on_hook_error=(value) ⇒ Object



74
75
76
77
# File 'lib/both_is_good/configuration.rb', line 74

def on_hook_error=(value)
  validate_hook!(:on_hook_error, value, [1])
  @on_hook_error = value
end

#on_mismatch=(value) ⇒ Object



54
55
56
57
# File 'lib/both_is_good/configuration.rb', line 54

def on_mismatch=(value)
  validate_hook!(:on_mismatch, value, [1])
  @on_mismatch = value
end

#on_primary_error=(value) ⇒ Object



64
65
66
67
# File 'lib/both_is_good/configuration.rb', line 64

def on_primary_error=(value)
  validate_hook!(:on_primary_error, value, [1])
  @on_primary_error = value
end

#on_secondary_error=(value) ⇒ Object



69
70
71
72
# File 'lib/both_is_good/configuration.rb', line 69

def on_secondary_error=(value)
  validate_hook!(:on_secondary_error, value, [1])
  @on_secondary_error = value
end

#rate=(value) ⇒ Object



42
43
44
45
46
47
# File 'lib/both_is_good/configuration.rb', line 42

def rate=(value)
  unless value.is_a?(Numeric) && (0.0..1.0).cover?(value)
    raise ArgumentError, "rate must be a number between 0.0 and 1.0, got #{value.inspect}"
  end
  @rate = value
end

#register_comparator(name, klass) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/both_is_good/configuration.rb', line 30

def register_comparator(name, klass)
  raise ArgumentError, "comparator name must be a Symbol" unless name.is_a?(Symbol)
  raise ArgumentError, "comparator must be a class" unless klass.is_a?(Class)
  unless klass.method_defined?(:call) && klass.instance_method(:call).arity == 0
    raise ArgumentError, "comparator class must define a zero-arity call instance method"
  end
  unless klass.instance_method(:initialize).arity == 2
    raise ArgumentError, "comparator class must define initialize with arity 2"
  end
  @comparators[name] = klass
end

#switch=(value) ⇒ Object



49
50
51
52
# File 'lib/both_is_good/configuration.rb', line 49

def switch=(value)
  validate_hook!(:switch, value, [0, 1])
  @switch = value
end