konfa 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/konfa.rb +95 -0
  2. metadata +68 -0
data/lib/konfa.rb ADDED
@@ -0,0 +1,95 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'yaml'
3
+
4
+ module Konfa
5
+ class Base
6
+
7
+ @@env_variable_prefix = 'APP_'
8
+ @@variables = {}
9
+
10
+ class << self
11
+
12
+ def get(variable)
13
+ raise UnsupportedVariableError.new(variable) unless @@variables.has_key? variable
14
+ @@variables[variable]
15
+ end
16
+
17
+ def set(variable, value = nil)
18
+ raise UnsupportedVariableError.new(variable) unless @@variables.has_key? variable
19
+ @@variables[variable] = value
20
+ end
21
+
22
+ def true?(variable)
23
+ v = self.get(variable)
24
+ (!v.nil? && v =~ /^\s*(?:true|1|yes|on)\s*$/i) ? true : false
25
+ end
26
+
27
+ def false?(variable)
28
+ self.true?(variable) == false
29
+ end
30
+
31
+ def variables
32
+ @@variables.keys
33
+ end
34
+
35
+ def dump
36
+ @@variables.dup
37
+ end
38
+
39
+ def env_variable_prefix
40
+ @@env_variable_prefix
41
+ end
42
+
43
+ def initialize_from_yaml(path)
44
+ # FIXME: It would be a lot cleaner if the YAML library would raise an
45
+ # exception if it fails to read the file. We'll handle it like this for now
46
+ # load_file just returns "false" if it fails
47
+
48
+ yaml_data = YAML.load_file(path)
49
+ yaml_data.each do |variable, value|
50
+ raise UnsupportedVariableError.new(variable) unless @@variables.has_key? variable.to_sym
51
+ set(variable.to_sym, value.to_s)
52
+ end
53
+ after_initialize
54
+ dump
55
+ end
56
+
57
+ def initialize_from_env(prefix=@@env_variable_prefix)
58
+ ENV.keys.reject { |key|
59
+ key !~ /^#{prefix}/ # Ignore everything that doesn't match the prefix
60
+ }.each { |key|
61
+ variable = key[prefix.size..-1].downcase.to_sym
62
+ unless @@variables.has_key? variable
63
+ raise UnsupportedVariableError.new(variable)
64
+ end
65
+
66
+ set(variable, ENV[key])
67
+ }
68
+ after_initialize
69
+ dump
70
+ end
71
+
72
+ def after_initialize
73
+ end
74
+
75
+ def with_config(overrides={})
76
+ originals = dump
77
+ overrides.each_pair {|k,v| self.set(k, v) }
78
+
79
+ begin
80
+ result = yield
81
+ ensure
82
+ @@variables = originals
83
+ end
84
+
85
+ result
86
+ end
87
+ end
88
+ end
89
+
90
+ class InitializationError < StandardError
91
+ end
92
+
93
+ class UnsupportedVariableError < StandardError
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: konfa
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Gunnar Hansson
14
+ - Avidity
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2013-09-20 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Helps you avoid common pitfalls when dealing with app config
24
+ email: code@avidiy.se
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/konfa.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/avidity/konfa
35
+ licenses:
36
+ - MIT
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.6.2
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Application configuration
67
+ test_files: []
68
+