Class: VhostWriter::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/vhost_writer/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vhost_writer/writer.rb', line 7

def initialize(options={})
  raise ArgumentError, ':conf_dir is required' unless options.key? :conf_dir
  raise ArgumentError, 'Either :sites_dir or :sites is required' unless options.key? :sites_dir or options.key? :sites
  raise ArgumentError, 'Only one of :sites_dir and :sites is allowed' if options.key? :sites_dir and options.key? :sites

  options[:conf_dir] << '/' unless options[:conf_dir] =~ /\/\z/
  @conf_dir = options[:conf_dir]

  if options.key? :sites_dir
    @sites = Dir.glob("#{options[:sites_dir]}*").map { |f| File.basename f }
  else
    @sites = options[:sites]
  end
end

Instance Attribute Details

#conf_dirObject (readonly)

Returns the value of attribute conf_dir.



5
6
7
# File 'lib/vhost_writer/writer.rb', line 5

def conf_dir
  @conf_dir
end

#sitesObject (readonly)

Returns the value of attribute sites.



5
6
7
# File 'lib/vhost_writer/writer.rb', line 5

def sites
  @sites
end

Instance Method Details

#blacklist(site_blacklist) ⇒ Object



22
23
24
# File 'lib/vhost_writer/writer.rb', line 22

def blacklist(site_blacklist)
  Writer.new :conf_dir => @conf_dir, :sites => @sites - site_blacklist
end

#whitelist(site_whitelist) ⇒ Object



26
27
28
# File 'lib/vhost_writer/writer.rb', line 26

def whitelist(site_whitelist)
  Writer.new :conf_dir => @conf_dir, :sites => site_whitelist - (@sites - site_whitelist)
end

#write_configs!(template) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/vhost_writer/writer.rb', line 30

def write_configs!(template)
  @sites.each do |site|
    File.open("#{@conf_dir}#{site}", 'w') do |f|
      f.write ERB.new(template).result(binding)
    end
  end
end