hopsoft-fig 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +10 -7
- data/Rakefile +1 -1
- data/fig.gemspec +5 -5
- data/init.rb +1 -1
- data/lib/hopsoft/fig.rb +18 -20
- data/lib/string.rb +12 -11
- metadata +14 -14
data/README.rdoc
CHANGED
@@ -2,12 +2,14 @@
|
|
2
2
|
|
3
3
|
===DRY up those magic numbers and hard coded strings into something more managable. Fig is the smart way to manage stuff that really belongs in a config file instead of scattered throughout your code.
|
4
4
|
|
5
|
+
Here are the Rdocs[http://hopsoft.github.com/fig/README.rdoc.html]
|
6
|
+
Video tutorial coming soon...
|
7
|
+
|
5
8
|
==Why
|
6
9
|
|
7
|
-
|
8
|
-
it's still good practice to have a centralized place to hold certain settings.
|
10
|
+
Even though Ruby is a dynamic language and hard coded values aren't as big a deal, it's still good practice to have a centralized place to hold certain settings.
|
9
11
|
|
10
|
-
I
|
12
|
+
In the past I would use a globally available Hash to store this stuff, but have always wanted something more powerful and elegant.
|
11
13
|
|
12
14
|
|
13
15
|
==What
|
@@ -48,28 +50,29 @@ Second, install the Gem.
|
|
48
50
|
===Usage
|
49
51
|
Create a YAML file that will serve as one of the configuration files you plan to use. In a Rails application, I usually create the file config/app.yml, but you can name the file anything you like and can save it to any location within your appliation or library.
|
50
52
|
|
51
|
-
Require Fig either explicitly or implicitly. In a Rails application, I generally do this in environment.rb.
|
53
|
+
Require Fig either explicitly or implicitly. In a Rails application, I generally do this in environment.rb. Note: this step isn't required if you installed as a Rails plugin.
|
52
54
|
# implicit
|
53
55
|
require 'hopsoft/fig'
|
54
56
|
# explicit
|
55
57
|
gem 'hopsoft-fig'
|
56
58
|
|
57
59
|
Instantiate a Fig object that is globally available to your application. In a Rails application, I generally do this in environment.rb.
|
58
|
-
APP_CONFIG = Fig.new(RAILS_ROOT + '/config/app.yml')
|
60
|
+
APP_CONFIG = Hopsoft::Fig.new(RAILS_ROOT + '/config/app.yml')
|
59
61
|
|
60
62
|
Start using your settings.
|
61
63
|
puts APP_CONFIG.settings.message
|
62
64
|
puts APP_CONFIG.yaml['message']
|
63
65
|
puts APP_CONFIG.get_setting('message')
|
66
|
+
|
64
67
|
# returns nil instead of an error when the setting doesn't exist
|
65
68
|
puts APP_CONFIG.get_setting('some.nested.setting.that.may.not.exist')
|
66
69
|
|
67
70
|
Reuse settings in your YAML file (This is a great way to apply the DRY principle to your configuration settings):
|
68
71
|
name: Nathan Hopkins
|
69
|
-
message: {fig:name}
|
72
|
+
message: Hello from {fig:name}.
|
70
73
|
|
71
74
|
puts APP_CONFIG.get_setting('message')
|
72
|
-
# outputs -> Nathan Hopkins
|
75
|
+
# outputs -> Hello from Nathan Hopkins.
|
73
76
|
|
74
77
|
Update the YAML file and load the changes without restarting your application.
|
75
78
|
APP_CONFIG.load
|
data/Rakefile
CHANGED
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.
|
5
|
+
s.version = "0.8.5"
|
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-
|
9
|
+
s.date = %q{2009-03-18}
|
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 = ["
|
13
|
-
s.files = ["
|
12
|
+
s.extra_rdoc_files = ["lib/hopsoft/fig.rb", "lib/string.rb", "README.rdoc", "tasks/fig_tasks.rake"]
|
13
|
+
s.files = ["fig.gemspec", "init.rb", "install.rb", "lib/hopsoft/fig.rb", "lib/string.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.rdoc", "tasks/fig_tasks.rake", "test/fig_test.rb", "test/string_test.rb", "test/test.yml", "test/test2.yml", "uninstall.rb"]
|
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"]
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.rubyforge_project = %q{fig}
|
19
19
|
s.rubygems_version = %q{1.3.1}
|
20
20
|
s.summary = %q{The smart way to manage configuration settings for your Ruby applications.}
|
21
|
-
s.test_files = ["test/
|
21
|
+
s.test_files = ["test/fig_test.rb", "test/string_test.rb"]
|
22
22
|
|
23
23
|
if s.respond_to? :specification_version then
|
24
24
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/init.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'string'
|
2
|
-
require 'fig'
|
2
|
+
require 'hopsoft/fig'
|
data/lib/hopsoft/fig.rb
CHANGED
@@ -8,8 +8,8 @@ module Hopsoft
|
|
8
8
|
|
9
9
|
# Constructor...
|
10
10
|
#
|
11
|
-
#
|
12
|
-
# *
|
11
|
+
# ====Params
|
12
|
+
# * +file_path+ - Path to the config file that should be loaded.
|
13
13
|
def initialize(file_path)
|
14
14
|
@lock = Mutex.new
|
15
15
|
@file_path = file_path
|
@@ -27,7 +27,7 @@ module Hopsoft
|
|
27
27
|
|
28
28
|
|
29
29
|
# Returns an OpenStruct object representation of the config file.
|
30
|
-
# This allows
|
30
|
+
# This allows access to config settings via dot notation.
|
31
31
|
def settings
|
32
32
|
copy = OpenStruct.new
|
33
33
|
@lock.synchronize do
|
@@ -39,14 +39,12 @@ module Hopsoft
|
|
39
39
|
# The safest way to get a config setting.
|
40
40
|
# Requesting a non-exsisting key, will simply return a nil value instead of raising an error.
|
41
41
|
#
|
42
|
-
#
|
43
|
-
# Fig.get_setting('some.nested.setting')
|
42
|
+
# Fig.get_setting('some.nested.setting')
|
44
43
|
#
|
45
|
-
#
|
46
|
-
# *
|
44
|
+
# ====Params
|
45
|
+
# * +key+ - A case insensivie config key.
|
47
46
|
#
|
48
|
-
#
|
49
|
-
# This may be the value itself or an OpenStruct containing child args
|
47
|
+
# Returns the value of the config setting requested. This may be the value itself or an OpenStruct containing child args.
|
50
48
|
def get_setting(key)
|
51
49
|
setting = nil
|
52
50
|
|
@@ -55,7 +53,7 @@ module Hopsoft
|
|
55
53
|
keys = key.to_s.downcase.split(/\./)
|
56
54
|
|
57
55
|
keys.each do |k|
|
58
|
-
item = eval("setting.#{k}")
|
56
|
+
item = eval("setting.#{k}") if setting.is_a?(OpenStruct)
|
59
57
|
return nil unless item
|
60
58
|
setting = item
|
61
59
|
end
|
@@ -82,16 +80,16 @@ module Hopsoft
|
|
82
80
|
|
83
81
|
private
|
84
82
|
|
85
|
-
#
|
86
|
-
# Config values that contain the pattern /{fig:/ are
|
87
|
-
#
|
83
|
+
# Interpolates all settings for the passed value.
|
84
|
+
# Config values that contain the pattern /{fig:/ are interpolated replacing the "fig"
|
85
|
+
# placeholder with the actual value from elsewhere in the config file.
|
88
86
|
#
|
89
87
|
# Example:
|
90
88
|
# name: Nathan Hopkins
|
91
|
-
# message: "This is a test! Hello #{fig:example.name}"
|
89
|
+
# message: "This is a test! Hello #{fig:example.name}."
|
92
90
|
#
|
93
|
-
#
|
94
|
-
# *
|
91
|
+
# ====Params
|
92
|
+
# * +value+ - The value to interpolate.
|
95
93
|
def interpolate_setting(yaml, value)
|
96
94
|
if value.is_a?(Hash)
|
97
95
|
value.each {|k,v| interpolate_setting(yaml, v) }
|
@@ -114,9 +112,9 @@ module Hopsoft
|
|
114
112
|
# Recursively adds a hash to an OpenStruct object, ultimately creating a complete OpenStruct object with attributes
|
115
113
|
# for all key/value pairs in the Hash.
|
116
114
|
#
|
117
|
-
#
|
118
|
-
# *
|
119
|
-
# *
|
115
|
+
# ====Params
|
116
|
+
# * +obj+ - The OpenStruct object to add Hash args to.
|
117
|
+
# * +hash+ - The Hash to pull args from.
|
120
118
|
def add_hash(obj, hash)
|
121
119
|
return unless hash
|
122
120
|
|
@@ -131,4 +129,4 @@ module Hopsoft
|
|
131
129
|
end
|
132
130
|
|
133
131
|
end
|
134
|
-
end
|
132
|
+
end
|
data/lib/string.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
class String
|
2
2
|
|
3
3
|
# Allows various forms of string interpolation.
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
4
|
+
# Originally designed to reuse YAML config values; howerver,
|
5
|
+
# this will likely come in handy elsewhere.
|
6
|
+
#
|
7
|
+
#
|
8
8
|
# "Hello! My name is ?".interpolate("Nathan Hopkins")
|
9
9
|
# "Hello! My first name is ? and my last name is ?".interpolate(["Nathan", "Hopkins"])
|
10
|
-
# 'Hello! My first name is {first_name} and my last name is {last_name}'.interpolate(:first_name => "Nathan",
|
10
|
+
# 'Hello! My first name is {first_name} and my last name is {last_name}'.interpolate(:first_name => "Nathan",
|
11
|
+
# :last_name => "Hopkins")
|
12
|
+
#
|
11
13
|
#
|
12
|
-
#
|
13
|
-
# *
|
14
|
-
# *
|
15
|
-
#
|
14
|
+
# ====Params
|
15
|
+
# * +args+ - The value(s) used to replace segments of the string.
|
16
|
+
# * +in_place+ - Indicates if the value should edited in place. Be careful when doing this, you may end up with unexpected results!
|
17
|
+
#
|
16
18
|
#
|
17
|
-
#
|
18
|
-
# The new string after interpolation.
|
19
|
+
# Returns the new string after interpolation.
|
19
20
|
def interpolate(args, in_place=false)
|
20
21
|
args = [args] unless args.is_a?(Array) || args.is_a?(Hash)
|
21
22
|
|
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.
|
4
|
+
version: 0.8.5
|
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-
|
12
|
+
date: 2009-03-18 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -20,26 +20,26 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
-
- README.rdoc
|
24
23
|
- lib/hopsoft/fig.rb
|
25
24
|
- lib/string.rb
|
25
|
+
- README.rdoc
|
26
26
|
- tasks/fig_tasks.rake
|
27
27
|
files:
|
28
|
-
-
|
28
|
+
- fig.gemspec
|
29
|
+
- init.rb
|
29
30
|
- install.rb
|
31
|
+
- lib/hopsoft/fig.rb
|
32
|
+
- lib/string.rb
|
33
|
+
- Manifest
|
30
34
|
- MIT-LICENSE
|
35
|
+
- Rakefile
|
36
|
+
- README.rdoc
|
37
|
+
- tasks/fig_tasks.rake
|
38
|
+
- test/fig_test.rb
|
39
|
+
- test/string_test.rb
|
31
40
|
- test/test.yml
|
32
41
|
- test/test2.yml
|
33
|
-
- test/string_test.rb
|
34
|
-
- test/fig_test.rb
|
35
|
-
- Manifest
|
36
42
|
- uninstall.rb
|
37
|
-
- Rakefile
|
38
|
-
- init.rb
|
39
|
-
- lib/hopsoft/fig.rb
|
40
|
-
- lib/string.rb
|
41
|
-
- tasks/fig_tasks.rake
|
42
|
-
- fig.gemspec
|
43
43
|
has_rdoc: true
|
44
44
|
homepage: http://github.com/hopsoft/fig
|
45
45
|
post_install_message:
|
@@ -72,5 +72,5 @@ signing_key:
|
|
72
72
|
specification_version: 2
|
73
73
|
summary: The smart way to manage configuration settings for your Ruby applications.
|
74
74
|
test_files:
|
75
|
-
- test/string_test.rb
|
76
75
|
- test/fig_test.rb
|
76
|
+
- test/string_test.rb
|