Class: Alf::Support::Config

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/alf/support/config.rb

Direct Known Subclasses

Database::Options

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

delegation_methods, each_option, getter_name, helpers, ivar_name, option, option_get, option_set, options, setter_name

Constructor Details

#initialize(h = {}) {|_self| ... } ⇒ Config

Creates a default options instance

Yields:

  • (_self)

Yield Parameters:


74
75
76
77
# File 'lib/alf/support/config.rb', line 74

def initialize(h = {})
  install_options_from_hash(h)
  yield(self) if block_given?
end

Instance Method Details

#[](option) ⇒ Object

Returns the value of an option


80
81
82
# File 'lib/alf/support/config.rb', line 80

def [](option)
  self.class.option_get(self, option)
end

#[]=(option, value) ⇒ Object

Sets the value of an option


85
86
87
# File 'lib/alf/support/config.rb', line 85

def []=(option, value)
  self.class.option_set(self, option, value)
end

#dupObject

Duplicates this configuration as well as all option values


95
96
97
98
99
100
101
102
103
104
# File 'lib/alf/support/config.rb', line 95

def dup
  super.tap do |c|
    self.class.each_option do |name,_,default|
      ivar_name = self.class.ivar_name(name)
      if instance_variable_defined?(ivar_name) || !default.is_a?(Proc)
        c[name] = (self[name].dup rescue self[name])
      end
    end
  end
end

#freezeObject

Freeze this configuration as well as all option values


107
108
109
110
111
112
113
# File 'lib/alf/support/config.rb', line 107

def freeze
  super
  self.class.each_option do |name,_,_|
    self[name].freeze
  end
  self
end

#merge(h) ⇒ Object

Merge with another options from a hash


90
91
92
# File 'lib/alf/support/config.rb', line 90

def merge(h)
  dup.install_options_from_hash(h)
end