easyconf-rails 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +30 -0
- data/lib/easyconf-rails.rb +62 -4
- data.tar.gz.sig +0 -0
- metadata +32 -45
- metadata.gz.sig +0 -0
data/README.md
CHANGED
@@ -58,3 +58,33 @@ Now you're ready to use `config` in your application.
|
|
58
58
|
|
59
59
|
# You can reload your configuration file by with .reload!.
|
60
60
|
$config.reload!
|
61
|
+
|
62
|
+
BIG RED WARNING
|
63
|
+
---------------
|
64
|
+
|
65
|
+
`$config` is a *mutable global variable*, and should therefore be treated with
|
66
|
+
extreme caution. In particular, `$config.reload!` will reset the value of
|
67
|
+
`$config` in *all* threads; it is therefore important to ensure that a program's
|
68
|
+
corrent functioning does not rely on subsequent accesses to the `$config` value
|
69
|
+
to be consistent.
|
70
|
+
|
71
|
+
THE WRONG WAY
|
72
|
+
^^^^^^^^^^^^^
|
73
|
+
|
74
|
+
Person[$config.good_guy].kill(Person[$config.bad_guy])
|
75
|
+
|
76
|
+
is wrong because `$config.reload!` could have been called after evaluating the
|
77
|
+
value of `Person[$config.good_guy]`, but *before* calling its _kill()_ method.
|
78
|
+
If `$config.bad_guy` was set to the former value of `$config.good_guy`, we would
|
79
|
+
be asking `Person[$config.bad_guy]` to kill itself.
|
80
|
+
|
81
|
+
THE RIGHT WAY
|
82
|
+
^^^^^^^^^^^^^
|
83
|
+
|
84
|
+
$config.lock do
|
85
|
+
Person[config.good_guy].kill(Person[config.bad_guy])
|
86
|
+
end
|
87
|
+
|
88
|
+
is correct because this gaurantees that `config.bad_guy` and `config.good_guy`
|
89
|
+
are consistent. Even if they're switched in the interim, no one will ever be
|
90
|
+
asked to kill themselves.
|
data/lib/easyconf-rails.rb
CHANGED
@@ -41,6 +41,47 @@ class SemiOpenStruct < OpenStruct
|
|
41
41
|
end
|
42
42
|
|
43
43
|
class EasyConfRails < Rails::Railtie
|
44
|
+
# This is a base class for easyconf errors.
|
45
|
+
class EasyConfError < Exception
|
46
|
+
end
|
47
|
+
|
48
|
+
# NonexistentConfig is raise when config.yml doesn't exist.
|
49
|
+
class NonexistentConfig < EasyConfError
|
50
|
+
def initialize(msg="config.yml doesn't exist.")
|
51
|
+
super(msg)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# InvalidConfig is raised when config.yml is invalid YAML.
|
56
|
+
class InvalidConfig < EasyConfError
|
57
|
+
# @line is the line where the syntax error occurred.
|
58
|
+
attr_reader :line
|
59
|
+
# @coulumn is the column where the syntax error occurred.
|
60
|
+
attr_reader :column
|
61
|
+
|
62
|
+
def initialize(line, column, msg="Syntax error in config.yml at line "\
|
63
|
+
"#{line}, column #{column}")
|
64
|
+
@line = line
|
65
|
+
@column = column
|
66
|
+
super(msg)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# InvalidDefaults is raised when config/defaults.yml is invalid YAML.
|
71
|
+
class InvalidDefaults < EasyConfError
|
72
|
+
# @line is the line where the syntax error occurred.
|
73
|
+
attr_reader :line
|
74
|
+
# @coulumn is the column where the syntax error occurred.
|
75
|
+
attr_reader :column
|
76
|
+
|
77
|
+
def initialize(line, column, msg="Syntax error in config/defaults.yml at "\
|
78
|
+
"line #{line}, column #{column}")
|
79
|
+
@line = line
|
80
|
+
@column = column
|
81
|
+
super(msg)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
44
85
|
# Turn a Hash or Array and all of its Hash children into OpenStructs.
|
45
86
|
def hash_to_openstruct(hash)
|
46
87
|
(hash.is_a?(Hash) ? hash.keys : (0...hash.length)).each do |key|
|
@@ -81,14 +122,22 @@ class EasyConfRails < Rails::Railtie
|
|
81
122
|
def load_config()
|
82
123
|
config_location = Rails.root + 'config.yml'
|
83
124
|
if File.exists?(config_location)
|
84
|
-
|
125
|
+
begin
|
126
|
+
config_hash = YAML.load_file(config_location)
|
127
|
+
rescue Psych::SyntaxError => ex
|
128
|
+
raise InvalidConfig.new(ex.line, ex.column)
|
129
|
+
end
|
85
130
|
else
|
86
|
-
|
131
|
+
raise(NonexistentConfig)
|
87
132
|
end
|
88
133
|
|
89
134
|
defaults_location = Rails.root + 'config' + 'defaults.yml'
|
90
135
|
if File.exists?(defaults_location)
|
91
|
-
|
136
|
+
begin
|
137
|
+
defaults_hash = YAML.load_file(defaults_location)
|
138
|
+
rescue Psych::SyntaxError => ex
|
139
|
+
raise InvalidDefaults.new(ex.line, ex.column)
|
140
|
+
end
|
92
141
|
else
|
93
142
|
defaults_hash = {}
|
94
143
|
end
|
@@ -104,6 +153,15 @@ class EasyConfRails < Rails::Railtie
|
|
104
153
|
end
|
105
154
|
|
106
155
|
config.before_initialize do
|
107
|
-
|
156
|
+
begin
|
157
|
+
assign_config()
|
158
|
+
rescue NonexistentConfig, InvalidConfig, InvalidDefaults
|
159
|
+
warn($!.message)
|
160
|
+
exit(1)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
trap(:HUP) do
|
165
|
+
$config.reload!
|
108
166
|
end
|
109
167
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easyconf-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,52 +9,39 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain:
|
12
|
-
- !
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
cpXaWg/oMtXJ2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MmxlVmFlL2ZKMDN6WUdBcmh1SktQZ2M9Ci0tLS0tRU5E
|
39
|
+
IENFUlRJRklDQVRFLS0tLS0K
|
40
|
+
date: 2012-05-07 00:00:00.000000000 Z
|
54
41
|
dependencies:
|
55
42
|
- !ruby/object:Gem::Dependency
|
56
43
|
name: rails
|
57
|
-
requirement: &
|
44
|
+
requirement: &74240240 !ruby/object:Gem::Requirement
|
58
45
|
none: false
|
59
46
|
requirements:
|
60
47
|
- - ! '>='
|
@@ -62,7 +49,7 @@ dependencies:
|
|
62
49
|
version: '0'
|
63
50
|
type: :runtime
|
64
51
|
prerelease: false
|
65
|
-
version_requirements: *
|
52
|
+
version_requirements: *74240240
|
66
53
|
description: Configure Rails apps with a simple YAML file.
|
67
54
|
email: the.magical.kat@gmail.com
|
68
55
|
executables: []
|
metadata.gz.sig
CHANGED
Binary file
|