figleaf 0.2.5 → 0.2.6

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
2
  SHA1:
3
- metadata.gz: ebc37827cd68fb9d931fa327581d19acd47a2c02
4
- data.tar.gz: 7f5c2050291f1ead028c57e98febeba9329fbe97
3
+ metadata.gz: d23035213f0420e9f22c4cb80640be461b048781
4
+ data.tar.gz: 71e650d1680977aa5353e1a60d71a52e44003ca1
5
5
  SHA512:
6
- metadata.gz: 73bc054a55de88db693d019a84d816e5a6e2adf40d76b8fcbba5484a49d79e0afc36fddada3e9d72bd1ff6e2797844c193f765cdd482d7166656f8d424f8d34e
7
- data.tar.gz: 452291366fe8950e631cf4a0c77c8da558e68291b904481e9df10b3abf797656d0b31b86640a5b95c280b9ea7882a45b300065c8cf39d9449ced1493ad9262b9
6
+ metadata.gz: abad1d584376dc35bfbdf421fd7969e7ab9fa731748e825237da18a961e5507a8c3fb859d2784612a719ac0645f313fdacbcabd79871f8bd4fbffe0b715817e2
7
+ data.tar.gz: 89742c7aad92c275d11e5b5e4aacb393de330a8b11cf9e3baf602398302f93bd5cc46f253c73da9623e12f13b1a34aa469e9563893795548134e45d9107a9722
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.2.6 (2017/09/28)
4
+ Add ability to write config files in ruby
5
+
3
6
  ## 0.2.5 (2017/09/28)
4
7
  Add support for `default` options
5
8
 
@@ -0,0 +1,55 @@
1
+ module Figleaf
2
+ # Convert a ruby block to nested hash
3
+ class Config
4
+ def initialize
5
+ @property = LazyBlockHash.new
6
+ end
7
+
8
+ def call(&block)
9
+ instance_eval(&block)
10
+
11
+ property
12
+ end
13
+
14
+ def method_missing(method_name, *args, &block)
15
+ process_method(method_name, *args, &block)
16
+ end
17
+
18
+ def test(&block)
19
+ process_method(:test, [], &block)
20
+ end
21
+
22
+ def process_method(method_name, *args, &block)
23
+ @property[method_name.to_s] =
24
+ if block_given?
25
+ obj = self.class.new
26
+ Proc.new { obj.call(&block) }
27
+ else
28
+ if args.count == 1
29
+ args[0]
30
+ else
31
+ args
32
+ end
33
+ end
34
+ end
35
+
36
+ def respond_to_missing?(method_name, *args)
37
+ true
38
+ end
39
+
40
+ private
41
+
42
+ attr_reader :property
43
+ end
44
+
45
+ class LazyBlockHash < Hash
46
+ def [](attr)
47
+ val = super(attr)
48
+ if val.is_a?(Proc)
49
+ val.call
50
+ else
51
+ val
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,6 +1,7 @@
1
1
  module Figleaf
2
2
  class Settings
3
3
  InvalidYAML = Class.new(RuntimeError)
4
+ InvalidRb = Class.new(RuntimeError)
4
5
 
5
6
  class_attribute :auto_define
6
7
  self.auto_define = false
@@ -41,12 +42,7 @@ module Figleaf
41
42
  def load_settings(file_pattern = default_file_pattern, env_to_load = env)
42
43
  configure_with_auto_define do
43
44
  Dir.glob(file_pattern).each do |file|
44
- property_name = File.basename(file, '.yml')
45
- yaml_hash = load_file(file) or next
46
- default = yaml_hash["default"]
47
- property = yaml_hash[env_to_load]
48
-
49
- property = default.merge(property) if !default.nil?
45
+ property_name, property = load_file(file, env_to_load)
50
46
 
51
47
  next if property.nil?
52
48
 
@@ -61,10 +57,34 @@ module Figleaf
61
57
  end
62
58
  end
63
59
 
64
- def load_file(file_path, env = nil)
65
- property = YAML.load(ERB.new(IO.read(file_path)).result)
66
- property = property[env] if env
67
- use_hashie_if_hash(property)
60
+ def load_file(file, env_to_load = env)
61
+ if file.end_with?('.rb')
62
+ property_name = File.basename(file, '.rb')
63
+ config = load_rb_file(file) or return nil
64
+ else
65
+ property_name = File.basename(file, '.yml')
66
+ config = load_yaml_file(file) or return nil
67
+ end
68
+
69
+ return if config.nil?
70
+
71
+ default = config["default"]
72
+ property = config[env_to_load]
73
+ property = default.merge(property) if !default.nil?
74
+
75
+ [property_name, use_hashie_if_hash(property)]
76
+ end
77
+
78
+ def load_rb_file(file_path, env = nil)
79
+ contents = File.read(file_path)
80
+ block = ->(*) { eval contents }
81
+ Config.new.call(&block)
82
+ rescue SyntaxError => e
83
+ raise InvalidRb, "#{file_path} has invalid Ruby\n" + e.message
84
+ end
85
+
86
+ def load_yaml_file(file_path)
87
+ YAML.load(ERB.new(IO.read(file_path)).result)
68
88
  rescue Psych::SyntaxError => e
69
89
  raise InvalidYAML, "#{file_path} has invalid YAML\n" + e.message
70
90
  end
@@ -75,7 +95,8 @@ module Figleaf
75
95
  end
76
96
 
77
97
  def default_file_pattern
78
- root.join('config/settings/*.yml')
98
+ root.join('config/settings/*.yml') +
99
+ root.join('config/settings/*.rb')
79
100
  end
80
101
 
81
102
  def env
@@ -1,3 +1,3 @@
1
1
  module Figleaf
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.6'
3
3
  end
data/lib/figleaf.rb CHANGED
@@ -8,9 +8,8 @@ require 'yaml'
8
8
  require 'erb'
9
9
 
10
10
  module Figleaf
11
+ autoload :Config, 'figleaf/config'
11
12
  autoload :Fighash, 'figleaf/fighash'
12
- autoload :Configuration, 'figleaf/configuration'
13
- autoload :LoadSettings, 'figleaf/load_settings'
14
13
  autoload :Settings, 'figleaf/settings'
15
14
  autoload :VERSION, 'figleaf/version'
16
15
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ module Figleaf
4
+ RSpec.describe Config do
5
+ let(:code) do
6
+ proc do
7
+ setting_set_on_root "bar"
8
+
9
+ default do
10
+ foo "bar"
11
+ bar "baz"
12
+ end
13
+
14
+ production do
15
+ env "foo"
16
+ end
17
+
18
+ failer do
19
+ raise "Hello"
20
+ end
21
+
22
+ test do
23
+ foo "testbar"
24
+ end
25
+ end
26
+ end
27
+
28
+ subject(:config) { described_class.new }
29
+
30
+ describe "#call" do
31
+ subject(:called) { config.call(&code) }
32
+ it "stores first level keywords as keys" do
33
+ expect(called.keys).to contain_exactly(*%w(
34
+ default
35
+ failer
36
+ production
37
+ setting_set_on_root
38
+ test
39
+ ))
40
+ end
41
+
42
+ it "expands default" do
43
+ expect(called["default"]).to eq(
44
+ "foo" => "bar",
45
+ "bar" => "baz"
46
+ )
47
+ end
48
+
49
+ it "expands test" do
50
+ expect(called["test"]).to eq(
51
+ "foo" => "testbar"
52
+ )
53
+ end
54
+
55
+ it "only raises when evaluating 'failer'" do
56
+ expect { called["failer"] }.to raise_error(RuntimeError)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,28 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
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
4
  describe "self.load_settings" do
27
5
  before do
28
6
  @fixtures_path = File.expand_path("../../fixtures/*.yml", __FILE__)
@@ -74,17 +52,28 @@ describe Figleaf::Settings do
74
52
  end
75
53
 
76
54
  it "raise exception when loading an undefined value" do
77
- YAML.stub(:load_file).and_return({ "test" => {} })
55
+ YAML.stub(:load_yaml_file).and_return({ "test" => {} })
78
56
  described_class.load_settings
79
57
  expect { described_class.service.blah }.to raise_error NoMethodError
80
58
  end
81
59
 
82
60
  context "with bad files" do
83
- let(:overload) { File.expand_path("../../fixtures/errors/*.yml", __FILE__) }
61
+ context "yaml" do
62
+ let(:overload) { File.expand_path("../../fixtures/errors/*.yml", __FILE__) }
84
63
 
85
- it "reports the file that has errors" do
86
- expect { described_class.load_settings(overload, "test") }.
87
- to raise_error(described_class::InvalidYAML)
64
+ it "reports the file that has errors" do
65
+ expect { described_class.load_settings(overload, "test") }.
66
+ to raise_error(described_class::InvalidYAML)
67
+ end
68
+ end
69
+
70
+ context "rb" do
71
+ let(:overload) { File.expand_path("../../fixtures/errors/*.rb", __FILE__) }
72
+
73
+ it "reports the file that has errors" do
74
+ expect { described_class.load_settings(overload, "test") }.
75
+ to raise_error(described_class::InvalidRb)
76
+ end
88
77
  end
89
78
  end
90
79
 
@@ -127,17 +116,58 @@ describe Figleaf::Settings do
127
116
 
128
117
  context "using default as a YAML anchor is OK" do
129
118
  before do
130
- default = File.expand_path("../../fixtures/extra/default_anchor.yml", __FILE__)
131
- described_class.load_settings(default, "test")
119
+ default_anchor = File.expand_path("../../fixtures/extra/default_anchor.yml", __FILE__)
120
+ described_class.load_settings(default_anchor, "test")
132
121
  end
133
122
 
134
123
  it "overrides values" do
135
- expect(described_class.default.foo).to eq("overriden")
124
+ expect(described_class.default_anchor.foo).to eq("overriden")
136
125
  end
137
126
 
138
- it "respects values set in default" do
139
- expect(described_class.default.bar).to eq("baz")
127
+ it "respects values set in default_anchor" do
128
+ expect(described_class.default_anchor.bar).to eq("baz")
140
129
  end
141
130
  end
142
131
  end
132
+
133
+ context "load ruby files" do
134
+ before do
135
+ fixtures_path = File.expand_path("../../fixtures/extra/*.rb", __FILE__)
136
+ described_class.load_settings(fixtures_path, "test")
137
+ end
138
+
139
+ it "load indifferently the key names" do
140
+ expect(described_class.code["foo"]).to eq("bar")
141
+ expect(described_class.code[:foo]).to eq("bar")
142
+ end
143
+
144
+ it "create foo as a method" do
145
+ expect(described_class.code.foo).to eq("bar")
146
+ end
147
+
148
+ it "create bool_true? and return true" do
149
+ expect(described_class.code.bool_true?).to eq(true)
150
+ end
151
+
152
+ it "create bool_false? and return false" do
153
+ expect(described_class.code.bool_false?).to eq(false)
154
+ end
155
+
156
+ it "work for array as well", :aggregate_failures do
157
+ expect(described_class.code.array).to eq([1, 2, 3, 4])
158
+ expect(described_class.code.array_alt).to eq([1, 2, 3, 4])
159
+ end
160
+
161
+ it "and for boolean (true)" do
162
+ expect(described_class.code.bool_true).to eq(true)
163
+ end
164
+
165
+ it "and for boolean (false)" do
166
+ expect(described_class.code.bool_false).to eq(false)
167
+ end
168
+
169
+ it "and for ENV values" do
170
+ expect(described_class.code.from_env).to eq('foo')
171
+ end
172
+ end
143
173
  end
@@ -0,0 +1 @@
1
+ begin
@@ -0,0 +1,21 @@
1
+ default do
2
+ foo "bar"
3
+ bar "baz"
4
+ end
5
+
6
+ production do
7
+ env "foo"
8
+ end
9
+
10
+ dev_int do
11
+ bar "baz"
12
+ end
13
+
14
+ test do
15
+ foo "bar"
16
+ bool_true true
17
+ bool_false false
18
+ array 1, 2, 3, 4
19
+ array_alt [1, 2, 3, 4]
20
+ from_env ENV['FIGLEAF_TEST_FOO']
21
+ end
data/spec/spec_helper.rb CHANGED
@@ -9,7 +9,6 @@ require "figleaf"
9
9
 
10
10
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
11
11
  RSpec.configure do |config|
12
- config.treat_symbols_as_metadata_keys_with_true_values = true
13
12
  config.run_all_when_everything_filtered = true
14
13
  config.filter_run :focus
15
14
 
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.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan C. Müller
@@ -57,18 +57,22 @@ files:
57
57
  - Rakefile
58
58
  - figleaf.gemspec
59
59
  - lib/figleaf.rb
60
+ - lib/figleaf/config.rb
60
61
  - lib/figleaf/fighash.rb
61
62
  - lib/figleaf/settings.rb
62
63
  - lib/figleaf/version.rb
64
+ - spec/figleaf/config_spec.rb
63
65
  - spec/figleaf/configuration_spec.rb
64
66
  - spec/figleaf/fighash_spec.rb
65
67
  - spec/figleaf/settings_spec.rb
66
68
  - spec/fixtures/array.yml
67
69
  - spec/fixtures/boolean.yml
68
70
  - spec/fixtures/erb.yml
71
+ - spec/fixtures/errors/bad_file.rb
69
72
  - spec/fixtures/errors/bad_file.yml
70
73
  - spec/fixtures/extra/array.yml
71
74
  - spec/fixtures/extra/boolean.yml
75
+ - spec/fixtures/extra/code.rb
72
76
  - spec/fixtures/extra/default.yml
73
77
  - spec/fixtures/extra/default_anchor.yml
74
78
  - spec/fixtures/extra/service.yml
@@ -99,15 +103,18 @@ signing_key:
99
103
  specification_version: 4
100
104
  summary: YAML based DRY settings manager.
101
105
  test_files:
106
+ - spec/figleaf/config_spec.rb
102
107
  - spec/figleaf/configuration_spec.rb
103
108
  - spec/figleaf/fighash_spec.rb
104
109
  - spec/figleaf/settings_spec.rb
105
110
  - spec/fixtures/array.yml
106
111
  - spec/fixtures/boolean.yml
107
112
  - spec/fixtures/erb.yml
113
+ - spec/fixtures/errors/bad_file.rb
108
114
  - spec/fixtures/errors/bad_file.yml
109
115
  - spec/fixtures/extra/array.yml
110
116
  - spec/fixtures/extra/boolean.yml
117
+ - spec/fixtures/extra/code.rb
111
118
  - spec/fixtures/extra/default.yml
112
119
  - spec/fixtures/extra/default_anchor.yml
113
120
  - spec/fixtures/extra/service.yml