confit 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2013b7e542bcf8dc35acdd6452fe262bf0707b1b
4
+ data.tar.gz: fdecbc102cba8a0293f062749420ead9ad495462
5
+ SHA512:
6
+ metadata.gz: 0ae85fe63e62917f8f35a3ad887447f3f0a0487ecfeceaff1eddfcf34f4eb27eb3dbbbbbade19c95da71f7d50e27d07a88f4b954cecd5ce7522e42fa1df41d74
7
+ data.tar.gz: 5b2726193ef4926eb8f9833965c9ce7d26a4ef2ec0fecd4b27d3aea10892e4e6611a885fb296ac4c603a7535168ba82ddebc143c66c8a5f43a81df36c2128108
data/lib/confit.rb CHANGED
@@ -1,86 +1,82 @@
1
1
  require 'ostruct'
2
2
  require 'rubygems'
3
- require 'extlib'
4
3
 
5
4
  class InvalidFileNameError < StandardError; end
6
5
  class MissingVariableError < StandardError; end
7
6
  class MissingFileError < StandardError; end
8
7
 
9
8
  module Confit
10
-
9
+
11
10
  @@app_config = nil
12
- @@strict = nil
11
+ @@strict = false
13
12
  @@files = Array.new
14
13
  @@current_file_name = nil
15
14
  @@debug = false
16
-
17
- def self.debug=(bool)
18
- @@debug = bool
19
- end
20
-
21
- def self.debug
22
- @@debug
23
- end
24
-
15
+ @@default_opts = lambda { return {:strict => @@strict, :force => false} }
16
+
25
17
  def self.debug(msg)
26
18
  puts "\nDebug:\t#{msg}" if @@debug
27
19
  end
28
-
20
+
29
21
  def self.strict
30
22
  @@strict
31
23
  end
32
24
 
33
- def self.prep_key(key)
34
- key.gsub(/\s/, "_")
35
- end
36
-
37
- def self.confit(file=nil, env=nil, strict=false, force=false)
38
- self.debug("self.confit(file=#{file}, env=#{env}, strict=#{strict}, force=#{force})")
39
-
40
- @@app_config = OpenStruct.new if not @@app_config
41
-
25
+ def self.confit(*args, &block)
26
+ file = args.shift
27
+ opts = (args.last and args.last.is_a? Hash) ? args.pop : @@default_opts.call
28
+ env = (args && args.compact) || []
29
+
30
+ self.debug("self.confit(file=#{file}, env=#{env}, strict=#{opts[:strict]}, force=#{opts[:force]})")
31
+
32
+ @@app_config ||= OpenStruct.new
33
+
42
34
  if not @@files.include?(file) and not file.nil?
43
35
  self.debug "New file: #{file}"
44
36
  self.prep_config(file)
45
37
  else
46
- self.debug "File exists: (#{force}) #{file}"
47
- return @@app_config if not force
38
+ self.debug "File exists: (#{opts[:force]}) #{file}"
39
+ return @@app_config unless opts[:force]
48
40
  self.debug "Forcing reload of file"
49
41
  end
50
-
51
- @@strict = strict ? true : false
52
-
53
- self.process_file(file, env)
42
+
43
+ @@strict = opts[:strict] ? true : false
44
+
45
+ self.process_file(file, env, &block)
54
46
 
55
47
  @@app_config
56
48
  end
57
-
49
+
58
50
  def self.prep_config(file)
59
51
  file =~ /([^\/]+)\.yml$/i
60
52
  raise InvalidFileNameError, "Filename is not valid: #{file}" if not $1
61
-
53
+
62
54
  @@current_file_name = $1
63
55
  self.debug "Current file #{@@current_file_name}"
64
56
  @@files << file
65
- @@app_config.send("#{@@current_file_name}=", OpenStruct.new)
57
+ @@app_config.send("#{@@current_file_name}=", OpenStruct.new)
66
58
  end
67
-
59
+
68
60
  def self.process_file(file, env=nil)
69
61
  if not file.nil?
70
62
  if File.exist?(file)
71
- yaml = env ? YAML.load_file(file)[env] : YAML.load_file(file)
63
+ #yaml = env ? YAML.load_file(file)[env] : YAML.load_file(file)
64
+ yaml = YAML.load_file(file)
65
+ env.each {|key| yaml = yaml[key.to_s]}
66
+ yield yaml if block_given?
72
67
  self.parse_hash(yaml, @@app_config.send(@@current_file_name))
73
68
  else
74
69
  raise MissingFileError, "File #{file} does not exist!"
75
70
  end
76
71
  end
77
72
  end
78
-
73
+
79
74
  def self.load_pair(key, val, parent)
80
- self.debug "load_pair: #{parent}.send(#{key}=, #{val})"
81
- parent.send("#{key}=", val)
75
+ _key = key.gsub /\s+/, '_'
76
+ self.debug "load_pair: #{parent}.send(#{_key}=, #{val})"
77
+ parent.send("#{_key}=", val)
82
78
  end
83
-
79
+
84
80
  def self.parse_hash(zee_hash, parent)
85
81
  zee_hash.each do |key, val|
86
82
  if val.is_a?(Hash)
@@ -91,14 +87,21 @@ module Confit
91
87
  self.debug "Not a Hash #{key},#{val}"
92
88
  self.load_pair(key, val, parent)
93
89
  end
94
-
90
+
95
91
  end
96
92
  end
97
-
93
+
94
+ def self.reset!
95
+ @@app_config = nil
96
+ @@strict = false
97
+ @@files = Array.new
98
+ @@current_file_name = nil
99
+ end
100
+
98
101
  end
99
102
 
100
103
  class OpenStruct
101
-
104
+
102
105
  def method_missing(mid, *args)
103
106
  mname = mid.id2name
104
107
  if mname !~ /=/
@@ -117,5 +120,5 @@ class OpenStruct
117
120
  end
118
121
  end
119
122
  end
120
-
123
+
121
124
  end
data/lib/confit/kernel.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'confit'
2
2
 
3
3
  module Kernel
4
-
5
- def confit(file=nil, env=nil, strict=false, force=false)
6
- Confit::confit(file, env, strict, force)
4
+
5
+ def confit(*args, &block)
6
+ Confit::confit(*args, &block)
7
7
  end
8
8
 
9
9
  end
data/test/complex.yml CHANGED
@@ -23,4 +23,12 @@ poeks_dev:
23
23
  uid: 655764281
24
24
  a_hash:
25
25
  name: the hash
26
- value: the value
26
+ value: the value
27
+
28
+ foo:
29
+ bar:
30
+ num: 1
31
+ boo: false
32
+ baz:
33
+ num: 2
34
+ boo: true
data/test/confit_test.rb CHANGED
@@ -1,35 +1,64 @@
1
1
  require File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'lib', 'confit')
2
2
 
3
3
  require 'test/unit'
4
+ require 'yaml'
4
5
 
5
6
  class ConfitTest < Test::Unit::TestCase
6
7
 
8
+ @@simple = File.join(File.expand_path(File.join(File.dirname(__FILE__))), 'simple.yml')
9
+ @@complex = File.join(File.expand_path(File.join(File.dirname(__FILE__))), 'complex.yml')
10
+
11
+ def setup
12
+ Confit.reset!
13
+ end
14
+
7
15
  def test_missing
8
- file = File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'test', 'simple.yml')
9
- confit(file, 'dev', true)
10
- puts "Doesn't exist: #{confit.test.your_mom}"
16
+ Confit::confit(@@simple, 'dev', :strict => true)
17
+ puts "Doesn't exist: #{Confit::confit.test.your_mom}"
11
18
  rescue MissingVariableError => e
12
19
  assert_equal(e.class.to_s, "MissingVariableError")
13
20
  end
14
-
21
+
15
22
  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)
23
+ Confit::confit(@@simple, 'dev', :strict => true)
24
+ assert_equal("Poeks", Confit::confit.simple.author)
19
25
  end
20
26
 
21
27
  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)
28
+ Confit::confit(@@complex, nil, :strict => true)
29
+ assert_equal("the value", Confit::confit.complex.poeks_dev.a_hash.value)
25
30
  end
26
-
31
+
27
32
  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)
33
+ Confit::confit(@@complex, 'jo_dev', :strict => true, :force => true)
34
+ assert_equal("jo dev", Confit::confit.complex.app_name)
35
+ Confit::confit(@@complex, 'poeks_dev', :strict => true, :force => true)
36
+ assert_equal("poeks dev", Confit::confit.complex.app_name)
37
+ end
38
+
39
+ def test_key_with_spaces
40
+ Confit::confit(@@simple, 'dev', :strict => true)
41
+ assert_equal "Now underscores", Confit::confit.simple.a_key_with_spaces
42
+ end
43
+
44
+ def test_nesting
45
+ Confit::confit(@@complex, 'foo', 'bar', :strict => false)
46
+ assert_equal 1, Confit::confit.complex.num
47
+ assert_equal false, Confit::confit.complex.boo
48
+ Confit::confit(@@complex, 'foo', 'baz', :force => true)
49
+ assert_equal 2, Confit::confit.complex.num
50
+ assert_equal true, Confit::confit.complex.boo
33
51
  end
34
-
52
+
53
+ def test_block
54
+ Confit::confit @@complex, 'foo', 'bar', :strict => false do |pre_parse_hash|
55
+ pre_parse_hash['boo_is_false'] = !pre_parse_hash['boo']
56
+ end
57
+ assert Confit::confit.complex.boo_is_false
58
+ Confit::confit @@complex, 'foo', 'baz', :force => true do |pre_parse_hash|
59
+ pre_parse_hash['boo_is_false'] = !pre_parse_hash['boo']
60
+ end
61
+ assert !Confit::confit.complex.boo_is_false
62
+ end
63
+
35
64
  end
metadata CHANGED
@@ -1,75 +1,51 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: confit
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 2
10
- version: 1.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Jen Oslislo
8
+ - Kenichi Nakamura
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-12 00:00:00 -07:00
19
- default_executable:
12
+ date: 2015-08-02 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
14
  description: A teeny, tiny configuration manager for YAML config files.
23
- email:
24
- - twitterpoeks@gmail.com
15
+ email:
16
+ - twitterpoeks@gmail.com, kenichi.nakamura@gmail.com
25
17
  executables: []
26
-
27
18
  extensions: []
28
-
29
19
  extra_rdoc_files: []
30
-
31
- files:
32
- - lib/confit/kernel.rb
20
+ files:
21
+ - README.md
33
22
  - lib/confit.rb
23
+ - lib/confit/kernel.rb
34
24
  - test/complex.yml
35
25
  - test/confit_test.rb
36
26
  - test/simple.yml
37
- - README.md
38
- has_rdoc: true
39
27
  homepage: https://github.com/poeks/confit
40
28
  licenses: []
41
-
29
+ metadata: {}
42
30
  post_install_message:
43
31
  rdoc_options: []
44
-
45
- require_paths:
32
+ require_paths:
46
33
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- hash: 3
53
- segments:
54
- - 0
55
- version: "0"
56
- required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 23
62
- segments:
63
- - 1
64
- - 3
65
- - 6
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
66
43
  version: 1.3.6
67
44
  requirements: []
68
-
69
45
  rubyforge_project: confit
70
- rubygems_version: 1.4.1
46
+ rubygems_version: 2.4.8
71
47
  signing_key:
72
- specification_version: 3
48
+ specification_version: 4
73
49
  summary: A teeny, tiny configuration manager for YAML config files.
74
50
  test_files: []
75
-
51
+ has_rdoc: