confit 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.md +18 -0
  2. data/lib/confit.rb +21 -13
  3. data/test/confit_test.rb +17 -5
  4. data/test/test.yml +3 -2
  5. metadata +7 -7
  6. data/README +0 -12
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ Installation
2
+ ------------
3
+ * sudo gem install confit
4
+
5
+ Initialization
6
+ --------------
7
+ * require 'confit'
8
+ * confit('path/to/config.yml')
9
+
10
+ Usage
11
+ -----
12
+ * puts confit.some_key # "The value of this key!"
13
+
14
+ Notes
15
+ -----
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.
data/lib/confit.rb CHANGED
@@ -3,22 +3,30 @@ require 'ostruct'
3
3
  module Confit
4
4
 
5
5
  @@app_config = nil
6
- @@verbose = nil
6
+ @@strict = nil
7
7
 
8
- def self.verbose
9
- @@verbose
8
+ def self.strict
9
+ @@strict
10
10
  end
11
11
 
12
- def self.confit(file=nil, env=nil, verbose=false)
12
+ def self.prep_key(key)
13
+ key.gsub(/\s/, "_")
14
+ end
15
+
16
+ def self.confit(file=nil, env=nil, strict=false)
13
17
 
14
18
  @@app_config ? (return @@app_config) : @@app_config = OpenStruct.new
15
19
 
16
- @@verbose = verbose ? true : false
17
-
18
- if File.exist?(file)
19
- yaml = env ? YAML.load_file(file)[env] : YAML.load_file(file)
20
- yaml.each do |key, val|
21
- @@app_config.send("#{key}=", val)
20
+ @@strict = strict ? true : false
21
+
22
+ if not file.nil?
23
+ if File.exist?(file)
24
+ 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
28
+ else
29
+ raise IOError, "File #{file} does not exist!"
22
30
  end
23
31
  end
24
32
 
@@ -29,8 +37,8 @@ end
29
37
 
30
38
  module Kernel
31
39
 
32
- def confit(file=nil, env=nil, verbose=false)
33
- Confit::confit(file, env, verbose)
40
+ def confit(file=nil, env=nil, strict=false)
41
+ Confit::confit(file, env, strict)
34
42
  end
35
43
 
36
44
  end
@@ -40,7 +48,7 @@ class OpenStruct
40
48
  def method_missing(mid, *args)
41
49
  mname = mid.id2name
42
50
  if mname !~ /=/
43
- raise NoMethodError, "Confit variable not defined! #{mname}" if Confit.verbose
51
+ raise NoMethodError, "Confit variable not defined! confit.#{mname}" if Confit.strict
44
52
  else
45
53
  len = args.length
46
54
  if mname.chomp!('=')
data/test/confit_test.rb CHANGED
@@ -3,16 +3,28 @@ require File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'li
3
3
  require 'test/unit'
4
4
  require 'rubygems'
5
5
 
6
- include Confit
7
-
8
6
  class ConfitTest < Test::Unit::TestCase
9
7
 
10
- def test_main_methods
8
+ def _init
11
9
  file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'test.yml')
12
- puts file
13
10
  confit(file, 'dev', true)
14
-
11
+ end
12
+
13
+ def test_main_methods
14
+ _init
15
15
  assert_equal("twitterpoeks@gmail.com", confit.email)
16
16
  end
17
17
 
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!"
23
+ end
24
+
25
+ def test_spacey
26
+ _init
27
+ assert_equal(confit.a_key_with_spaces, "Now underscores")
28
+ end
29
+
18
30
  end
data/test/test.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  dev:
2
- app_name: AppName
2
+ app_name: Confit
3
3
  email: twitterpoeks@gmail.com
4
- author: Poeks
4
+ author: Poeks
5
+ a key with spaces: Now underscores
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jen Oslislo
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-13 00:00:00 -08:00
18
+ date: 2010-11-14 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: A teeny, tiny configuration manager.
22
+ description: A teeny, tiny configuration manager for YAML config files.
23
23
  email:
24
24
  - twitterpoeks@gmail.com
25
25
  executables: []
@@ -32,7 +32,7 @@ files:
32
32
  - lib/confit.rb
33
33
  - test/confit_test.rb
34
34
  - test/test.yml
35
- - README
35
+ - README.md
36
36
  has_rdoc: true
37
37
  homepage: https://github.com/poeks/confit
38
38
  licenses: []
@@ -68,6 +68,6 @@ rubyforge_project: confit
68
68
  rubygems_version: 1.3.7
69
69
  signing_key:
70
70
  specification_version: 3
71
- summary: A teeny, tiny configuration manager.
71
+ summary: A teeny, tiny configuration manager for YAML config files.
72
72
  test_files: []
73
73
 
data/README DELETED
@@ -1,12 +0,0 @@
1
- Initializa lika dees:
2
- ---------------------
3
-
4
- require 'confit'
5
- include Confit
6
- confit('path/to/config.yml')
7
-
8
-
9
- Usa lika dees:
10
- --------------
11
-
12
- puts confit.database # your_db.db