Module: Configerator

Included in:
Config
Defined in:
lib/configerator/configerator.rb,
lib/generators/configerator/config_generator.rb

Defined Under Namespace

Classes: ConfigGenerator

Instance Method Summary collapse

Instance Method Details

#array(method = nil) ⇒ Object

optional :accronyms, array(string)

> [‘a’, ‘b’]

optional :numbers, array(int)

> [1, 2]

optional :notype, array

> [‘a’, ‘b’]



64
65
66
67
68
69
70
# File 'lib/configerator/configerator.rb', line 64

def array(method = nil)
  -> (v) do
    if v
      v.split(',').map{|a| cast(a, method) }
    end
  end
end

#boolObject



42
43
44
# File 'lib/configerator/configerator.rb', line 42

def bool
  ->(v) { v.to_s=='true'}
end

#floatObject



38
39
40
# File 'lib/configerator/configerator.rb', line 38

def float
  ->(v) { v.to_f }
end

#intObject

Scope methods



34
35
36
# File 'lib/configerator/configerator.rb', line 34

def int
  ->(v) { v.to_i }
end

#namespace(namespace, prefix: true, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/configerator/configerator.rb', line 21

def namespace namespace, prefix: true, &block
  @processed = []
  @prefix = "#{namespace}_" if prefix

  yield

  instance_eval "def #{namespace}?; !!(#{@processed.join(' && ')}) end", __FILE__, __LINE__
ensure
  @prefix = nil
  @processed = []
end

#optional(name, method = nil) ⇒ Object



11
12
13
14
# File 'lib/configerator/configerator.rb', line 11

def optional(name, method=nil)
  value = cast(fetch_env(name), method)
  create(name, value)
end

#override(name, default, method = nil) ⇒ Object



16
17
18
19
# File 'lib/configerator/configerator.rb', line 16

def override(name, default, method=nil)
  value = cast(fetch_env(name) || default, method)
  create(name, value)
end

#required(name, method = nil, error_on_load: true) ⇒ Object

Initializers (DSL)



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

def required(name, method=nil, error_on_load: true)
  value = cast(fetch_env(name, error_on_load: error_on_load), method)

  create(name, value, error_on_load)
end

#stringObject



46
47
48
# File 'lib/configerator/configerator.rb', line 46

def string
  nil
end

#symbolObject



50
51
52
# File 'lib/configerator/configerator.rb', line 50

def symbol
  ->(v) { v.to_sym }
end

#urlObject



54
55
56
# File 'lib/configerator/configerator.rb', line 54

def url
  ->(v) { v && URI.parse(v) }
end