Class: Padrino::Env::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/padrino/env.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Returns a new instance of Application.



14
15
16
17
# File 'lib/padrino/env.rb', line 14

def initialize(options = {})
	@path = options.fetch(:path, 'config/application.yml')
	@environment = options.fetch(:environment, Padrino.env)
end

Class Method Details

.load(options = {}) ⇒ Object



10
11
12
# File 'lib/padrino/env.rb', line 10

def self.load(options = {})
	new(options).load
end

Instance Method Details

#configurationObject



33
34
35
# File 'lib/padrino/env.rb', line 33

def configuration
	global_configuration.merge(environment_configuration)
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/padrino/env.rb', line 43

def each(&block)
	configuration.each(&block)
end

#environmentObject



29
30
31
# File 'lib/padrino/env.rb', line 29

def environment
	@environment.to_s
end

#environment_configurationObject



59
60
61
# File 'lib/padrino/env.rb', line 59

def environment_configuration
	raw_configuration.fetch(environment) { {} }
end

#global_configurationObject



55
56
57
# File 'lib/padrino/env.rb', line 55

def global_configuration
	raw_configuration.reject { |_, value| value.is_a?(Hash) }
end

#loadObject



37
38
39
40
41
# File 'lib/padrino/env.rb', line 37

def load
	each do |key, value|
		set(key, value) unless skip?(key)
	end
end

#non_string_configuration!(value) ⇒ Object



68
69
70
# File 'lib/padrino/env.rb', line 68

def non_string_configuration!(value)
	warn "WARNING: Use strings for Figaro configuration. #{value.inspect} was converted to #{value.to_s.inspect}."
end

#parse(path) ⇒ Object



63
64
65
66
# File 'lib/padrino/env.rb', line 63

def parse(path)
	return (YAML.load(ERB.new(File.read(path)).result) || {}) if File.exist?(path)
	{}
end

#pathObject



47
48
49
# File 'lib/padrino/env.rb', line 47

def path
	Padrino.root(@path)
end

#raw_configurationObject



51
52
53
# File 'lib/padrino/env.rb', line 51

def raw_configuration
 		@parsed ||= parse(path)
end

#set(key, value) ⇒ Object



19
20
21
22
23
# File 'lib/padrino/env.rb', line 19

def set(key, value)
	non_string_configuration!(key) unless key.is_a?(String)
	non_string_configuration!(value) unless value.is_a?(String)
	::ENV[key.to_s] = value.to_s
end

#skip?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/padrino/env.rb', line 25

def skip?(key)
	::ENV.key?(key.to_s)
end