sugarfree-config 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +69 -0
  3. data/init.rb +2 -0
  4. data/lib/sugarfree-config.rb +152 -0
  5. metadata +58 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = SugarfreeConfig
2
+
3
+ Configuration handling the easy way.
4
+
5
+ SugarfreeConfig allows easy access to per Rails environment config values
6
+ stored in a YAML file.
7
+
8
+ SugarfreeConfig only works with Rails!!
9
+
10
+ == Installation
11
+
12
+ Just install the gem from Gemcutter
13
+
14
+ $ gem install sugarfree-config --source http://gemcutter.org
15
+
16
+ == Usage
17
+
18
+ Config must be stored in "#{RAILS_ROOT}/config/config.yml" and must have
19
+ at least a section for each RAILS_ENV
20
+
21
+ Configuration can be accessed like this
22
+
23
+ SugarfreeConfig.a.b.c #=> 'value'
24
+
25
+ which will expect a config file like (given development env)
26
+
27
+ development:
28
+ a:
29
+ b:
30
+ c: 'value'
31
+
32
+ otherwise a ConfigKeyException will be raised
33
+
34
+ Configuration is accessed through a ConfigIterator that (as its name says) is
35
+ and iterator and cannot be reused. However there is a workaround to allow the
36
+ repeated use of scoped iterators.
37
+
38
+ # This doesn't work
39
+ var a_config = app_config.a
40
+ a_config.b.c #=> 'value'
41
+ a_config.b.c #=> Ups!!! trie to grab config.a.b.c.b.c
42
+
43
+ # Workaround
44
+ var a_config = app_config.a
45
+ a_config.dup.b.c #=> 'value'
46
+ a_config.dup.b.c #=> 'value'
47
+
48
+ == Further customization
49
+
50
+ By default Sugarfree-config caches in memory the YAML file. If you want the gem
51
+ to reload this file each time a value is accessed (like in development mode)
52
+ you must force the initialization of the SugarfreeConfig module.
53
+
54
+ This could be a possible Rails initializer:
55
+
56
+ SugarfreeConfig.init(Rails.env.development?) # True to force reload each time
57
+
58
+ class Object
59
+ # For people who prefer app_config.a.b.c instead of SugarfreeConfig.a.b.c
60
+ def app_config
61
+ SugarfreeConfig
62
+ end
63
+ end
64
+
65
+ == Contact
66
+
67
+ Please mail bugs, suggestions and patches to
68
+ contact@davidbarral.com[mailto:contact@davidbarral.com]
69
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'sugarfree-config'
2
+
@@ -0,0 +1,152 @@
1
+ #
2
+ # Sugarfree config allows easy access to the config values.
3
+ #
4
+ # See README.textile for usage info
5
+ #
6
+ module SugarfreeConfig
7
+
8
+ def self.init(force_reload = false)
9
+ @@config = Config.new(force_reload)
10
+ end
11
+
12
+ def self.method_missing(*args)
13
+ init unless @@config
14
+ @@config.send(*args)
15
+ end
16
+
17
+ #
18
+ # Exception raised by the module on unexpected behaviors
19
+ #
20
+ class ConfigException < Exception
21
+ end
22
+
23
+ #
24
+ # Exception raised by the Config Iterator when a key is not found
25
+ #
26
+ class ConfigKeyException < ConfigException
27
+
28
+ #
29
+ # Config key path (as a key collection) that failed
30
+ #
31
+ attr_accessor :key_path_elements
32
+
33
+ #
34
+ # Create a new exception with the not found key paths (+key_path_elements+
35
+ # Array)
36
+ #
37
+ def initialize(key_path_elements)
38
+ self.key_path_elements = [*key_path_elements].map(&:to_s)
39
+ super("Cannot find key #{self.key_path_elements.join('.')}")
40
+ end
41
+ end
42
+
43
+ #
44
+ # Config base object. Caches the configuration in memory an acts as a factory
45
+ # for the ConfigIterators needed to get config values
46
+ #
47
+ class Config
48
+
49
+ #
50
+ # Conifg file is expected at "#{RAILS_ROOT}/config/config.yml"
51
+ #
52
+ DEFAULT_CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'config.yml')
53
+
54
+ def values
55
+ @values = fetch_config unless @values && !@force_reload
56
+ @values
57
+ end
58
+
59
+ #
60
+ # Creates a new config object and load the config file into memory
61
+ #
62
+ def initialize(force_reload = false)
63
+ @force_reload = force_reload
64
+ end
65
+
66
+ #
67
+ # Returns all the config as a big hash
68
+ #
69
+ def to_hash
70
+ @values
71
+ end
72
+
73
+ #
74
+ # Here is the magic. The first request to config returns a new
75
+ # ConfigIterator that will handle the first +symbol+
76
+ #
77
+ def method_missing(symbol, *args)
78
+ ConfigIterator.new(values, symbol).next
79
+ end
80
+
81
+ protected
82
+
83
+ #
84
+ # Fetch the config from the file
85
+ #
86
+ def fetch_config
87
+ if Object.const_defined?('RAILS_DEFAULT_LOGGER') && RAILS_DEFAULT_LOGGER.debug?
88
+ RAILS_DEFAULT_LOGGER.debug "Loading #{DEFAULT_CONFIG_FILE}::#{RAILS_ENV}"
89
+ end
90
+ HashWithIndifferentAccess.new(YAML::load(File.new(DEFAULT_CONFIG_FILE))[RAILS_ENV])
91
+ end
92
+ end
93
+
94
+ #
95
+ # Config Iterator. Given a configuration hash it can navigate trough the
96
+ # values using method calls that will be translated into hash keys and
97
+ # indexed
98
+ #
99
+ class ConfigIterator
100
+
101
+ #
102
+ # Create a new iterator with a given +configuration+ and the first
103
+ # element of the path to be iterated (+first_path_element+)
104
+ #
105
+ def initialize(configuration, first_path_element)
106
+ @scoped_config = configuration
107
+ @path_elements = [first_path_element]
108
+ end
109
+
110
+ #
111
+ # Returns the current scope as a hash. Usefull to get a Big hash of config
112
+ # that will be used later.
113
+ #
114
+ def to_hash
115
+ @scoped_config
116
+ end
117
+
118
+ #
119
+ # Iterate to the next element in the path
120
+ #
121
+ # Algorithm:
122
+ # 1. Get the last element of the key path
123
+ # 2. Try to find it in the scoped config.
124
+ # 3. If not present raise an error
125
+ # 4. If present and is a hash we are not in a config leaf, so the scoped
126
+ # config is reset to this new value and self is returned
127
+ # 5. If present and is a value then return the value
128
+ #
129
+ def next
130
+ if (value = @scoped_config[@path_elements.last]).nil?
131
+ raise ConfigKeyException.new(@path_elements)
132
+ elsif value.is_a?(Hash)
133
+ @scoped_config = value
134
+ self
135
+ else
136
+ value
137
+ end
138
+ end
139
+
140
+ #
141
+ # Here is the magic. When an unknown symbol is passed this symbol is set
142
+ # as the last path element of this iteration, and the iterator is then
143
+ # forced to make that movement
144
+ #
145
+ def method_missing(symbol, *args)
146
+ @path_elements << symbol
147
+ self.next
148
+ end
149
+ end
150
+
151
+ end
152
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sugarfree-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Barral
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-01 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Access to per Rails environment configuration stored in a YAML file
17
+ email: contact@davidbarral.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - LICENSE
27
+ - init.rb
28
+ - lib/sugarfree-config.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/davidbarral/sugarfree-config
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Configuration handling the easy way
57
+ test_files: []
58
+