sugarfree-config 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +26 -12
  2. data/lib/sugarfree-config.rb +43 -40
  3. metadata +12 -5
data/README.rdoc CHANGED
@@ -2,10 +2,11 @@
2
2
 
3
3
  Configuration handling the easy way.
4
4
 
5
- SugarfreeConfig allows easy access to per Rails environment config values
6
- stored in a YAML file.
5
+ SugarfreeConfig allows easy access to per Rails environment config values
6
+ stored in a YAML file.
7
7
 
8
- SugarfreeConfig only works with Rails!!
8
+ SugarfreeConfig only works with Rails! since SugarfreecConfig 1.1.0 works
9
+ with rails 3 and ruby 1.9.1
9
10
 
10
11
  == Installation
11
12
 
@@ -13,11 +14,24 @@ Just install the gem from Gemcutter
13
14
 
14
15
  $ gem install sugarfree-config --source http://gemcutter.org
15
16
 
17
+ == Notes on rails 3
18
+
19
+ In rails 2.x gems were required after Rails.root was initialized. Default rails 3.0
20
+ application generator uses Bundler, and auto-requires all gems before application
21
+ initialization. This could be solved forcing the bundler not to require the gem.
22
+ In your Gemfile:
23
+
24
+ gem 'problematic-gem', :require => false
25
+
26
+ and adding an initializer with the require line
27
+
28
+ require 'problematic-gem'
29
+
16
30
  == Usage
17
31
 
18
32
  Config must be stored in "#{RAILS_ROOT}/config/config.yml" and must have
19
33
  at least a section for each RAILS_ENV
20
-
34
+
21
35
  Configuration can be accessed like this
22
36
 
23
37
  SugarfreeConfig.a.b.c #=> 'value'
@@ -32,24 +46,24 @@ which will expect a config file like (given development env)
32
46
  otherwise a ConfigKeyException will be raised
33
47
 
34
48
  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
49
+ an iterator and cannot be reused. However there is a workaround to allow the
36
50
  repeated use of scoped iterators.
37
51
 
38
52
  # This doesn't work
39
- var a_config = app_config.a
53
+ var a_config = SugarfreeConfig.a
40
54
  a_config.b.c #=> 'value'
41
- a_config.b.c #=> Ups!!! trie to grab config.a.b.c.b.c
42
-
55
+ a_config.b.c #=> Ups!!! tried to grab config.a.b.c.b.c
56
+
43
57
  # Workaround
44
- var a_config = app_config.a
58
+ var a_config = SugarfreeConfig.a
45
59
  a_config.dup.b.c #=> 'value'
46
60
  a_config.dup.b.c #=> 'value'
47
61
 
48
62
  == Further customization
49
63
 
50
- By default Sugarfree-config caches in memory the YAML file. If you want the gem
64
+ By default Sugarfree-config caches in memory the YAML file. If you want the gem
51
65
  to reload this file each time a value is accessed (like in development mode)
52
- you must force the initialization of the SugarfreeConfig module.
66
+ you must force the initialization of the SugarfreeConfig module.
53
67
 
54
68
  This could be a possible Rails initializer:
55
69
 
@@ -64,6 +78,6 @@ This could be a possible Rails initializer:
64
78
 
65
79
  == Contact
66
80
 
67
- Please mail bugs, suggestions and patches to
81
+ Please mail bugs, suggestions and patches to
68
82
  contact@davidbarral.com[mailto:contact@davidbarral.com]
69
83
 
@@ -1,17 +1,20 @@
1
1
  #
2
2
  # Sugarfree config allows easy access to the config values.
3
- #
4
- # See README.textile for usage info
3
+ #
4
+ # See README.textile for usage info
5
5
  #
6
6
  module SugarfreeConfig
7
-
8
- def self.init(force_reload = false)
9
- @@config = Config.new(force_reload)
7
+
8
+ class << self
9
+ attr_reader :config
10
+ def init(force_reload = false)
11
+ @config = Config.new(force_reload)
12
+ end
10
13
  end
11
14
 
12
15
  def self.method_missing(*args)
13
- init unless @@config
14
- @@config.send(*args)
16
+ init unless SugarfreeConfig.config
17
+ SugarfreeConfig.config.send(*args)
15
18
  end
16
19
 
17
20
  #
@@ -19,17 +22,17 @@ module SugarfreeConfig
19
22
  #
20
23
  class ConfigException < Exception
21
24
  end
22
-
25
+
23
26
  #
24
27
  # Exception raised by the Config Iterator when a key is not found
25
28
  #
26
29
  class ConfigKeyException < ConfigException
27
-
30
+
28
31
  #
29
32
  # Config key path (as a key collection) that failed
30
33
  #
31
34
  attr_accessor :key_path_elements
32
-
35
+
33
36
  #
34
37
  # Create a new exception with the not found key paths (+key_path_elements+
35
38
  # Array)
@@ -39,28 +42,28 @@ module SugarfreeConfig
39
42
  super("Cannot find key #{self.key_path_elements.join('.')}")
40
43
  end
41
44
  end
42
-
45
+
43
46
  #
44
47
  # Config base object. Caches the configuration in memory an acts as a factory
45
48
  # for the ConfigIterators needed to get config values
46
49
  #
47
50
  class Config
48
-
51
+
49
52
  #
50
53
  # Conifg file is expected at "#{RAILS_ROOT}/config/config.yml"
51
- #
52
- DEFAULT_CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'config.yml')
53
-
54
+ #
55
+ DEFAULT_CONFIG_FILE = Rails.root.join('config', 'config.yml')
56
+
54
57
  def values
55
- @values = fetch_config unless @values && !@force_reload
58
+ @values = fetch_config unless @values && !@force_reload
56
59
  @values
57
60
  end
58
-
61
+
59
62
  #
60
63
  # Creates a new config object and load the config file into memory
61
64
  #
62
- def initialize(force_reload = false)
63
- @force_reload = force_reload
65
+ def initialize(force_reload = false)
66
+ @force_reload = force_reload
64
67
  end
65
68
 
66
69
  #
@@ -69,56 +72,56 @@ module SugarfreeConfig
69
72
  def to_hash
70
73
  @values
71
74
  end
72
-
75
+
73
76
  #
74
- # Here is the magic. The first request to config returns a new
77
+ # Here is the magic. The first request to config returns a new
75
78
  # ConfigIterator that will handle the first +symbol+
76
79
  #
77
80
  def method_missing(symbol, *args)
78
81
  ConfigIterator.new(values, symbol).next
79
82
  end
80
-
83
+
81
84
  protected
82
-
85
+
83
86
  #
84
87
  # Fetch the config from the file
85
88
  #
86
89
  def fetch_config
87
- if Object.const_defined?('RAILS_DEFAULT_LOGGER') && RAILS_DEFAULT_LOGGER.debug?
90
+ if Object.const_defined?('RAILS_DEFAULT_LOGGER') && RAILS_DEFAULT_LOGGER.debug?
88
91
  RAILS_DEFAULT_LOGGER.debug "Loading #{DEFAULT_CONFIG_FILE}::#{RAILS_ENV}"
89
92
  end
90
- HashWithIndifferentAccess.new(YAML::load(File.new(DEFAULT_CONFIG_FILE))[RAILS_ENV])
91
- end
93
+ HashWithIndifferentAccess.new(YAML::load(File.new(DEFAULT_CONFIG_FILE))[RAILS_ENV])
94
+ end
92
95
  end
93
96
 
94
97
  #
95
98
  # Config Iterator. Given a configuration hash it can navigate trough the
96
- # values using method calls that will be translated into hash keys and
99
+ # values using method calls that will be translated into hash keys and
97
100
  # indexed
98
101
  #
99
102
  class ConfigIterator
100
-
103
+
101
104
  #
102
- # Create a new iterator with a given +configuration+ and the first
105
+ # Create a new iterator with a given +configuration+ and the first
103
106
  # element of the path to be iterated (+first_path_element+)
104
107
  #
105
108
  def initialize(configuration, first_path_element)
106
109
  @scoped_config = configuration
107
110
  @path_elements = [first_path_element]
108
111
  end
109
-
112
+
110
113
  #
111
114
  # Returns the current scope as a hash. Usefull to get a Big hash of config
112
115
  # that will be used later.
113
116
  #
114
- def to_hash
117
+ def to_hash
115
118
  @scoped_config
116
119
  end
117
-
120
+
118
121
  #
119
122
  # Iterate to the next element in the path
120
- #
121
- # Algorithm:
123
+ #
124
+ # Algorithm:
122
125
  # 1. Get the last element of the key path
123
126
  # 2. Try to find it in the scoped config.
124
127
  # 3. If not present raise an error
@@ -126,7 +129,7 @@ module SugarfreeConfig
126
129
  # config is reset to this new value and self is returned
127
130
  # 5. If present and is a value then return the value
128
131
  #
129
- def next
132
+ def next
130
133
  if (value = @scoped_config[@path_elements.last]).nil?
131
134
  raise ConfigKeyException.new(@path_elements)
132
135
  elsif value.is_a?(Hash)
@@ -136,17 +139,17 @@ module SugarfreeConfig
136
139
  value
137
140
  end
138
141
  end
139
-
142
+
140
143
  #
141
144
  # 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
145
+ # as the last path element of this iteration, and the iterator is then
143
146
  # forced to make that movement
144
147
  #
145
148
  def method_missing(symbol, *args)
146
149
  @path_elements << symbol
147
150
  self.next
148
- end
151
+ end
149
152
  end
150
-
153
+
151
154
  end
152
-
155
+
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarfree-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 0
9
+ version: 1.1.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - David Barral
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-12-01 00:00:00 +01:00
17
+ date: 2010-03-19 00:00:00 +01:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -39,18 +44,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
44
  requirements:
40
45
  - - ">="
41
46
  - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
42
49
  version: "0"
43
- version:
44
50
  required_rubygems_version: !ruby/object:Gem::Requirement
45
51
  requirements:
46
52
  - - ">="
47
53
  - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
48
56
  version: "0"
49
- version:
50
57
  requirements: []
51
58
 
52
59
  rubyforge_project:
53
- rubygems_version: 1.3.5
60
+ rubygems_version: 1.3.6
54
61
  signing_key:
55
62
  specification_version: 3
56
63
  summary: Configuration handling the easy way