Class: Zabby::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/zabby/config.rb

Constant Summary collapse

SETTING_LIST =
%w{server user password proxy_host proxy_user proxy_password}

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Config

Returns a new instance of Config.



11
12
13
# File 'lib/zabby/config.rb', line 11

def initialize &block
  setup(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zabby/config.rb', line 25

def method_missing(name, *args, &block)
  name = name.to_s.gsub(/=$/, '')
  raise ConfigurationError.new("Unknown setting '#{name}'") if !SETTING_LIST.include?(name.to_s)

  if args.empty?
    instance_variable_get("@#{name}")
  elsif args.size != 1
    raise ConfigurationError.new("Too many values for '#{name}'")
  else
    instance_variable_set("@#{name}", args.first)
  end
end

Instance Method Details

#listObject



19
20
21
22
23
# File 'lib/zabby/config.rb', line 19

def list
  SETTING_LIST.each do |k|
    puts "#{k} = #{instance_variable_get("@#{k}")}"
  end
end

#setup(&block) ⇒ Object



15
16
17
# File 'lib/zabby/config.rb', line 15

def setup &block
  instance_eval(&block) if block_given?
end