figleaf 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +0 -1
- data/.travis.yml +10 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +48 -0
- data/README.md +9 -7
- data/figleaf.gemspec +2 -1
- data/lib/figleaf/configuration.rb +54 -0
- data/lib/figleaf/fighash.rb +7 -0
- data/lib/figleaf/load_settings.rb +40 -0
- data/lib/figleaf/settings.rb +2 -74
- data/lib/figleaf/version.rb +1 -1
- data/lib/figleaf.rb +9 -0
- data/spec/settings_spec.rb +68 -6
- metadata +45 -2
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/intridea/hashie.git
|
3
|
+
revision: 8dd0fec950c33373c5c954d7ac72df4724334b12
|
4
|
+
specs:
|
5
|
+
hashie (2.0.0.beta)
|
6
|
+
|
7
|
+
PATH
|
8
|
+
remote: .
|
9
|
+
specs:
|
10
|
+
figleaf (0.0.2)
|
11
|
+
activesupport
|
12
|
+
hashie
|
13
|
+
|
14
|
+
GEM
|
15
|
+
remote: http://rubygems.org/
|
16
|
+
specs:
|
17
|
+
activesupport (3.2.8)
|
18
|
+
i18n (~> 0.6)
|
19
|
+
multi_json (~> 1.0)
|
20
|
+
coderay (1.0.7)
|
21
|
+
diff-lcs (1.1.3)
|
22
|
+
i18n (0.6.0)
|
23
|
+
method_source (0.8)
|
24
|
+
multi_json (1.3.6)
|
25
|
+
pry (0.9.10)
|
26
|
+
coderay (~> 1.0.5)
|
27
|
+
method_source (~> 0.8)
|
28
|
+
slop (~> 3.3.1)
|
29
|
+
rake (0.9.2.2)
|
30
|
+
rspec (2.11.0)
|
31
|
+
rspec-core (~> 2.11.0)
|
32
|
+
rspec-expectations (~> 2.11.0)
|
33
|
+
rspec-mocks (~> 2.11.0)
|
34
|
+
rspec-core (2.11.1)
|
35
|
+
rspec-expectations (2.11.2)
|
36
|
+
diff-lcs (~> 1.1.3)
|
37
|
+
rspec-mocks (2.11.2)
|
38
|
+
slop (3.3.2)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
figleaf!
|
45
|
+
hashie!
|
46
|
+
pry
|
47
|
+
rake
|
48
|
+
rspec
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# Figleaf
|
2
|
+
[](http://travis-ci.org/challengepost/figleaf)
|
3
|
+
|
2
4
|
|
3
5
|
YAML based DRY settings manager.
|
4
6
|
|
@@ -10,7 +12,7 @@ and include in our applications.
|
|
10
12
|
|
11
13
|
Add this line to your application's Gemfile:
|
12
14
|
|
13
|
-
gem '
|
15
|
+
gem 'figleaf'
|
14
16
|
|
15
17
|
And then execute:
|
16
18
|
|
@@ -18,14 +20,14 @@ And then execute:
|
|
18
20
|
|
19
21
|
Or install it yourself as:
|
20
22
|
|
21
|
-
$ gem install
|
23
|
+
$ gem install figleaf
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
27
|
In `application.rb`:
|
26
28
|
|
27
29
|
```
|
28
|
-
Settings.configure_with_auto_define do |s|
|
30
|
+
Figleaf::Settings.configure_with_auto_define do |s|
|
29
31
|
s.env = Rails.env
|
30
32
|
s.some_awesome_flag = true
|
31
33
|
s.load_settings
|
@@ -36,7 +38,7 @@ files.
|
|
36
38
|
|
37
39
|
eg: In `production.rb`
|
38
40
|
```
|
39
|
-
Settings.configure do |s|
|
41
|
+
Figleaf::Settings.configure do |s|
|
40
42
|
s.some_awesome_flag = false
|
41
43
|
end
|
42
44
|
```
|
@@ -57,11 +59,11 @@ production:
|
|
57
59
|
foo: foo
|
58
60
|
```
|
59
61
|
|
60
|
-
The Settings parser will create a namespace for your YAML file after the file
|
62
|
+
The Figleaf::Settings parser will create a namespace for your YAML file after the file
|
61
63
|
name.
|
62
64
|
|
63
65
|
Then, assuming that you named your YAML file `mysetting.yml`. you can just
|
64
|
-
access `foo` as `Settings.mysetting["foo"]`. (Inspired by Rails' `database.yml`,
|
66
|
+
access `foo` as `Figleaf::Settings.mysetting["foo"]`. (Inspired by Rails' `database.yml`,
|
65
67
|
of course.)
|
66
68
|
|
67
69
|
## Contributing
|
data/figleaf.gemspec
CHANGED
@@ -15,9 +15,10 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Figleaf::VERSION
|
17
17
|
|
18
|
-
%w(rake rspec).each do |g|
|
18
|
+
%w(rake rspec pry).each do |g|
|
19
19
|
gem.add_development_dependency(g)
|
20
20
|
end
|
21
21
|
|
22
22
|
gem.add_dependency("activesupport")
|
23
|
+
gem.add_dependency("hashie")
|
23
24
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Figleaf
|
2
|
+
module Configuration
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :auto_define
|
7
|
+
self.auto_define = false
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def configure_with_auto_define
|
12
|
+
self.auto_define.tap do |cached_auto_define|
|
13
|
+
self.auto_define = true
|
14
|
+
yield self
|
15
|
+
self.auto_define = cached_auto_define
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def configure
|
20
|
+
self.auto_define.tap do |cached_auto_define|
|
21
|
+
self.auto_define = false
|
22
|
+
yield self
|
23
|
+
self.auto_define = cached_auto_define
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def override_with_local!(local_file)
|
28
|
+
#this file (i.e. test.local.rb) is an optional place to put settings
|
29
|
+
local_file.tap do |local_settings_path|
|
30
|
+
eval(IO.read(local_settings_path), binding) if File.exists?(local_settings_path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(method_name, *args)
|
35
|
+
if self.auto_define && method_name.to_s =~ /=$/ && args.length == 1
|
36
|
+
self.define_cattr_methods(method_name)
|
37
|
+
self.send(method_name, args.shift)
|
38
|
+
else
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def define_cattr_methods(setter_name)
|
44
|
+
getter_name = setter_name.to_s.gsub('=','')
|
45
|
+
|
46
|
+
cattr_writer getter_name
|
47
|
+
define_singleton_method(getter_name) do
|
48
|
+
result = class_variable_get "@@#{getter_name}"
|
49
|
+
result.respond_to?(:call) ? result.call : result
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'pry'
|
2
|
+
module Figleaf
|
3
|
+
module LoadSettings
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Load all files in config/settings and set their contents as first level
|
8
|
+
# citizen of Settings:
|
9
|
+
# config/settings/some_service.yml contains
|
10
|
+
# production:
|
11
|
+
# foo: bar
|
12
|
+
# --> Settings.some_service.foo = bar
|
13
|
+
def load_settings
|
14
|
+
Dir.glob(root.join('config', 'settings', '*.yml')).each do |file|
|
15
|
+
property_name = File.basename(file, '.yml')
|
16
|
+
property = YAML.load_file(file)[env]
|
17
|
+
property = use_hashie_if_hash(property)
|
18
|
+
self.configure_with_auto_define do |s|
|
19
|
+
s.send("#{property_name}=", property)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def root
|
25
|
+
return Rails.root if defined?(Rails)
|
26
|
+
Pathname.new('.')
|
27
|
+
end
|
28
|
+
|
29
|
+
def env
|
30
|
+
return Rails.env if defined?(Rails)
|
31
|
+
ENV['ENVIRONMENT']
|
32
|
+
end
|
33
|
+
|
34
|
+
def use_hashie_if_hash(property)
|
35
|
+
return Figleaf::Fighash.new(property) if property.is_a?(Hash)
|
36
|
+
property
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/figleaf/settings.rb
CHANGED
@@ -1,78 +1,6 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'active_support/core_ext/class'
|
3
|
-
require 'pathname'
|
4
|
-
|
5
1
|
module Figleaf
|
6
2
|
class Settings
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def self.configure_with_auto_define
|
11
|
-
self.auto_define.tap do |cached_auto_define|
|
12
|
-
self.auto_define = true
|
13
|
-
yield self
|
14
|
-
self.auto_define = cached_auto_define
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.configure
|
19
|
-
self.auto_define.tap do |cached_auto_define|
|
20
|
-
self.auto_define = false
|
21
|
-
yield self
|
22
|
-
self.auto_define = cached_auto_define
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.override_with_local!(local_file)
|
27
|
-
#this file (i.e. test.local.rb) is an optional place to put settings
|
28
|
-
local_file.tap do |local_settings_path|
|
29
|
-
eval(IO.read(local_settings_path), binding) if File.exists?(local_settings_path)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.method_missing(method_name, *args)
|
34
|
-
if self.auto_define && method_name.to_s =~ /=$/ && args.length == 1
|
35
|
-
self.define_cattr_methods(method_name)
|
36
|
-
self.send(method_name, args.shift)
|
37
|
-
else
|
38
|
-
super
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.define_cattr_methods(setter_name)
|
43
|
-
getter_name = setter_name.to_s.gsub('=','')
|
44
|
-
|
45
|
-
cattr_writer getter_name
|
46
|
-
define_singleton_method(getter_name) do
|
47
|
-
result = class_variable_get "@@#{getter_name}"
|
48
|
-
result.respond_to?(:call) ? result.call : result
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# Load all files in config/settings and set their contents as first level
|
53
|
-
# citizen of Settings:
|
54
|
-
# config/settings/some_service.yml contains
|
55
|
-
# production:
|
56
|
-
# foo: bar
|
57
|
-
# --> Settings.some_service.foo = bar
|
58
|
-
def self.load_settings
|
59
|
-
Dir.glob(root.join('config', 'settings', '*.yml')).each do |file|
|
60
|
-
property_name = File.basename(file, '.yml')
|
61
|
-
property = YAML.load_file(file)[env]
|
62
|
-
configure_with_auto_define do |s|
|
63
|
-
s.send("#{property_name}=", property)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.root
|
69
|
-
return Rails.root if defined?(Rails)
|
70
|
-
Pathname.new('.')
|
71
|
-
end
|
72
|
-
|
73
|
-
def self.env
|
74
|
-
return Rails.env if defined?(Rails)
|
75
|
-
ENV['ENVIRONMENT']
|
76
|
-
end
|
3
|
+
include Figleaf::Configuration
|
4
|
+
include Figleaf::LoadSettings
|
77
5
|
end
|
78
6
|
end
|
data/lib/figleaf/version.rb
CHANGED
data/lib/figleaf.rb
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/class'
|
3
|
+
require 'hashie'
|
4
|
+
require 'pathname'
|
5
|
+
require 'yaml'
|
6
|
+
|
1
7
|
module Figleaf
|
8
|
+
autoload :Fighash, 'figleaf/fighash'
|
9
|
+
autoload :Configuration, 'figleaf/configuration'
|
10
|
+
autoload :LoadSettings, 'figleaf/load_settings'
|
2
11
|
autoload :Settings, 'figleaf/settings'
|
3
12
|
autoload :VERSION, 'figleaf/version'
|
4
13
|
end
|
data/spec/settings_spec.rb
CHANGED
@@ -79,14 +79,76 @@ describe Figleaf::Settings do
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
describe "self.
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
82
|
+
describe "self.load_settings" do
|
83
|
+
let(:configuration) {
|
84
|
+
{
|
85
|
+
"test" => {
|
86
|
+
"foo" => "bar",
|
87
|
+
"bool_true" => true,
|
88
|
+
"bool_false" => false
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
before do
|
94
|
+
Dir.stub(:glob).and_return(["config/described_class/some_service.yml"])
|
95
|
+
described_class.stub(:env).and_return("test")
|
96
|
+
YAML.stub(:load_file).and_return(configuration)
|
97
|
+
|
98
|
+
described_class.load_settings
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should load some service" do
|
102
|
+
described_class.some_service["foo"].should == "bar"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should load indifferently the key names" do
|
106
|
+
described_class.some_service["foo"].should == "bar"
|
107
|
+
described_class.some_service[:foo].should == "bar"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should create foo as a method" do
|
111
|
+
described_class.some_service.foo.should == "bar"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should create bool_true? and return true" do
|
115
|
+
described_class.some_service.bool_true?.should be_true
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should create bool_false? and return false" do
|
119
|
+
described_class.some_service.bool_false?.should be_false
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should work for arrays as well" do
|
123
|
+
YAML.stub(:load_file).and_return({ "test" => [1, 2] })
|
124
|
+
described_class.load_settings
|
125
|
+
described_class.some_service.should == [1, 2]
|
126
|
+
end
|
127
|
+
|
128
|
+
it "and for plain strings" do
|
129
|
+
YAML.stub(:load_file).and_return({ "test" => "Hello, World!" })
|
130
|
+
described_class.load_settings
|
131
|
+
described_class.some_service.should == "Hello, World!"
|
132
|
+
end
|
133
|
+
|
134
|
+
it "and for booleans (true)" do
|
135
|
+
YAML.stub(:load_file).and_return({ "test" => true })
|
87
136
|
described_class.load_settings
|
88
|
-
described_class.some_service.should
|
137
|
+
described_class.some_service.should be_true
|
89
138
|
end
|
139
|
+
|
140
|
+
it "and for booleans (false)" do
|
141
|
+
YAML.stub(:load_file).and_return({ "test" => false })
|
142
|
+
described_class.load_settings
|
143
|
+
described_class.some_service.should be_false
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should raise exception when loading an undefined value" do
|
147
|
+
YAML.stub(:load_file).and_return({ "test" => {} })
|
148
|
+
described_class.load_settings
|
149
|
+
expect { described_class.some_service.blah }.to raise_error NoMethodError
|
150
|
+
end
|
151
|
+
|
90
152
|
end
|
91
153
|
|
92
154
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: figleaf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
63
|
name: activesupport
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,6 +75,22 @@ dependencies:
|
|
59
75
|
- - ! '>='
|
60
76
|
- !ruby/object:Gem::Version
|
61
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: hashie
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
62
94
|
description: YAML based DRY settings manager.
|
63
95
|
email:
|
64
96
|
- jcmuller@gmail.com
|
@@ -68,12 +100,17 @@ extra_rdoc_files: []
|
|
68
100
|
files:
|
69
101
|
- .gitignore
|
70
102
|
- .rspec
|
103
|
+
- .travis.yml
|
71
104
|
- Gemfile
|
105
|
+
- Gemfile.lock
|
72
106
|
- LICENSE
|
73
107
|
- README.md
|
74
108
|
- Rakefile
|
75
109
|
- figleaf.gemspec
|
76
110
|
- lib/figleaf.rb
|
111
|
+
- lib/figleaf/configuration.rb
|
112
|
+
- lib/figleaf/fighash.rb
|
113
|
+
- lib/figleaf/load_settings.rb
|
77
114
|
- lib/figleaf/settings.rb
|
78
115
|
- lib/figleaf/version.rb
|
79
116
|
- spec/settings_spec.rb
|
@@ -90,12 +127,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
127
|
- - ! '>='
|
91
128
|
- !ruby/object:Gem::Version
|
92
129
|
version: '0'
|
130
|
+
segments:
|
131
|
+
- 0
|
132
|
+
hash: -1491263342896218815
|
93
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
134
|
none: false
|
95
135
|
requirements:
|
96
136
|
- - ! '>='
|
97
137
|
- !ruby/object:Gem::Version
|
98
138
|
version: '0'
|
139
|
+
segments:
|
140
|
+
- 0
|
141
|
+
hash: -1491263342896218815
|
99
142
|
requirements: []
|
100
143
|
rubyforge_project:
|
101
144
|
rubygems_version: 1.8.24
|