hopsoft-fig 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'echoe'
2
2
 
3
- Echoe.new('fig', '0.8.3') do |p|
3
+ Echoe.new('fig', '0.8.4') do |p|
4
4
  p.description = "The smart way to manage configuration settings for your Ruby applications."
5
5
  p.url = "http://github.com/hopsoft/fig"
6
6
  p.author = "Nathan Hopkins, Hopsoft LLC"
data/fig.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{fig}
5
- s.version = "0.8.3"
5
+ s.version = "0.8.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Nathan Hopkins, Hopsoft LLC"]
9
- s.date = %q{2009-03-15}
9
+ s.date = %q{2009-03-16}
10
10
  s.description = %q{The smart way to manage configuration settings for your Ruby applications.}
11
11
  s.email = %q{natehop@gmail.com}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/fig.rb", "lib/string.rb", "tasks/fig_tasks.rake"]
13
- s.files = ["README.rdoc", "install.rb", "MIT-LICENSE", "test/test.yml", "test/test2.yml", "test/string_test.rb", "test/fig_test.rb", "Manifest", "uninstall.rb", "Rakefile", "init.rb", "lib/fig.rb", "lib/string.rb", "tasks/fig_tasks.rake", "fig.gemspec"]
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/hopsoft/fig.rb", "lib/string.rb", "tasks/fig_tasks.rake"]
13
+ s.files = ["README.rdoc", "install.rb", "MIT-LICENSE", "test/test.yml", "test/test2.yml", "test/string_test.rb", "test/fig_test.rb", "Manifest", "uninstall.rb", "Rakefile", "init.rb", "lib/hopsoft/fig.rb", "lib/string.rb", "tasks/fig_tasks.rake", "fig.gemspec"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://github.com/hopsoft/fig}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Fig", "--main", "README.rdoc"]
@@ -0,0 +1,134 @@
1
+ require 'yaml'
2
+ require 'ostruct'
3
+ require 'thread'
4
+ require File.dirname(__FILE__) + '/../string'
5
+
6
+ module Hopsoft
7
+ class Fig
8
+
9
+ # Constructor...
10
+ #
11
+ # ===Params
12
+ # * *file_path* - Path to the config file that should be loaded.
13
+ def initialize(file_path)
14
+ @lock = Mutex.new
15
+ @file_path = file_path
16
+ load
17
+ end
18
+
19
+ # Returns the config file file as a YAML Hash.
20
+ def yaml
21
+ copy = {}
22
+ @lock.synchronize do
23
+ copy.merge!(@yaml)
24
+ end
25
+ copy
26
+ end
27
+
28
+
29
+ # Returns an OpenStruct object representation of the config file.
30
+ # This allows you to access config settings with dot notation.
31
+ def settings
32
+ copy = OpenStruct.new
33
+ @lock.synchronize do
34
+ copy.marshal_load(@settings.marshal_dump)
35
+ end
36
+ copy
37
+ end
38
+
39
+ # The safest way to get a config setting.
40
+ # Requesting a non-exsisting key, will simply return a nil value instead of raising an error.
41
+ #
42
+ # Examples:
43
+ # Fig.get_setting('some.nested.setting')
44
+ #
45
+ # ===Params
46
+ # * *key* - A case insensivie config key
47
+ #
48
+ # *Returns* The value of the config setting requested.
49
+ # This may be the value itself or an OpenStruct containing child args
50
+ def get_setting(key)
51
+ setting = nil
52
+
53
+ @lock.synchronize do
54
+ setting = @settings
55
+ keys = key.to_s.downcase.split(/\./)
56
+
57
+ keys.each do |k|
58
+ item = eval("setting.#{k}")
59
+ return nil unless item
60
+ setting = item
61
+ end
62
+ end
63
+
64
+ setting
65
+ end
66
+
67
+ # Loads the config file and builds the internal Fig objects.
68
+ # Can be used to reload the file when changes have been made.
69
+ def load
70
+ yaml = YAML.load_file(@file_path)
71
+ yaml.each {|k, v| interpolate_setting(yaml, v)}
72
+ settings = OpenStruct.new
73
+ add_hash(settings, yaml)
74
+
75
+ @lock.synchronize do
76
+ @yaml = yaml
77
+ @settings = settings
78
+ end
79
+ rescue
80
+ puts "Failed to load file: #{@file_path}\n#{$!}"
81
+ end
82
+
83
+ private
84
+
85
+ # Invoked recursively to implicitly interpolate all settings for the passed value.
86
+ # Config values that contain the pattern /{fig:/ are implicitly interpolated,
87
+ # replacing the "fig" placeholder with the actual value from elsewhere in the config file.
88
+ #
89
+ # Example:
90
+ # name: Nathan Hopkins
91
+ # message: "This is a test! Hello #{fig:example.name}"
92
+ #
93
+ # ===Params
94
+ # * *value* [_Object_] The value to interpolate.
95
+ def interpolate_setting(yaml, value)
96
+ if value.is_a?(Hash)
97
+ value.each {|k,v| interpolate_setting(yaml, v) }
98
+ elsif value.is_a?(String)
99
+ pattern = /\{fig:/i
100
+ start = value.index(pattern, 0)
101
+ replace = {}
102
+
103
+ while start
104
+ finish = value.index(/\}/, start)
105
+ key = value[(start + 1)..(finish - 1)]
106
+ replace[key] = eval("yaml['#{key.sub(/^fig:/i, "").gsub(/\./, "']['")}'].to_s")
107
+ start = value.index(pattern, finish)
108
+ end
109
+
110
+ value.interpolate(replace, true)
111
+ end
112
+ end
113
+
114
+ # Recursively adds a hash to an OpenStruct object, ultimately creating a complete OpenStruct object with attributes
115
+ # for all key/value pairs in the Hash.
116
+ #
117
+ # ===Params
118
+ # * *obj* - The OpenStruct object to add Hash args to.
119
+ # * *hash* - The Hash to pull args from.
120
+ def add_hash(obj, hash)
121
+ return unless hash
122
+
123
+ hash.each do |key, value|
124
+ if value.class == Hash
125
+ eval "obj.#{key} = OpenStruct.new"
126
+ add_hash(eval("obj.#{key}"), value)
127
+ else
128
+ eval "obj.#{key.downcase} = value"
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+ end
data/test/fig_test.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'test/unit'
2
2
  require 'fileutils'
3
- require File.dirname(__FILE__) + '/../lib/fig'
3
+ require File.dirname(__FILE__) + '/../lib/hopsoft/fig'
4
4
 
5
5
  class FigTest < Test::Unit::TestCase
6
- @@fig = Fig.new(File.dirname(__FILE__) + '/test.yml')
6
+ @@fig = Hopsoft::Fig.new(File.dirname(__FILE__) + '/test.yml')
7
7
 
8
8
  def test_yaml_values
9
9
  assert @@fig.yaml
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hopsoft-fig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins, Hopsoft LLC
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-15 00:00:00 -07:00
12
+ date: 2009-03-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -21,7 +21,7 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - README.rdoc
24
- - lib/fig.rb
24
+ - lib/hopsoft/fig.rb
25
25
  - lib/string.rb
26
26
  - tasks/fig_tasks.rake
27
27
  files:
@@ -36,7 +36,7 @@ files:
36
36
  - uninstall.rb
37
37
  - Rakefile
38
38
  - init.rb
39
- - lib/fig.rb
39
+ - lib/hopsoft/fig.rb
40
40
  - lib/string.rb
41
41
  - tasks/fig_tasks.rake
42
42
  - fig.gemspec
data/lib/fig.rb DELETED
@@ -1,132 +0,0 @@
1
- require 'yaml'
2
- require 'ostruct'
3
- require 'thread'
4
- require File.dirname(__FILE__) + '/string'
5
-
6
- class Fig
7
-
8
- # Constructor...
9
- #
10
- # ===Params
11
- # * *file_path* - Path to the config file that should be loaded.
12
- def initialize(file_path)
13
- @lock = Mutex.new
14
- @file_path = file_path
15
- load
16
- end
17
-
18
- # Returns the config file file as a YAML Hash.
19
- def yaml
20
- copy = {}
21
- @lock.synchronize do
22
- copy.merge!(@yaml)
23
- end
24
- copy
25
- end
26
-
27
-
28
- # Returns an OpenStruct object representation of the config file.
29
- # This allows you to access config settings with dot notation.
30
- def settings
31
- copy = OpenStruct.new
32
- @lock.synchronize do
33
- copy.marshal_load(@settings.marshal_dump)
34
- end
35
- copy
36
- end
37
-
38
- # The safest way to get a config setting.
39
- # Requesting a non-exsisting key, will simply return a nil value instead of raising an error.
40
- #
41
- # Examples:
42
- # Fig.get_setting('some.nested.setting')
43
- #
44
- # ===Params
45
- # * *key* - A case insensivie config key
46
- #
47
- # *Returns* The value of the config setting requested.
48
- # This may be the value itself or an OpenStruct containing child args
49
- def get_setting(key)
50
- setting = nil
51
-
52
- @lock.synchronize do
53
- setting = @settings
54
- keys = key.to_s.downcase.split(/\./)
55
-
56
- keys.each do |k|
57
- item = eval("setting.#{k}")
58
- return nil unless item
59
- setting = item
60
- end
61
- end
62
-
63
- setting
64
- end
65
-
66
- # Loads the config file and builds the internal Fig objects.
67
- # Can be used to reload the file when changes have been made.
68
- def load
69
- yaml = YAML.load_file(@file_path)
70
- yaml.each {|k, v| interpolate_setting(yaml, v)}
71
- settings = OpenStruct.new
72
- add_hash(settings, yaml)
73
-
74
- @lock.synchronize do
75
- @yaml = yaml
76
- @settings = settings
77
- end
78
- rescue
79
- puts "Failed to load file: #{@file_path}\n#{$!}"
80
- end
81
-
82
- private
83
-
84
- # Invoked recursively to implicitly interpolate all settings for the passed value.
85
- # Config values that contain the pattern /{fig:/ are implicitly interpolated,
86
- # replacing the "fig" placeholder with the actual value from elsewhere in the config file.
87
- #
88
- # Example:
89
- # name: Nathan Hopkins
90
- # message: "This is a test! Hello #{fig:example.name}"
91
- #
92
- # ===Params
93
- # * *value* [_Object_] The value to interpolate.
94
- def interpolate_setting(yaml, value)
95
- if value.is_a?(Hash)
96
- value.each {|k,v| interpolate_setting(yaml, v) }
97
- elsif value.is_a?(String)
98
- pattern = /\{fig:/i
99
- start = value.index(pattern, 0)
100
- replace = {}
101
-
102
- while start
103
- finish = value.index(/\}/, start)
104
- key = value[(start + 1)..(finish - 1)]
105
- replace[key] = eval("yaml['#{key.sub(/^fig:/i, "").gsub(/\./, "']['")}'].to_s")
106
- start = value.index(pattern, finish)
107
- end
108
-
109
- value.interpolate(replace, true)
110
- end
111
- end
112
-
113
- # Recursively adds a hash to an OpenStruct object, ultimately creating a complete OpenStruct object with attributes
114
- # for all key/value pairs in the Hash.
115
- #
116
- # ===Params
117
- # * *obj* - The OpenStruct object to add Hash args to.
118
- # * *hash* - The Hash to pull args from.
119
- def add_hash(obj, hash)
120
- return unless hash
121
-
122
- hash.each do |key, value|
123
- if value.class == Hash
124
- eval "obj.#{key} = OpenStruct.new"
125
- add_hash(eval("obj.#{key}"), value)
126
- else
127
- eval "obj.#{key.downcase} = value"
128
- end
129
- end
130
- end
131
-
132
- end