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

Returns a new instance of Options.



14
15
16
17
18
# File 'lib/ettin/options.rb', line 14

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



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

def method_missing(method, *args, &block)
  if handles?(method)
    if bang?(method) && !key?(debang(method))
      raise KeyError, "key #{debang(method)} not found"
    else
      self[debang(method)]
    end
  else
    super(method, *args, &block)
  end
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



53
54
55
# File 'lib/ettin/options.rb', line 53

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

#eachObject



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

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

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/ettin/options.rb', line 62

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

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

Returns:

  • (Boolean)


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

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

#merge!(other) ⇒ Object



45
46
47
# File 'lib/ettin/options.rb', line 45

def merge!(other)
  hash.deeper_merge!(other.to_h, overwrite_arrays: true)
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]*!$/

Returns:

  • (Boolean)


36
37
38
# File 'lib/ettin/options.rb', line 36

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

#to_hObject Also known as: to_hash



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

def to_h
  hash
end