Class: Nginxtra::Config::ConfigFile

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

Overview

Represents a config file being defined by nginxtra.conf.rb.

Instance Method Summary collapse

Constructor Details

#initialize(filename, config, &block) ⇒ ConfigFile

Returns a new instance of ConfigFile.



372
373
374
375
376
377
378
# File 'lib/nginxtra/config.rb', line 372

def initialize(filename, config, &block)
  @filename = filename
  @config = config
  @indentation = Nginxtra::Config::Indentation.new :indent_size => 4
  @file_contents = []
  instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Arbitrary config can be specified as long as the name doesn’t clash with one of the Config instance methods.

Example usage:

nginxtra.config do
  user "my_user"
  worker_processes 42
  events do
    worker_connections 512
  end
end

Any arguments the the method will be joined with the method name with a space to produce the output.



514
515
516
517
518
519
520
# File 'lib/nginxtra/config.rb', line 514

def method_missing(method, *args, &block)
  if partial? method
    invoke_partial method, args, block
  else
    process_config_block_or_line method, args, block
  end
end

Instance Method Details

#_break(*args, &block) ⇒ Object

Convenience method to use the “break” keyword, as seen in if blocks of nginx configurations.



447
448
449
# File 'lib/nginxtra/config.rb', line 447

def _break(*args, &block)
  process_config_block_or_line "break", args, block
end

#_if(*args, &block) ⇒ Object

Convenience method to invoke an if block in the nginx configuration. Parenthesis are added around the arguments of this method.

Example usage:

nginxtra.config do
  _if "some", "~", "thing" do
    _return 404
  end
end

Which will produce the following config:

if (some ~ thing) {
  return 404;
}


466
467
468
# File 'lib/nginxtra/config.rb', line 466

def _if(*args, &block)
  config_block "if (#{args.join " "})", &block
end

#_return(*args, &block) ⇒ Object

Convenience method to use the “return” keyword, as seen in if blocks of nginx configurations.



472
473
474
# File 'lib/nginxtra/config.rb', line 472

def _return(*args, &block)
  process_config_block_or_line "return", args, block
end

#bare_config_line(contents) ⇒ Object

Add a new line to the config, but without a semicolon at the end.

Example usage:

nginxtra.config do
  bare_config_line "a line with no semicolon"
end


409
410
411
412
413
# File 'lib/nginxtra/config.rb', line 409

def bare_config_line(contents)
  @begin_of_block = false
  @end_of_block = false
  @file_contents << "#{@indentation}#{contents}"
end

#config_block(name) ⇒ Object

Add a new block to the config. This will result in outputting something in the config like a server block, wrapped in { }. A block should be passed in to this method, which will represent the contents of the block (if no block is given, the resulting config will have an empty block).

Example usage:

nginxtra.config do
  config_block "events" do
    config_line "worker_connections 512"
  end
end


434
435
436
437
438
439
440
441
442
443
# File 'lib/nginxtra/config.rb', line 434

def config_block(name)
  empty_config_line unless @file_contents.empty? || @begin_of_block
  bare_config_line "#{name} {"
  @begin_of_block = true
  @indentation + 1
  yield if block_given?
  @indentation - 1
  bare_config_line "}"
  @end_of_block = true
end

#config_file_contentsObject

The file contents that were defined for this config file.



381
382
383
384
385
# File 'lib/nginxtra/config.rb', line 381

def config_file_contents
  result = @file_contents.join "\n"
  result += "\n" unless result.empty?
  result
end

#config_line(contents) ⇒ Object

Add a new line to the config. A semicolon is added automatically.

Example usage:

nginxtra.config do
  config_line "user my_user"
  config_line "worker_processes 42"
end


395
396
397
398
399
400
# File 'lib/nginxtra/config.rb', line 395

def config_line(contents)
  empty_config_line if @end_of_block
  @begin_of_block = false
  @end_of_block = false
  bare_config_line "#{contents};"
end

#empty_config_lineObject

Add an empty config line to the resulting config file.



416
417
418
419
420
# File 'lib/nginxtra/config.rb', line 416

def empty_config_line
  @begin_of_block = false
  @end_of_block = false
  @file_contents << ""
end

#passenger_on!Object

Output that passenger is enabled in this block.



534
535
536
# File 'lib/nginxtra/config.rb', line 534

def passenger_on!
  config_line %{passenger_enabled on}
end

#passenger_root!Object

Output the passenger_root line, including the proper passenger gem path.



524
525
526
# File 'lib/nginxtra/config.rb', line 524

def passenger_root!
  config_line %{passenger_root #{Nginxtra::Config.passenger_spec.gem_dir}}
end

#passenger_ruby!Object

Output the passenger_ruby, including the proper ruby path.



529
530
531
# File 'lib/nginxtra/config.rb', line 529

def passenger_ruby!
  config_line %{passenger_ruby #{Nginxtra::Config.ruby_path}}
end

#process_template!(template, options = {}, yielder = nil) ⇒ Object

Process the given template. Optionally, include options (as yielded values) available to the template. The yielder passed in will be invoked (if given) if the template invokes yield.



479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/nginxtra/config.rb', line 479

def process_template!(template, options = {}, yielder = nil)
  if template.respond_to? :call
    block = Proc.new { instance_eval &yielder if yielder }
    instance_exec options, block, &template
  else
    process_template_with_yields! template do |x|
      if x
        options[x.to_sym]
      else
        instance_eval &yielder if yielder
      end
    end
  end
end

#process_template_with_yields!(template) ⇒ Object

Helper method for process_template! Which is expected to have a block passed in to handle yields from within the template.



496
497
498
# File 'lib/nginxtra/config.rb', line 496

def process_template_with_yields!(template)
  instance_eval File.read(template)
end