Module: Pliny::ConfigHelpers

Defined in:
lib/pliny/config_helpers.rb

Overview

DEPRECATED: ConfigHelpers was a slightly more primitive version of CastingConfigHelpers above that didn’t contain any of the latter’s features around typing. It’s been left here for backwards compatibility in apps based on old templates that may be using it.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_question_method(attr) ⇒ Object



102
103
104
105
106
107
# File 'lib/pliny/config_helpers.rb', line 102

def self.add_question_method(attr)
  define_method "#{attr}?" do
    return false if send(attr).nil?
    !!(send(attr) =~ /\Atrue|yes|on\z/i || send(attr).to_i > 0)
  end
end

Instance Method Details

#mandatory(*attrs) ⇒ Object



88
89
90
91
92
93
# File 'lib/pliny/config_helpers.rb', line 88

def mandatory(*attrs)
  attrs.each do |attr|
    instance_eval "def #{attr}; @#{attr} ||= ENV['#{attr.upcase}'] || raise('missing=#{attr.upcase}') end", __FILE__, __LINE__
    ConfigHelpers.add_question_method(attr)
  end
end

#optional(*attrs) ⇒ Object



81
82
83
84
85
86
# File 'lib/pliny/config_helpers.rb', line 81

def optional(*attrs)
  attrs.each do |attr|
    instance_eval "def #{attr}; @#{attr} ||= ENV['#{attr.upcase}'] end", __FILE__, __LINE__
    ConfigHelpers.add_question_method(attr)
  end
end

#override(attrs) ⇒ Object



95
96
97
98
99
100
# File 'lib/pliny/config_helpers.rb', line 95

def override(attrs)
  attrs.each do |attr, value|
    instance_eval "def #{attr}; @#{attr} ||= ENV['#{attr.upcase}'] || '#{value}'.to_s end", __FILE__, __LINE__
    ConfigHelpers.add_question_method(attr)
  end
end