class Nginxtra::Config::ConfigFile
Represents a config file being defined by nginxtra.conf.rb.
Public Class Methods
# 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
Public Instance Methods
Convenience method to use the “break” keyword, as seen in if blocks of nginx configurations.
# File lib/nginxtra/config.rb, line 440 def _break(*args, &block) process_config_block_or_line "break", args, block end
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; }
# File lib/nginxtra/config.rb, line 459 def _if(*args, &block) config_block "if (#{args.join " "})", &block end
Convenience method to use the “return” keyword, as seen in if blocks of nginx configurations.
# File lib/nginxtra/config.rb, line 465 def _return(*args, &block) process_config_block_or_line "return", args, block end
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
# File lib/nginxtra/config.rb, line 402 def bare_config_line(contents) @begin_of_block = false @end_of_block = false @file_contents << "#{@indentation}#{contents}" end
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
# File lib/nginxtra/config.rb, line 427 def config_block(name) empty_config_line unless @file_contents.empty? || @begin_of_block bare_config_line "#{name} {" @begin_of_block = true @indentation.increment yield if block_given? @indentation.decrement bare_config_line "}" @end_of_block = true end
The file contents that were defined for this config file.
# File lib/nginxtra/config.rb, line 374 def config_file_contents result = @file_contents.join "\n" result += "\n" unless result.empty? result end
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
# 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 bare_config_line "#{contents};" end
Add an empty config line to the resulting config file.
# File lib/nginxtra/config.rb, line 409 def empty_config_line @begin_of_block = false @end_of_block = false @file_contents << "" end
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.
# 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
Output that passenger is enabled in this block.
# File lib/nginxtra/config.rb, line 527 def passenger_on! config_line %(passenger_enabled on) end
Output the passenger_root line, including the proper passenger gem path.
# File lib/nginxtra/config.rb, line 517 def passenger_root! config_line %(passenger_root #{Nginxtra::Config.passenger_spec.gem_dir}) end
Output the passenger_ruby, including the proper ruby path.
# File lib/nginxtra/config.rb, line 522 def passenger_ruby! config_line %(passenger_ruby #{Nginxtra::Config.ruby_path}) end
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.
# File lib/nginxtra/config.rb, line 472 def process_template!(template, options = {}, yielder = nil) if template.respond_to? :call block = proc { instance_eval(&yielder) if yielder } instance_exec options, block, &template else process_template_with_yields! template do |x| if x options[x.to_sym] elsif yielder instance_eval(&yielder) end end end end
Helper method for process_template! Which is expected to have a block passed in to handle yields from within the template.
# File lib/nginxtra/config.rb, line 489 def process_template_with_yields!(template) instance_eval File.read(template) end
Private Instance Methods
# File lib/nginxtra/config.rb, line 533 def full_partial_paths(partial_name) @config.partial_paths.lazy.map do |path| File.join path, @filename, "#{partial_name}.rb" end end
# File lib/nginxtra/config.rb, line 547 def invoke_partial(partial_name, args, block) partial_path = if Nginxtra::Config::Extension.partial? @filename, partial_name Nginxtra::Config::Extension.partial @filename, partial_name else full_partial_paths(partial_name).find do |path| File.exist? path end end if args.empty? partial_options = {} elsif args.length == 1 && args.first.is_a?(Hash) partial_options = args.first else raise Nginxtra::Error::InvalidPartialArguments end process_template! partial_path, partial_options, block end
# File lib/nginxtra/config.rb, line 539 def partial?(partial_name) return true if Nginxtra::Config::Extension.partial? @filename, partial_name full_partial_paths(partial_name).any? do |path| File.exist? path end end
# File lib/nginxtra/config.rb, line 567 def process_config_block_or_line(name, args, block) values = [name, *args].join " " if block config_block values, &block else config_line values end end