Class: Nginxtra::Config::ConfigFile
- Inherits:
-
Object
- Object
- Nginxtra::Config::ConfigFile
- Defined in:
- lib/nginxtra/config.rb
Overview
Represents a config file being defined by nginxtra.conf.rb.
Instance Method Summary collapse
-
#_break(*args, &block) ⇒ Object
Convenience method to use the “break” keyword, as seen in if blocks of nginx configurations.
-
#_if(*args, &block) ⇒ Object
Convenience method to invoke an if block in the nginx configuration.
-
#_return(*args, &block) ⇒ Object
Convenience method to use the “return” keyword, as seen in if blocks of nginx configurations.
-
#bare_config_line(contents) ⇒ Object
Add a new line to the config, but without a semicolon at the end.
-
#config_block(name) ⇒ Object
Add a new block to the config.
-
#config_file_contents ⇒ Object
The file contents that were defined for this config file.
-
#config_line(contents) ⇒ Object
Add a new line to the config.
-
#empty_config_line ⇒ Object
Add an empty config line to the resulting config file.
-
#initialize(filename, config, &block) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
-
#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.
-
#passenger_on! ⇒ Object
Output that passenger is enabled in this block.
-
#passenger_root! ⇒ Object
Output the passenger_root line, including the proper passenger gem path.
-
#passenger_ruby! ⇒ Object
Output the passenger_ruby, including the proper ruby path.
-
#process_template!(template, options = {}, yielder = nil) ⇒ Object
Process the given template.
-
#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.
Constructor Details
#initialize(filename, config, &block) ⇒ ConfigFile
Returns a new instance of ConfigFile.
365 366 367 368 369 370 371 |
# File 'lib/nginxtra/config.rb', line 365 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.
507 508 509 510 511 512 513 |
# File 'lib/nginxtra/config.rb', line 507 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.
440 441 442 |
# File 'lib/nginxtra/config.rb', line 440 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;
}
459 460 461 |
# File 'lib/nginxtra/config.rb', line 459 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.
465 466 467 |
# File 'lib/nginxtra/config.rb', line 465 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
"a line with no semicolon"
end
402 403 404 405 406 |
# File 'lib/nginxtra/config.rb', line 402 def (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
427 428 429 430 431 432 433 434 435 436 |
# File 'lib/nginxtra/config.rb', line 427 def config_block(name) empty_config_line unless @file_contents.empty? || @begin_of_block "#{name} {" @begin_of_block = true @indentation.increment yield if block_given? @indentation.decrement "}" @end_of_block = true end |
#config_file_contents ⇒ Object
The file contents that were defined for this config file.
374 375 376 377 378 |
# File 'lib/nginxtra/config.rb', line 374 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
388 389 390 391 392 393 |
# File 'lib/nginxtra/config.rb', line 388 def config_line(contents) empty_config_line if @end_of_block @begin_of_block = false @end_of_block = false "#{contents};" end |
#empty_config_line ⇒ Object
Add an empty config line to the resulting config file.
409 410 411 412 413 |
# File 'lib/nginxtra/config.rb', line 409 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.
527 528 529 |
# File 'lib/nginxtra/config.rb', line 527 def passenger_on! config_line %(passenger_enabled on) end |
#passenger_root! ⇒ Object
Output the passenger_root line, including the proper passenger gem path.
517 518 519 |
# File 'lib/nginxtra/config.rb', line 517 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.
522 523 524 |
# File 'lib/nginxtra/config.rb', line 522 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.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/nginxtra/config.rb', line 472 def process_template!(template, = {}, yielder = nil) if template.respond_to? :call block = proc { instance_eval(&yielder) if yielder } instance_exec , block, &template else process_template_with_yields! template do |x| if x [x.to_sym] elsif yielder instance_eval(&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.
489 490 491 |
# File 'lib/nginxtra/config.rb', line 489 def process_template_with_yields!(template) instance_eval File.read(template) end |