Class: Vines::Config

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

Overview

A Config object is passed to the stream handlers to give them access to server configuration information like virtual host names, storage systems, etc. This class provides the DSL methods used in the config/vines.rb file.

Defined Under Namespace

Classes: ClientPort, ComponentPort, Host, HttpPort, Port, ServerPort

Constant Summary collapse

LOG_LEVELS =
%w[debug info warn error fatal].freeze
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Config

Returns a new instance of Config.



23
24
25
26
27
# File 'lib/vines/config.rb', line 23

def initialize(&block)
  @vhosts, @ports = {}, {}
  instance_eval(&block)
  raise "must define at least one virtual host" if @vhosts.empty?
end

Instance Attribute Details

#vhostsObject (readonly)

Returns the value of attribute vhosts.



12
13
14
# File 'lib/vines/config.rb', line 12

def vhosts
  @vhosts
end

Class Method Details

.configure(&block) ⇒ Object



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

def self.configure(&block)
  @@instance = self.new(&block)
end

.instanceObject



19
20
21
# File 'lib/vines/config.rb', line 19

def self.instance
  @@instance
end

Instance Method Details

#[](name) ⇒ Object

Retrieve the Port subclass with this name:

:client, :server, :http, :component


72
73
74
# File 'lib/vines/config.rb', line 72

def [](name)
  @ports[name] or raise ArgumentError.new("no port named #{name}")
end

#host(*names, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/vines/config.rb', line 29

def host(*names, &block)
  names = names.flatten.map {|name| name.downcase }
  dupes = names.uniq.size != names.size || (@vhosts.keys & names).any?
  raise "one host definition per domain allowed" if dupes
  names.each do |name|
    @vhosts.merge! Host.new(name, &block).to_hash
  end
end

#log(level) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/vines/config.rb', line 48

def log(level)
  const = Logger.const_get(level.to_s.upcase) rescue nil
  unless LOG_LEVELS.include?(level.to_s) && const
    raise "log level must be one of: #{LOG_LEVELS.join(', ')}"
  end
  Class.new.extend(Vines::Log).log.level = const
end

#portsObject



56
57
58
# File 'lib/vines/config.rb', line 56

def ports
  @ports.values
end

#s2s?(domain) ⇒ Boolean

Returns true if server-to-server connections are allowed with the given domain.

Returns:

  • (Boolean)


66
67
68
# File 'lib/vines/config.rb', line 66

def s2s?(domain)
  @ports[:server] && @ports[:server].hosts.include?(domain)
end

#vhost?(domain) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/vines/config.rb', line 60

def vhost?(domain)
  @vhosts.key?(domain)
end