Class: Ettin::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/ettin/options.rb

Overview

An object that holds configuration settings / options

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Options


16
17
18
19
20
# File 'lib/ettin/options.rb', line 16

def initialize(hash)
  @hash = hash
  @hash.deep_transform_keys! {|key| key.to_s.to_sym }
  @hash.default = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

rubocop:disable Style/MethodMissingSuper


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ettin/options.rb', line 22

def method_missing(method, *args, &block) # rubocop:disable Style/MethodMissingSuper
  return super(method, *args, &block) unless handles?(method)

  method = MethodName.new(method)
  if method.bang?
    handle_bang_method(method)
  elsif method.assignment?
    handle_assignment_method(method, *args)
  else
    self[method.clean]
  end
end

Instance Method Details

#[](key) ⇒ Object


60
61
62
# File 'lib/ettin/options.rb', line 60

def [](key)
  convert(hash[convert_key(key)])
end

#[]=(key, value) ⇒ Object


64
65
66
# File 'lib/ettin/options.rb', line 64

def []=(key, value)
  hash[convert_key(key)] = value
end

#eachObject


78
79
80
# File 'lib/ettin/options.rb', line 78

def each
  hash.each {|k, v| yield k, convert(v) }
end

#eql?(other) ⇒ Boolean Also known as: ==


73
74
75
# File 'lib/ettin/options.rb', line 73

def eql?(other)
  to_h == other.to_h
end

#key?(key) ⇒ Boolean Also known as: has_key?


43
44
45
# File 'lib/ettin/options.rb', line 43

def key?(key)
  hash.key?(convert_key(key))
end

#merge(other) ⇒ Object


48
49
50
51
52
53
# File 'lib/ettin/options.rb', line 48

def merge(other)
  new_hash = {}
    .deeper_merge(hash)
    .deeper_merge!(other.to_h, overwrite_arrays: true)
  self.class.new(new_hash)
end

#merge!(other) ⇒ Object


55
56
57
58
# File 'lib/ettin/options.rb', line 55

def merge!(other)
  hash.deeper_merge!(other.to_h, overwrite_arrays: true)
  self
end

#respond_to_missing?(method, include_all = false) ⇒ Boolean

We respond to:

  • all methods our parents respond to

  • all methods that are mostly alpha-numeric: /^[a-zA-Z_0-9]*$/

  • all methods that are mostly alpha-numeric + !: /^[a-zA-Z_0-9]*!$/


39
40
41
# File 'lib/ettin/options.rb', line 39

def respond_to_missing?(method, include_all = false)
  handles?(method) || super(method, include_all)
end

#to_hObject Also known as: to_hash


68
69
70
# File 'lib/ettin/options.rb', line 68

def to_h
  hash
end