confit 0.0.5 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,18 +1,22 @@
1
+ **N.B. Confit 1.0 breaks backwards-compatibility with earlier versions!**
2
+
1
3
  Installation
2
4
  ------------
3
5
  * sudo gem install confit
4
6
 
5
7
  Initialization
6
8
  --------------
7
- * require 'confit'
8
- * confit('path/to/config.yml')
9
+ * `require 'confit'`
10
+ `confit('path/to/file_name.yml')`
9
11
 
10
12
  Usage
11
13
  -----
12
- * puts confit.some_key # "The value of this key!"
14
+ * `puts confit.file_name.some_key # "The value of this key!"`
13
15
 
14
16
  Notes
15
17
  -----
16
- * Optional initialization params include environment and strict mode:
17
- * confit('path/to/config.yml', 'development', true)
18
- * Strict mode raises a NoMethodError if that key hasn't been loaded from your yaml file.
18
+ * Optional initialization params include environment, strict mode, and force reload of file
19
+ * `confit('path/to/file_name.yml', 'development', true, true) # confit.file_name.key_name -- from the yaml block named "development"`
20
+ * Strict mode raises a `MissingVariableError` if that key hasn't been loaded from your yaml file.
21
+ * Force reload ensures that the file is processed, even if Confit has already seen it before. This is useful for loading multiple
22
+ environments at different times from within one file.
data/lib/confit.rb CHANGED
@@ -1,9 +1,21 @@
1
1
  require 'ostruct'
2
+ require 'rubygems'
3
+
4
+ class InvalidFileNameError < StandardError; end
5
+ class MissingVariableError < StandardError; end
6
+ class MissingFileError < StandardError; end
2
7
 
3
8
  module Confit
4
9
 
5
10
  @@app_config = nil
6
11
  @@strict = nil
12
+ @@files = Array.new
13
+ @@current_file_name = nil
14
+ @@debug = false
15
+
16
+ def self.debug(msg)
17
+ puts "\nDebug:\t#{msg}" if @@debug
18
+ end
7
19
 
8
20
  def self.strict
9
21
  @@strict
@@ -14,31 +26,72 @@ module Confit
14
26
  end
15
27
 
16
28
  def self.confit(file=nil, env=nil, strict=false, force=false)
17
-
18
- @@app_config ? (return @@app_config if not force) : @@app_config = OpenStruct.new
19
-
29
+ self.debug("self.confit(file=#{file}, env=#{env}, strict=#{strict}, force=#{force})")
30
+
31
+ @@app_config = OpenStruct.new if not @@app_config
32
+
33
+ if not @@files.include?(file) and not file.nil?
34
+ self.debug "New file: #{file}"
35
+ self.prep_config(file)
36
+ else
37
+ self.debug "File exists: (#{force}) #{file}"
38
+ return @@app_config if not force
39
+ self.debug "Forcing reload of file"
40
+ end
41
+
20
42
  @@strict = strict ? true : false
21
43
 
44
+ self.process_file(file, env)
45
+
46
+ @@app_config
47
+ end
48
+
49
+ def self.prep_config(file)
50
+ file =~ /([^\/]+)\.yml$/i
51
+ raise InvalidFileNameError, "Filename is not valid: #{file}" if not $1
52
+
53
+ @@current_file_name = $1
54
+ self.debug "Current file #{@@current_file_name}"
55
+ @@files << file
56
+ @@app_config.send("#{@@current_file_name}=", OpenStruct.new)
57
+ end
58
+
59
+ def self.process_file(file, env=nil)
22
60
  if not file.nil?
23
61
  if File.exist?(file)
24
62
  yaml = env ? YAML.load_file(file)[env] : YAML.load_file(file)
25
- yaml.each do |key, val|
26
- @@app_config.send("#{self.prep_key(key)}=", val)
27
- end
63
+ self.parse_hash(yaml, @@app_config.send(@@current_file_name))
28
64
  else
29
- raise IOError, "File #{file} does not exist!"
65
+ raise MissingFileError, "File #{file} does not exist!"
30
66
  end
31
67
  end
32
-
33
- @@app_config
34
68
  end
35
-
69
+
70
+ def self.load_pair(key, val, parent)
71
+ self.debug "load_pair: #{parent}.send(#{key}=, #{val})"
72
+ parent.send("#{key}=", val)
73
+ end
74
+
75
+ def self.parse_hash(zee_hash, parent)
76
+ zee_hash.each do |key, val|
77
+ if val.is_a?(Hash)
78
+ self.debug "is a Hash #{key},#{val}"
79
+ self.load_pair(key, OpenStruct.new, parent)
80
+ self.parse_hash(val, parent.send(key))
81
+ else
82
+ self.debug "Not a Hash #{key},#{val}"
83
+ self.load_pair(key, val, parent)
84
+ end
85
+
86
+ end
87
+ end
88
+
36
89
  end
37
90
 
38
91
  module Kernel
39
92
 
40
93
  def confit(file=nil, env=nil, strict=false, force=false)
41
- Confit::confit(file, env, strict)
94
+ Confit::confit(file, env, strict, force)
42
95
  end
43
96
 
44
97
  end
@@ -48,7 +101,7 @@ class OpenStruct
48
101
  def method_missing(mid, *args)
49
102
  mname = mid.id2name
50
103
  if mname !~ /=/
51
- raise NoMethodError, "Confit variable not defined! confit.#{mname}" if Confit.strict
104
+ raise MissingVariableError, "Confit variable not defined! #{mname}" if Confit.strict
52
105
  else
53
106
  len = args.length
54
107
  if mname.chomp!('=')
data/test/complex.yml ADDED
@@ -0,0 +1,26 @@
1
+ jo_dev:
2
+ app_name: jo dev
3
+ api_key: theapikey
4
+ secret_key: thesecret
5
+ canvas_page_name: jo_dev_test
6
+ callback_url: http://urldoesntexist.com
7
+ page_id: 12345
8
+ app_id: 56789
9
+ page_url: http://www.facebook.com/apps/application.php?id=56789
10
+ page_tab_url: http://www.facebook.com/apps/application.php?id=56789&v=app_56789
11
+ uid: 100000981545471
12
+
13
+ poeks_dev:
14
+ app_name: poeks dev
15
+ api_key: thepoeksapikey
16
+ secret_key: thepoekssecretkey
17
+ canvas_page_name: poeks_dev_test
18
+ callback_url: http://urldontexist.com
19
+ page_id: 923048
20
+ app_id: 685490605
21
+ page_url: http://www.facebook.com/apps/application.php?id=685490605
22
+ page_tab_url: http://www.facebook.com/apps/application.php?id=685490605&v=app_685490605
23
+ uid: 655764281
24
+ a_hash:
25
+ name: the hash
26
+ value: the value
data/test/confit_test.rb CHANGED
@@ -1,30 +1,35 @@
1
1
  require File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'lib', 'confit')
2
2
 
3
3
  require 'test/unit'
4
- require 'rubygems'
5
4
 
6
5
  class ConfitTest < Test::Unit::TestCase
7
-
8
- def _init
9
- file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'test.yml')
6
+
7
+ def test_missing
8
+ file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'simple.yml')
10
9
  confit(file, 'dev', true)
10
+ puts "Doesn't exist: #{confit.test.your_mom}"
11
+ rescue MissingVariableError => e
12
+ assert_equal(e.class.to_s, "MissingVariableError")
11
13
  end
12
14
 
13
- def test_main_methods
14
- _init
15
- assert_equal("twitterpoeks@gmail.com", confit.email)
15
+ def test_simple
16
+ file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'simple.yml')
17
+ confit(file, 'dev', true)
18
+ assert_equal("Poeks", confit.simple.author)
16
19
  end
17
20
 
18
- def test_missing
19
- _init
20
- puts "Doesn't exist: #{confit.your_mom}"
21
- rescue NoMethodError => e
22
- puts "Rescued NoMethodError for confit.your_mom!"
21
+ def test_complex
22
+ file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'complex.yml')
23
+ confit(file, nil, true)
24
+ assert_equal("the value", confit.complex.poeks_dev.a_hash.value)
23
25
  end
24
26
 
25
- def test_spacey
26
- _init
27
- assert_equal(confit.a_key_with_spaces, "Now underscores")
27
+ def test_force
28
+ file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'complex.yml')
29
+ confit(file, 'jo_dev', true, true)
30
+ assert_equal("jo dev", confit.complex.app_name)
31
+ confit(file, 'poeks_dev', true, true)
32
+ assert_equal("poeks dev", confit.complex.app_name)
28
33
  end
29
34
 
30
35
  end
File without changes
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 0
9
- - 5
10
- version: 0.0.5
9
+ version: "1.0"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Jen Oslislo
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-11-22 00:00:00 -08:00
17
+ date: 2010-11-30 00:00:00 -08:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -30,8 +29,9 @@ extra_rdoc_files: []
30
29
 
31
30
  files:
32
31
  - lib/confit.rb
32
+ - test/complex.yml
33
33
  - test/confit_test.rb
34
- - test/test.yml
34
+ - test/simple.yml
35
35
  - README.md
36
36
  has_rdoc: true
37
37
  homepage: https://github.com/poeks/confit