Class: Userlist::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/userlist/config.rb

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
  push_key: nil,
  push_endpoint: 'https://push.userlist.io/',
  push_strategy: :threaded,
  log_level: :warn
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_from_initialize = {}) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
# File 'lib/userlist/config.rb', line 10

def initialize(config_from_initialize = {})
  @config = default_config
    .merge(config_from_initialize)
    .merge(config_from_environment)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



71
72
73
74
75
76
77
78
79
80
# File 'lib/userlist/config.rb', line 71

def method_missing(name, *args, &block)
  if respond_to_missing?(name)
    name = name.to_s
    method = name =~ /=$/ ? :[]= : :[]
    name = name.sub(/=$/, '').to_sym
    send(method, name, *args, &block)
  else
    super
  end
end

Class Method Details

.inherit(parent, config_from_arguments) ⇒ Object



16
17
18
19
20
21
# File 'lib/userlist/config.rb', line 16

def self.inherit(parent, config_from_arguments)
  config = allocate
  config.instance_variable_set(:@parent, parent)
  config.instance_variable_set(:@config, config_from_arguments.to_hash)
  config
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
# File 'lib/userlist/config.rb', line 35

def ==(other)
  config == other.config && parent == other.parent
end

#merge(other_config) ⇒ Object



23
24
25
# File 'lib/userlist/config.rb', line 23

def merge(other_config)
  self.class.inherit(self, other_config)
end

#to_hObject



31
32
33
# File 'lib/userlist/config.rb', line 31

def to_h
  parent ? parent.to_h.merge(config) : to_hash
end

#to_hashObject



27
28
29
# File 'lib/userlist/config.rb', line 27

def to_hash
  config
end