Class: PumaPlus::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/puma_plus/config_file.rb

Overview

Three-layer option lookup: command line beats config file beats default.

Same precedence as puma's UserFileDefaultOptions, and for the same reason -- a config file is checked in and shared, a command line is what you type to override it right now. Kept as three separate hashes rather than one merged one so puma-plus --port 3000 still wins over a port 9292 in the file no matter which was read first.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}) ⇒ Options

Returns a new instance of Options.



14
15
16
17
18
# File 'lib/puma_plus/config_file.rb', line 14

def initialize(defaults = {})
  @default = defaults
  @file    = {}
  @user    = {}
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



20
21
22
# File 'lib/puma_plus/config_file.rb', line 20

def default
  @default
end

#fileObject (readonly)

Returns the value of attribute file.



20
21
22
# File 'lib/puma_plus/config_file.rb', line 20

def file
  @file
end

#userObject (readonly)

Returns the value of attribute user.



20
21
22
# File 'lib/puma_plus/config_file.rb', line 20

def user
  @user
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
25
26
27
# File 'lib/puma_plus/config_file.rb', line 22

def [](key)
  return @user[key] if @user.key?(key)
  return @file[key] if @file.key?(key)

  @default[key]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


29
# File 'lib/puma_plus/config_file.rb', line 29

def key?(key) = @user.key?(key) || @file.key?(key) || @default.key?(key)

#origin(key) ⇒ Object

Where a value came from. Used by --dry-run so a surprising setting can be traced to the line that set it.



33
34
35
36
37
38
39
# File 'lib/puma_plus/config_file.rb', line 33

def origin(key)
  return :cli if @user.key?(key)
  return :config if @file.key?(key)
  return :default if @default.key?(key)

  nil
end

#to_hObject



41
42
43
# File 'lib/puma_plus/config_file.rb', line 41

def to_h
  @default.merge(@file).merge(@user)
end