class Comodule::ConfigSupport::Config

Public Class Methods

combine(config1, config2) click to toggle source
# File lib/comodule/config_support.rb, line 12
def combine(config1, config2)
  new_obj = new

  config1.each do |key, value|
    new_obj[key] = value
  end

  config2.each do |key, value|
    if self === new_obj[key] && self === value
      new_obj[key] = combine(new_obj[key], value)
    else
      new_obj[key] = value
    end
  end

  new_obj
end
new(config_hash={}) { |self| ... } click to toggle source
# File lib/comodule/config_support.rb, line 31
def initialize(config_hash={})
  config_hash[:configure_type] ||= :soft

  config_hash.each do |directive, value|
    value = value.to_sym if directive == :configure_type

    if Hash === value
      value[:configure_type] ||= config_hash[:configure_type]
      value = self.class.new(value)
    end

    set_directive directive, value
  end

  if block_given?
    yield self
  end
end
original_methods() click to toggle source
# File lib/comodule/config_support.rb, line 4
def original_methods
  return @original_methods if @original_methods

  @original_methods =  public_instance_methods
  @original_methods += protected_instance_methods
  @original_methods += private_instance_methods
end

Public Instance Methods

+(other_config)
Alias for: merge
[](directive) click to toggle source
# File lib/comodule/config_support.rb, line 58
def [](directive)
  get_directive(directive)
end
[]=(directive, arg) click to toggle source
# File lib/comodule/config_support.rb, line 62
def []=(directive, arg)
  set_directive(directive, arg)
end
each() { |key, value| ... } click to toggle source
# File lib/comodule/config_support.rb, line 72
def each
  instance_variables.each do |variable_name|
    next if variable_name == :@configure_type

    key = variable_name.to_s.sub(/@/, '').to_sym
    value = instance_variable_get(variable_name)

    yield key, value
  end
end
merge(other_config) click to toggle source
# File lib/comodule/config_support.rb, line 66
def merge(other_config)
  self.class.combine(self, other_config)
end
Also aliased as: +
method_missing(directive, arg=nil) click to toggle source
# File lib/comodule/config_support.rb, line 50
def method_missing(directive, arg=nil)
  if directive =~ /^(.+)=/
    set_directive($1, arg)
  else
    get_directive(directive)
  end
end
slice(*keys) click to toggle source
# File lib/comodule/config_support.rb, line 92
def slice(*keys)
  keys.inject(self.class.new) do |new_obj, key|
    new_obj[key] = self[key] if self[key]
    new_obj
  end
end
to_hash() click to toggle source
# File lib/comodule/config_support.rb, line 83
def to_hash
  hsh = {}
  each do |key, value|
    value = value.to_hash if self.class === value
    hsh[key] = value
  end
  hsh
end

Private Instance Methods

get_directive(directive) click to toggle source
# File lib/comodule/config_support.rb, line 102
def get_directive(directive)
  value = instance_variable_get("@#{directive}")
  if @configure_type == :hard && !value
    raise ArgumentError, "Comodule::ConfigSupport::Config is missing this directive [#{directive}]."
  end
  value
end
set_directive(directive, arg) click to toggle source
# File lib/comodule/config_support.rb, line 110
def set_directive(directive, arg)
  directive = directive.to_sym

  if self.class.original_methods.member?(directive)
    raise ArgumentError, "You cannot use the directive [#{directive}= #{arg}] same as a member of Object#methods."
  end

  arg = arg.to_sym if directive == :configure_type

  if Hash === arg
    arg[:configure_type] ||= configure_type
    arg = self.class.new(arg)
  end

  instance_variable_set("@#{directive}", arg)
end