Class: Configify::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/configify/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



3
4
5
6
7
# File 'lib/configify/base.rb', line 3

def initialize
  self.class.properties.each do |key, value|
    send "#{key}=", value
  end
end

Class Method Details

.env_variable_prefix(prefix = nil) ⇒ Object



19
20
21
22
23
# File 'lib/configify/base.rb', line 19

def env_variable_prefix prefix = nil
  @env_variable_prefix = prefix if prefix

  @env_variable_prefix
end

.propertiesObject



31
32
33
# File 'lib/configify/base.rb', line 31

def properties
  @properties ||= Hash.new
end

.property(key, default: nil) ⇒ Object



25
26
27
28
29
# File 'lib/configify/base.rb', line 25

def property key, default: nil
  attr_accessor key.to_sym

  properties[key.to_sym] = default
end

Instance Method Details

#load_from_env(prefix: self.class.env_variable_prefix) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/configify/base.rb', line 9

def load_from_env prefix: self.class.env_variable_prefix
  self.class.properties.each do |key, _|
    env_var_name = "#{prefix}_#{key.upcase}"
    self.send "#{key}=", ENV[env_var_name] if ENV.has_key? env_var_name
  end

  self
end