123456789_123456789_123456789_123456789_123456789_

Class: Puma::UserFileDefaultOptions

Relationships & Source Files
Inherits: Object
Defined in: lib/puma/configuration.rb

Overview

A class used for storing “leveled” configuration options.

In this class any “user” specified options take precedence over any “file” specified options, take precedence over any “default” options.

User input is prefered over “defaults”:

user_options    = { foo: "bar" }
default_options = { foo: "zoo" }
options = UserFileDefaultOptions.new(user_options, default_options)
puts options[:foo]
# => "bar"

All values can be accessed via `all_of`

puts options.all_of(:foo)
# => ["bar", "zoo"]

A “file” option can be set. This config will be prefered over “default” options but will defer to any available “user” specified options.

user_options    = { foo: "bar" }
default_options = { rackup: "zoo.rb" }
options = UserFileDefaultOptions.new(user_options, default_options)
options.file_options[:rackup] = "sup.rb"
puts options[:rackup]
# => "sup.rb"

The “default” options can be set via procs. These are resolved during runtime via calls to `finalize_values`

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(user_options, default_options) ⇒ UserFileDefaultOptions

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 48

def initialize(user_options, default_options)
  @user_options    = user_options
  @file_options    = {}
  @default_options = default_options
end

Instance Attribute Details

#default_options (readonly)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 54

attr_reader :user_options, :file_options, :default_options

#file_options (readonly)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 54

attr_reader :user_options, :file_options, :default_options

#user_options (readonly)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 54

attr_reader :user_options, :file_options, :default_options

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 56

def [](key)
  return user_options[key]    if user_options.key?(key)
  return file_options[key]    if file_options.key?(key)
  return default_options[key] if default_options.key?(key)
end

#[]=(key, value)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 62

def []=(key, value)
  user_options[key] = value
end

#all_of(key)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 70

def all_of(key)
  user    = user_options[key]
  file    = file_options[key]
  default = default_options[key]

  user    = [user]    unless user.is_a?(Array)
  file    = [file]    unless file.is_a?(Array)
  default = [default] unless default.is_a?(Array)

  user.compact!
  file.compact!
  default.compact!

  user + file + default
end

#fetch(key, default_value = nil)

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 66

def fetch(key, default_value = nil)
  self[key] || default_value
end

#finalize_values

[ GitHub ]

  
# File 'lib/puma/configuration.rb', line 86

def finalize_values
  @default_options.each do |k,v|
    if v.respond_to? :call
      @default_options[k] = v.call
    end
  end
end