Class: ENVied::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/envied/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Configuration

Returns a new instance of Configuration.



5
6
7
8
9
# File 'lib/envied/configuration.rb', line 5

def initialize(options = {}, &block)
  @coercer = options.fetch(:coercer, Coercer.new)
  @defaults_enabled = options.fetch(:enable_defaults, defaults_enabled_default)
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



3
4
5
# File 'lib/envied/configuration.rb', line 3

def coercer
  @coercer
end

#current_groupObject (readonly)

Returns the value of attribute current_group.



3
4
5
# File 'lib/envied/configuration.rb', line 3

def current_group
  @current_group
end

#defaults_enabledObject (readonly)

Returns the value of attribute defaults_enabled.



3
4
5
# File 'lib/envied/configuration.rb', line 3

def defaults_enabled
  @defaults_enabled
end

Class Method Details

.load(options = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/envied/configuration.rb', line 19

def self.load(options = {})
  envfile = File.expand_path('Envfile')
  new(options).tap do |v|
    v.instance_eval(File.read(envfile), envfile)
  end
end

Instance Method Details

#defaults_enabled?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/envied/configuration.rb', line 30

def defaults_enabled?
  @defaults_enabled.respond_to?(:call) ?
    @defaults_enabled.call :
    @defaults_enabled
end

#defaults_enabled_defaultObject



11
12
13
14
15
16
17
# File 'lib/envied/configuration.rb', line 11

def defaults_enabled_default
  if ENV['ENVIED_ENABLE_DEFAULTS'].nil?
    false
  else
    @coercer.coerce(ENV['ENVIED_ENABLE_DEFAULTS'], :boolean)
  end
end

#enable_defaults!(value = true, &block) ⇒ Object



26
27
28
# File 'lib/envied/configuration.rb', line 26

def enable_defaults!(value = true, &block)
  @defaults_enabled = block_given? ? block.call : value
end

#group(*names, &block) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/envied/configuration.rb', line 48

def group(*names, &block)
  names.each do |name|
    @current_group = name.to_sym
    yield
  end
ensure
  @current_group = nil
end

#variable(name, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/envied/configuration.rb', line 36

def variable(name, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  type = args.first || :string

  unless coercer.supported_type?(type)
    raise ArgumentError,
      "Variable type (of #{name}) should be one of #{coercer.supported_types}"
  end
  options[:group] = current_group if current_group
  variables << ENVied::Variable.new(name, type, options)
end

#variablesObject



57
58
59
# File 'lib/envied/configuration.rb', line 57

def variables
  @variables ||= []
end