figleaf 0.0.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c34ecefd305356a76cbdc55e7c13fdb00a6f2f65
4
- data.tar.gz: bef872b89c5847b5d64110b6a80de5dc56263d83
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 320f3bccb7dce596f1772493f1e6523c062120f8
4
+ data.tar.gz: d3095e511f2683a295ab14433c1b9f8e84a72dbf
5
5
  SHA512:
6
- metadata.gz: 8e14ebdc3cd747d3dca531cf1d200e8d8917a248428e03e3fad267fa6dd6fb58cdabb2ae3783363f9abff48a4f653ec0b6f06af4eb5fa8c8644a1aa4c319800e
7
- data.tar.gz: 4792942855f09111ae1a08f6d46a3245ee68791ef99ef167e8b97964f09f8112a8cd225c1a38cbf75b0454d40625f7c695f53f34c23daad96c5b0f8836e2f5e2
6
+ metadata.gz: d74d217c8b2d6aafec66abd81a398e7ed9c9930178bd4d7a5194cffdc312faa310286507014a5279f2c49897f63758b9e76c739df4cda563d69abe66c2b07b19
7
+ data.tar.gz: af3478e94d692e3cdccad4cebd76b479a51333da96c86cc0e1c20dcce2982f1f90af620ecab415acc5c5d4e521ac1ed256e75052baa34ca1b1e375b06a8c5331
data/.travis.yml CHANGED
@@ -4,6 +4,7 @@ bundler_args: --without development
4
4
  rvm:
5
5
  - 1.9.3
6
6
  - 2.0.0
7
+ - 2.1.6
7
8
  notifications:
8
9
  email:
9
10
  - tech@challengepost.com
data/Gemfile CHANGED
@@ -2,10 +2,20 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
+ # This are needed here by travis-ci
6
+ group :test do
7
+ gem "rake"
8
+ gem "rspec"
9
+ end
10
+
5
11
  group :development do
6
12
  gem "rb-fchange", require: false
7
13
  gem "rb-fsevent", require: false
8
14
  gem "rb-inotify", require: false
9
15
  gem "ruby_gntp"
10
16
  gem "terminal-notifier-guard"
17
+ gem "guard-bundler"
18
+ gem "guard-rspec"
19
+ gem "pry"
20
+ gem "pry-debugger", platforms: :ruby_19
11
21
  end
data/Gemfile.lock CHANGED
@@ -1,20 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- figleaf (0.0.7)
5
- activesupport
6
- hashie
4
+ figleaf (0.2.0)
5
+ activesupport (>= 4)
6
+ hashie (>= 2)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activesupport (4.0.0)
12
- i18n (~> 0.6, >= 0.6.4)
13
- minitest (~> 4.2)
14
- multi_json (~> 1.3)
15
- thread_safe (~> 0.1)
16
- tzinfo (~> 0.3.37)
17
- atomic (1.1.14)
11
+ activesupport (4.2.4)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
18
17
  coderay (1.0.9)
19
18
  columnize (0.3.6)
20
19
  debugger (1.6.2)
@@ -26,8 +25,6 @@ GEM
26
25
  diff-lcs (1.2.4)
27
26
  ffi (1.9.0)
28
27
  formatador (0.2.4)
29
- graphviz (0.0.2)
30
- rexec
31
28
  guard (1.8.3)
32
29
  formatador (>= 0.2.4)
33
30
  listen (~> 1.3)
@@ -40,16 +37,16 @@ GEM
40
37
  guard-rspec (3.1.0)
41
38
  guard (>= 1.8)
42
39
  rspec (~> 2.13)
43
- hashie (2.0.5)
44
- i18n (0.6.5)
40
+ hashie (3.4.2)
41
+ i18n (0.7.0)
42
+ json (1.8.3)
45
43
  listen (1.3.1)
46
44
  rb-fsevent (>= 0.9.3)
47
45
  rb-inotify (>= 0.9)
48
46
  rb-kqueue (>= 0.2)
49
47
  lumberjack (1.0.4)
50
48
  method_source (0.8.2)
51
- minitest (4.7.5)
52
- multi_json (1.8.2)
49
+ minitest (5.8.2)
53
50
  pry (0.9.12.2)
54
51
  coderay (~> 1.0.5)
55
52
  method_source (~> 0.8)
@@ -57,7 +54,6 @@ GEM
57
54
  pry-debugger (0.2.2)
58
55
  debugger (~> 1.3)
59
56
  pry (~> 0.9.10)
60
- rainbow (1.1.4)
61
57
  rake (10.1.0)
62
58
  rb-fchange (0.0.6)
63
59
  ffi
@@ -66,8 +62,6 @@ GEM
66
62
  ffi (>= 0.5.0)
67
63
  rb-kqueue (0.2.0)
68
64
  ffi (>= 0.5.0)
69
- rexec (1.6.0)
70
- rainbow
71
65
  rspec (2.14.1)
72
66
  rspec-core (~> 2.14.0)
73
67
  rspec-expectations (~> 2.14.0)
@@ -80,19 +74,18 @@ GEM
80
74
  slop (3.4.6)
81
75
  terminal-notifier-guard (1.5.3)
82
76
  thor (0.18.1)
83
- thread_safe (0.1.3)
84
- atomic
85
- tzinfo (0.3.38)
77
+ thread_safe (0.3.5)
78
+ tzinfo (1.2.2)
79
+ thread_safe (~> 0.1)
86
80
 
87
81
  PLATFORMS
88
82
  ruby
89
83
 
90
84
  DEPENDENCIES
91
85
  figleaf!
92
- graphviz
93
- guard
94
86
  guard-bundler
95
87
  guard-rspec
88
+ pry
96
89
  pry-debugger
97
90
  rake
98
91
  rb-fchange
data/HISTORY.md ADDED
@@ -0,0 +1,2 @@
1
+ 0.2.0 - 2015-11-02
2
+ * Add the ability to evaluate ERB in YAML settings files
data/figleaf.gemspec CHANGED
@@ -15,17 +15,6 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Figleaf::VERSION
17
17
 
18
- %w(
19
- rake
20
- rspec
21
- pry-debugger
22
- guard
23
- guard-bundler
24
- guard-rspec
25
- ).each do |g|
26
- gem.add_development_dependency(g)
27
- end
28
-
29
- gem.add_dependency("activesupport")
30
- gem.add_dependency("hashie")
18
+ gem.add_dependency "activesupport", ">= 4"
19
+ gem.add_dependency "hashie", ">= 2"
31
20
  end
@@ -1,6 +1,119 @@
1
1
  module Figleaf
2
2
  class Settings
3
- include Figleaf::Configuration
4
- include Figleaf::LoadSettings
3
+ class << self
4
+ class_attribute :auto_define
5
+ self.auto_define = false
6
+
7
+ # Public - configure pre-defined attributes
8
+ #
9
+ def configure
10
+ self.auto_define.tap do |cached_auto_define|
11
+ self.auto_define = false
12
+ yield self
13
+ self.auto_define = cached_auto_define
14
+ end
15
+ end
16
+
17
+ # Public - configure auto defined settings attributes
18
+ # and load yaml settings from confing/settings directory
19
+ #
20
+ def configure!(&block)
21
+ load_settings
22
+ configure_with_auto_define(&block)
23
+ end
24
+
25
+ def configure_with_auto_define
26
+ self.auto_define.tap do |cached_auto_define|
27
+ self.auto_define = true
28
+ yield self
29
+ self.auto_define = cached_auto_define
30
+ end
31
+ end
32
+
33
+ # Load all files in config/settings and set their contents as first level
34
+ # citizen of Settings:
35
+ # config/settings/some_service.yml contains
36
+ # production:
37
+ # foo: bar
38
+ # --> Settings.some_service.foo = bar
39
+ def load_settings(file_pattern = default_file_pattern, env_to_load = env)
40
+ configure_with_auto_define do
41
+ Dir.glob(file_pattern).each do |file|
42
+ property_name = File.basename(file, '.yml')
43
+ yaml_hash = load_file(file) or next
44
+ property = yaml_hash[env_to_load]
45
+
46
+ next if property.nil?
47
+
48
+ if self.respond_to?(property_name) &&
49
+ self.send(property_name).respond_to?(:merge) &&
50
+ property.respond_to?(:merge)
51
+ property = self.send(property_name).merge(property)
52
+ end
53
+
54
+ self.send("#{property_name}=", property)
55
+ end
56
+ end
57
+ end
58
+
59
+ def load_file(file_path, env = nil)
60
+ property = YAML.load(ERB.new(IO.read(file_path)).result)
61
+ property = property[env] if env
62
+ use_hashie_if_hash(property)
63
+ end
64
+
65
+ def root
66
+ return Rails.root if defined?(Rails)
67
+ Pathname.new('.')
68
+ end
69
+
70
+ def default_file_pattern
71
+ root.join('config/settings/*.yml')
72
+ end
73
+
74
+ def env
75
+ return Rails.env if defined?(Rails)
76
+ ENV['ENVIRONMENT']
77
+ end
78
+
79
+ def use_hashie_if_hash(property)
80
+ return Figleaf::Fighash.new(property) if property.is_a?(Hash)
81
+ property
82
+ end
83
+
84
+ def override_with_local!(local_file)
85
+ #this file (i.e. test.local.rb) is an optional place to put settings
86
+ local_file.tap do |local_settings_path|
87
+ eval(IO.read(local_settings_path), binding) if File.exists?(local_settings_path)
88
+ end
89
+ end
90
+
91
+ def method_missing(method_name, *args)
92
+ getter_name, modifier = extract_getter_name_and_modifier(method_name)
93
+
94
+ if self.auto_define && modifier == '=' && args.length == 1
95
+ self.define_cattr_methods(getter_name)
96
+ self.send(method_name, args.shift)
97
+ elsif modifier == '?' && args.empty?
98
+ self.send(getter_name).present?
99
+ else
100
+ super
101
+ end
102
+ end
103
+
104
+ def extract_getter_name_and_modifier(method_name)
105
+ match = method_name.to_s.match(/(?<name>.*?)(?<modifier>[?=]?)$/)
106
+ [match[:name], match[:modifier]]
107
+ end
108
+
109
+ def define_cattr_methods(getter_name)
110
+ cattr_writer getter_name
111
+ define_singleton_method(getter_name) do
112
+ result = class_variable_get "@@#{getter_name}"
113
+ result.respond_to?(:call) ? result.call : result
114
+ end
115
+ end
116
+
117
+ end
5
118
  end
6
119
  end
@@ -1,3 +1,3 @@
1
1
  module Figleaf
2
- VERSION = '0.0.7'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/figleaf.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'active_support/concern'
2
- require 'active_support/core_ext/class'
2
+ require 'active_support/core_ext/class/attribute'
3
+ require 'active_support/core_ext/module/attribute_accessors'
3
4
  require "active_support/core_ext/object/blank"
4
5
  require 'hashie'
5
6
  require 'pathname'
6
7
  require 'yaml'
8
+ require 'erb'
7
9
 
8
10
  module Figleaf
9
11
  autoload :Fighash, 'figleaf/fighash'
@@ -1,12 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- class FigleafConfigurationImplementer
4
- include Figleaf::Configuration
5
- end
6
-
7
- describe Figleaf::Configuration do
8
- let(:described_class) { FigleafConfigurationImplementer }
9
-
3
+ describe Figleaf::Settings do
10
4
  describe "setters and getters" do
11
5
  context "auto define enabled" do
12
6
  before(:each) do
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe Figleaf::Settings do
4
+
5
+ describe "self.load_file" do
6
+ before do
7
+ @fixture_path = File.expand_path("../../fixtures/service.yml", __FILE__)
8
+ end
9
+
10
+ it "converts file settings from given env" do
11
+ settings = described_class.load_file(@fixture_path, "test")
12
+ expect(settings.foo).to eq("bar")
13
+ end
14
+
15
+ it "allows env to be optional" do
16
+ settings = described_class.load_file(@fixture_path)
17
+ expect(settings.test.foo).to eq("bar")
18
+ end
19
+
20
+ it "returns nil for missing env" do
21
+ settings = described_class.load_file(@fixture_path, "foo")
22
+ expect(settings).to eq(nil)
23
+ end
24
+ end
25
+
26
+ describe "self.load_settings" do
27
+ before do
28
+ @fixtures_path = File.expand_path("../../fixtures/*.yml", __FILE__)
29
+
30
+ described_class.load_settings(@fixtures_path, "test")
31
+ end
32
+
33
+ it "load some service" do
34
+ expect(described_class.service["foo"]).to eq("bar")
35
+ end
36
+
37
+ it "load indifferently the key names" do
38
+ expect(described_class.service["foo"]).to eq("bar")
39
+ expect(described_class.service[:foo]).to eq("bar")
40
+ end
41
+
42
+ it "create foo as a method" do
43
+ expect(described_class.service.foo).to eq("bar")
44
+ end
45
+
46
+ it "create bool_true? and return true" do
47
+ expect(described_class.service.bool_true?).to eq(true)
48
+ end
49
+
50
+ it "create bool_false? and return false" do
51
+ expect(described_class.service.bool_false?).to eq(false)
52
+ end
53
+
54
+ it "work for array as well" do
55
+ expect(described_class.array).to eq([1, 2])
56
+ end
57
+
58
+ it "and for plain string" do
59
+ expect(described_class.string).to eq("Hello, World!")
60
+ end
61
+
62
+ it "and for boolean (true)" do
63
+ expect(described_class.boolean).to eq(true)
64
+ end
65
+
66
+ it "and for boolean (false)" do
67
+ described_class.load_settings(@fixtures_path, "alt")
68
+ expect(described_class.boolean).to eq(false)
69
+ end
70
+
71
+ it "and for erb values" do
72
+ expect(described_class.erb.foo).to eq('foo')
73
+ expect(described_class.erb.bar).to be_nil
74
+ end
75
+
76
+ it "raise exception when loading an undefined value" do
77
+ YAML.stub(:load_file).and_return({ "test" => {} })
78
+ described_class.load_settings
79
+ expect { described_class.service.blah }.to raise_error NoMethodError
80
+ end
81
+
82
+ context "overloading settings" do
83
+ before do
84
+ overload = File.expand_path("../../fixtures/extra/*.yml", __FILE__)
85
+ described_class.load_settings(overload, "test")
86
+ end
87
+
88
+ it "merge values for matching env" do
89
+ expect(described_class.service.foo).to eq 'overridden'
90
+ expect(described_class.service.extra).to eq 'extra'
91
+ expect(described_class.service.bool_false).to eq(false)
92
+ expect(described_class.service.bool_true).to eq(true)
93
+ end
94
+
95
+ it "not change existing settings for missing env" do
96
+ expect(described_class.array).to eq([1, 2]) # remains unchanged
97
+ end
98
+
99
+ it "not change settings when overloaded file blank" do
100
+ expect(described_class.boolean).to eq(true) # remains unchanged
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ test:
3
+ - 1
4
+ - 2
@@ -0,0 +1,3 @@
1
+ ---
2
+ test: true
3
+ alt: false
@@ -0,0 +1,4 @@
1
+ ---
2
+ test:
3
+ foo: <%= ENV['FIGLEAF_TEST_FOO'] %>
4
+ bar: <%= ENV['FIGLEAF_TEST_BAR'] %>
@@ -0,0 +1,5 @@
1
+ # no test entry is intentional
2
+ ---
3
+ development:
4
+ - 1
5
+ - 2
@@ -0,0 +1 @@
1
+ # intentionally left blank
@@ -0,0 +1,5 @@
1
+ ---
2
+ test:
3
+ foo: overridden
4
+ extra: extra
5
+
@@ -0,0 +1,5 @@
1
+ ---
2
+ test:
3
+ foo: bar
4
+ bool_true: true
5
+ bool_false: false
@@ -0,0 +1,2 @@
1
+ ---
2
+ test: Hello, World!
data/spec/spec_helper.rb CHANGED
@@ -19,3 +19,6 @@ RSpec.configure do |config|
19
19
  # --seed 1234
20
20
  config.order = "random"
21
21
  end
22
+
23
+ ENV['FIGLEAF_TEST_FOO'] ||= 'foo'
24
+ # ENV['FIGLEAF_TEST_BAR'] intentionally left blank
metadata CHANGED
@@ -1,127 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figleaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan C. Müller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-25 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - '>='
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: pry-debugger
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - '>='
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: guard
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - '>='
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: guard-bundler
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - '>='
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: guard-rspec
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - '>='
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - '>='
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
13
  - !ruby/object:Gem::Dependency
98
14
  name: activesupport
99
15
  requirement: !ruby/object:Gem::Requirement
100
16
  requirements:
101
- - - '>='
17
+ - - ! '>='
102
18
  - !ruby/object:Gem::Version
103
- version: '0'
19
+ version: '4'
104
20
  type: :runtime
105
21
  prerelease: false
106
22
  version_requirements: !ruby/object:Gem::Requirement
107
23
  requirements:
108
- - - '>='
24
+ - - ! '>='
109
25
  - !ruby/object:Gem::Version
110
- version: '0'
26
+ version: '4'
111
27
  - !ruby/object:Gem::Dependency
112
28
  name: hashie
113
29
  requirement: !ruby/object:Gem::Requirement
114
30
  requirements:
115
- - - '>='
31
+ - - ! '>='
116
32
  - !ruby/object:Gem::Version
117
- version: '0'
33
+ version: '2'
118
34
  type: :runtime
119
35
  prerelease: false
120
36
  version_requirements: !ruby/object:Gem::Requirement
121
37
  requirements:
122
- - - '>='
38
+ - - ! '>='
123
39
  - !ruby/object:Gem::Version
124
- version: '0'
40
+ version: '2'
125
41
  description: YAML based DRY settings manager.
126
42
  email:
127
43
  - jcmuller@gmail.com
@@ -135,19 +51,26 @@ files:
135
51
  - Gemfile
136
52
  - Gemfile.lock
137
53
  - Guardfile
54
+ - HISTORY.md
138
55
  - LICENSE
139
56
  - README.md
140
57
  - Rakefile
141
58
  - figleaf.gemspec
142
59
  - lib/figleaf.rb
143
- - lib/figleaf/configuration.rb
144
60
  - lib/figleaf/fighash.rb
145
- - lib/figleaf/load_settings.rb
146
61
  - lib/figleaf/settings.rb
147
62
  - lib/figleaf/version.rb
148
63
  - spec/figleaf/configuration_spec.rb
149
64
  - spec/figleaf/fighash_spec.rb
150
- - spec/figleaf/load_settings_spec.rb
65
+ - spec/figleaf/settings_spec.rb
66
+ - spec/fixtures/array.yml
67
+ - spec/fixtures/boolean.yml
68
+ - spec/fixtures/erb.yml
69
+ - spec/fixtures/extra/array.yml
70
+ - spec/fixtures/extra/boolean.yml
71
+ - spec/fixtures/extra/service.yml
72
+ - spec/fixtures/service.yml
73
+ - spec/fixtures/string.yml
151
74
  - spec/spec_helper.rb
152
75
  homepage: http://github.com/challengepost/figleaf
153
76
  licenses: []
@@ -158,22 +81,31 @@ require_paths:
158
81
  - lib
159
82
  required_ruby_version: !ruby/object:Gem::Requirement
160
83
  requirements:
161
- - - '>='
84
+ - - ! '>='
162
85
  - !ruby/object:Gem::Version
163
86
  version: '0'
164
87
  required_rubygems_version: !ruby/object:Gem::Requirement
165
88
  requirements:
166
- - - '>='
89
+ - - ! '>='
167
90
  - !ruby/object:Gem::Version
168
91
  version: '0'
169
92
  requirements: []
170
93
  rubyforge_project:
171
- rubygems_version: 2.0.3
94
+ rubygems_version: 2.2.3
172
95
  signing_key:
173
96
  specification_version: 4
174
97
  summary: YAML based DRY settings manager.
175
98
  test_files:
176
99
  - spec/figleaf/configuration_spec.rb
177
100
  - spec/figleaf/fighash_spec.rb
178
- - spec/figleaf/load_settings_spec.rb
101
+ - spec/figleaf/settings_spec.rb
102
+ - spec/fixtures/array.yml
103
+ - spec/fixtures/boolean.yml
104
+ - spec/fixtures/erb.yml
105
+ - spec/fixtures/extra/array.yml
106
+ - spec/fixtures/extra/boolean.yml
107
+ - spec/fixtures/extra/service.yml
108
+ - spec/fixtures/service.yml
109
+ - spec/fixtures/string.yml
179
110
  - spec/spec_helper.rb
111
+ has_rdoc:
@@ -1,61 +0,0 @@
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
- getter_name, modifier = extract_getter_name_and_modifier(method_name)
36
-
37
- if self.auto_define && modifier == '=' && args.length == 1
38
- self.define_cattr_methods(getter_name)
39
- self.send(method_name, args.shift)
40
- elsif modifier == '?' && args.empty?
41
- self.send(getter_name).present?
42
- else
43
- super
44
- end
45
- end
46
-
47
- def extract_getter_name_and_modifier(method_name)
48
- match = method_name.to_s.match(/(?<name>.*?)(?<modifier>[?=]?)$/)
49
- [match[:name], match[:modifier]]
50
- end
51
-
52
- def define_cattr_methods(getter_name)
53
- cattr_writer getter_name
54
- define_singleton_method(getter_name) do
55
- result = class_variable_get "@@#{getter_name}"
56
- result.respond_to?(:call) ? result.call : result
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,39 +0,0 @@
1
- module Figleaf
2
- module LoadSettings
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- # Load all files in config/settings and set their contents as first level
7
- # citizen of Settings:
8
- # config/settings/some_service.yml contains
9
- # production:
10
- # foo: bar
11
- # --> Settings.some_service.foo = bar
12
- def load_settings
13
- Dir.glob(root.join('config', 'settings', '*.yml')).each do |file|
14
- property_name = File.basename(file, '.yml')
15
- property = YAML.load_file(file)[env]
16
- property = use_hashie_if_hash(property)
17
- self.configure_with_auto_define do |s|
18
- s.send("#{property_name}=", property)
19
- end
20
- end
21
- end
22
-
23
- def root
24
- return Rails.root if defined?(Rails)
25
- Pathname.new('.')
26
- end
27
-
28
- def env
29
- return Rails.env if defined?(Rails)
30
- ENV['ENVIRONMENT']
31
- end
32
-
33
- def use_hashie_if_hash(property)
34
- return Figleaf::Fighash.new(property) if property.is_a?(Hash)
35
- property
36
- end
37
- end
38
- end
39
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class FigleafLoadSettingsImplementer
4
- include Figleaf::Configuration
5
- include Figleaf::LoadSettings
6
- end
7
-
8
- describe Figleaf::LoadSettings do
9
- let(:described_class) { FigleafLoadSettingsImplementer }
10
-
11
- describe "self.load_settings" do
12
- let(:configuration) {
13
- {
14
- "test" => {
15
- "foo" => "bar",
16
- "bool_true" => true,
17
- "bool_false" => false
18
- }
19
- }
20
- }
21
-
22
- before do
23
- Dir.stub(:glob).and_return(["config/described_class/some_service.yml"])
24
- described_class.stub(:env).and_return("test")
25
- YAML.stub(:load_file).and_return(configuration)
26
-
27
- described_class.load_settings
28
- end
29
-
30
- it "should load some service" do
31
- described_class.some_service["foo"].should == "bar"
32
- end
33
-
34
- it "should load indifferently the key names" do
35
- described_class.some_service["foo"].should == "bar"
36
- described_class.some_service[:foo].should == "bar"
37
- end
38
-
39
- it "should create foo as a method" do
40
- described_class.some_service.foo.should == "bar"
41
- end
42
-
43
- it "should create bool_true? and return true" do
44
- described_class.some_service.bool_true?.should be_true
45
- end
46
-
47
- it "should create bool_false? and return false" do
48
- described_class.some_service.bool_false?.should be_false
49
- end
50
-
51
- it "should work for arrays as well" do
52
- YAML.stub(:load_file).and_return({ "test" => [1, 2] })
53
- described_class.load_settings
54
- described_class.some_service.should == [1, 2]
55
- end
56
-
57
- it "and for plain strings" do
58
- YAML.stub(:load_file).and_return({ "test" => "Hello, World!" })
59
- described_class.load_settings
60
- described_class.some_service.should == "Hello, World!"
61
- end
62
-
63
- it "and for booleans (true)" do
64
- YAML.stub(:load_file).and_return({ "test" => true })
65
- described_class.load_settings
66
- described_class.some_service.should be_true
67
- end
68
-
69
- it "and for booleans (false)" do
70
- YAML.stub(:load_file).and_return({ "test" => false })
71
- described_class.load_settings
72
- described_class.some_service.should be_false
73
- end
74
-
75
- it "should raise exception when loading an undefined value" do
76
- YAML.stub(:load_file).and_return({ "test" => {} })
77
- described_class.load_settings
78
- expect { described_class.some_service.blah }.to raise_error NoMethodError
79
- end
80
-
81
- end
82
- end