Class: Puma::UserFileDefaultOptions

Inherits:
Object
  • Object
show all
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 preferred 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 preferred 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`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_options, default_options) ⇒ UserFileDefaultOptions

Returns a new instance of UserFileDefaultOptions.



39
40
41
42
43
# File 'lib/puma/configuration.rb', line 39

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

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



45
46
47
# File 'lib/puma/configuration.rb', line 45

def default_options
  @default_options
end

#file_optionsObject (readonly)

Returns the value of attribute file_options.



45
46
47
# File 'lib/puma/configuration.rb', line 45

def file_options
  @file_options
end

#user_optionsObject (readonly)

Returns the value of attribute user_options.



45
46
47
# File 'lib/puma/configuration.rb', line 45

def user_options
  @user_options
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  fetch(key)
end

#[]=(key, value) ⇒ Object



51
52
53
# File 'lib/puma/configuration.rb', line 51

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

#all_of(key) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puma/configuration.rb', line 63

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) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/puma/configuration.rb', line 55

def fetch(key, default_value = nil)
  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)

  default_value
end

#final_optionsObject



87
88
89
90
91
# File 'lib/puma/configuration.rb', line 87

def final_options
  default_options
    .merge(file_options)
    .merge(user_options)
end

#finalize_valuesObject



79
80
81
82
83
84
85
# File 'lib/puma/configuration.rb', line 79

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