loadcfg 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,4 @@
1
+ v0.2
2
+ -- You can pass options to loadconfig method like your own environment and if will ignore to raise exceptions when no data is found the environment option.
1
3
  v0.1
2
4
  -- Allow you to load a configuration file, let's say config/whatever.yaml as constants
data/README.rdoc CHANGED
@@ -1,14 +1,18 @@
1
1
  == loadcfg
2
2
 
3
- Rails gem for read environments constants following some convention.
3
+ Rails gem for read environment variables configuration, just follow some convention.
4
4
 
5
5
  == Install
6
6
 
7
- gem install loadcfg --source http://gems.github.com or then gem 'loadcfg' in your bundle file.
7
+ gem install loadcfg
8
+
9
+ or then add gem 'loadcfg' to your Gemfile and run:
10
+
11
+ bundle install
8
12
 
9
13
  == Usage
10
14
 
11
- Let's say you have some configuration constants that depends on the environment you are working on, for example config/facebook.yml
15
+ Let's say you have some configuration variables that depend of the environment you are working on, for example config/facebook.yml
12
16
 
13
17
  development:
14
18
  app_id: <the_app_id_for_development_env>
@@ -27,21 +31,30 @@ Let's say you have some configuration constants that depends on the environment
27
31
  secret_key: <the_secret_key_for_production_env>
28
32
  callback_url: <the_callback_url_for_production_env>
29
33
 
30
- One of the conventions is that you should put the file in the config dir of your proyect, then in any class where you want to use the constants you call the loadconfig method passing the config file name as symbol, for example
34
+ One of the conventions is that you should put the file in the config directory of your rails project, then, in any class where you want to use the constants you just need to call loadconfig method passing the config file name as a symbol, for example:
31
35
 
32
36
  class FacebookHelper
33
37
  loadconfig :facebook
34
38
  end
35
39
 
36
- that's going to generate a constant for each key in the yaml config file with the file name as a prefix, for example
40
+ that's going to generate a constant for each key in the yaml config file, each constant will has file name as a prefix, for example:
37
41
 
38
42
  class FacebookHelper
39
43
  loadconfig :facebook
40
44
 
41
45
  def some_method_that_deals_with_facebook_api
42
46
  fb_id = FACEBOOK_APP_ID
43
- # here fb_id contains the value you gave for the environment you are working on
47
+ fb_secret_key = FACEBOOK_SECRET_KEY
48
+ # here fb_id and fb_secret_key contain the values you gave them for the environment you are working on
44
49
  end
45
50
  end
51
+
52
+ == Options
53
+
54
+ There is a couple of options you can pass to loadconfig method
55
+
56
+ loadconfig :facebook, :environment => :my_environment, :ignore_not_found => true
57
+
58
+ If there's no my_environment entry in the config/facebook.yml file there will be no exception raised.
46
59
 
47
60
  That's it.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('loadcfg', '0.1') do |p|
5
+ Echoe.new('loadcfg', '0.2') do |p|
6
6
  p.description = "Allow you to load any configuration constants from config/whatever.yml"
7
7
  p.url = "http://github.com/j4rs/loadcfg"
8
8
  p.author = "Jorge Rodriguez"
data/lib/loadcfg.rb CHANGED
@@ -7,10 +7,15 @@ module LoadCfg
7
7
  end
8
8
 
9
9
  module ClassMethods
10
- def loadconfig(yaml_file_name)
11
- loaded_yaml = YAML.load_file(Rails.root.join("config/#{yaml_file_name}.yml"))[Rails.env]
12
- loaded_yaml.keys.each do |key|
13
- self.const_set("#{[yaml_file_name, '_', key].join.upcase}", loaded_yaml[key])
10
+ def loadconfig(yaml_file_name, options = {})
11
+ environment = (options[:environment] || Rails.env).to_s
12
+ loaded_yaml = YAML.load_file(Rails.root.join("config/#{yaml_file_name}.yml"))[environment]
13
+ if loaded_yaml.present?
14
+ loaded_yaml.keys.each do |key|
15
+ self.const_set("#{[yaml_file_name, '_', key].join.upcase}", loaded_yaml[key])
16
+ end
17
+ else
18
+ raise "Data not found on enviroment: '#{environment}'" unless options[:ignore_not_found]
14
19
  end
15
20
  end
16
21
  end
data/loadcfg.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{loadcfg}
5
- s.version = "0.1"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [%q{Jorge Rodriguez}]
9
- s.date = %q{2011-08-09}
9
+ s.date = %q{2011-08-13}
10
10
  s.description = %q{Allow you to load any configuration constants from config/whatever.yml}
11
11
  s.email = %q{jorge.rodriguez.suarez@gmail.com}
12
12
  s.extra_rdoc_files = [%q{CHANGELOG}, %q{README.rdoc}, %q{lib/loadcfg.rb}]
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: loadcfg
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.1"
5
+ version: "0.2"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jorge Rodriguez
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-09 00:00:00 Z
13
+ date: 2011-08-13 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Allow you to load any configuration constants from config/whatever.yml