class Ettin::Options

An object that holds configuration settings / options

Attributes

hash[R]

Public Class Methods

new(hash) click to toggle source
# 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

Public Instance Methods

==(other)
Alias for: eql?
[](key) click to toggle source
# File lib/ettin/options.rb, line 60
def [](key)
  convert(hash[convert_key(key)])
end
[]=(key, value) click to toggle source
# File lib/ettin/options.rb, line 64
def []=(key, value)
  hash[convert_key(key)] = value
end
each() { |k, convert(v)| ... } click to toggle source
# File lib/ettin/options.rb, line 78
def each
  hash.each {|k, v| yield k, convert(v) }
end
eql?(other) click to toggle source
# File lib/ettin/options.rb, line 73
def eql?(other)
  to_h == other.to_h
end
Also aliased as: ==
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/ettin/options.rb, line 43
def key?(key)
  hash.key?(convert_key(key))
end
Also aliased as: has_key?
merge(other) click to toggle source
# 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) click to toggle source
# File lib/ettin/options.rb, line 55
def merge!(other)
  hash.deeper_merge!(other.to_h, overwrite_arrays: true)
  self
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# 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
respond_to_missing?(method, include_all = false) click to toggle source

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]*!$/

Calls superclass method
# File lib/ettin/options.rb, line 39
def respond_to_missing?(method, include_all = false)
  handles?(method) || super(method, include_all)
end
to_h() click to toggle source
# File lib/ettin/options.rb, line 68
def to_h
  hash
end
Also aliased as: to_hash
to_hash()
Alias for: to_h

Private Instance Methods

convert(value) click to toggle source
# File lib/ettin/options.rb, line 106
def convert(value)
  case value
  when Hash
    Options.new(value)
  when Array
    value.map {|i| convert(i) }
  else
    value
  end
end
convert_key(key) click to toggle source
# File lib/ettin/options.rb, line 102
def convert_key(key)
  key.to_s.to_sym
end
handle_assignment_method(method, *args) click to toggle source
# File lib/ettin/options.rb, line 94
def handle_assignment_method(method, *args)
  self[method.clean] = args.first
end
handle_bang_method(method) click to toggle source
# File lib/ettin/options.rb, line 86
def handle_bang_method(method)
  if key?(method.clean)
    self[method.clean]
  else
    raise KeyError, "key #{method.clean} not found"
  end
end
handles?(method) click to toggle source
# File lib/ettin/options.rb, line 98
def handles?(method)
  /^[a-zA-Z_0-9]*(\!|=)?$/.match(method.to_s)
end