fuguta 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/fuguta.rb +65 -16
- data/lib/fuguta/version.rb +1 -1
- data/spec/{nest-test1.conf → conf_files/nest-test1.conf} +0 -0
- data/spec/conf_files/syntax-error.conf +3 -0
- data/spec/{test1.conf → conf_files/test1.conf} +0 -0
- data/spec/{test2.conf → conf_files/test2.conf} +0 -0
- data/spec/configuration_spec.rb +56 -15
- data/spec/spec_helper.rb +2 -0
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ce06a414af76fbb7237b3cc66001ed726b3e466
|
4
|
+
data.tar.gz: 102c6d2aad85b6d62ce59bc0decc43eda85a67be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41d0b8df4582f1da011f2454d7b3378555780b8c67bd9cc6a94383e0ac78de049c01d33644ed9b142ea1b59fc5ada83ee35eb4b6e6ffabdf6cc6e12657e1aa7d
|
7
|
+
data.tar.gz: 807eb8d86ea21ce3118602bcf9d44a4e531a6f6d538b5e4091ae0c5b9ae8ca741806081a88cde928196072477bd8abca2ba52606d2971c26a9e790dfcd89515a
|
data/.travis.yml
CHANGED
data/lib/fuguta.rb
CHANGED
@@ -1,16 +1,50 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
module Fuguta
|
4
|
+
class ValidationError < StandardError
|
5
|
+
attr_reader :errors
|
6
|
+
def initialize(errors)
|
7
|
+
super("validation error")
|
8
|
+
@errors = errors
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class SyntaxError < StandardError
|
13
|
+
# self.cause() is as of Ruby 2.1 so we
|
14
|
+
# handles root error .
|
15
|
+
attr_reader :root_cause, :source
|
16
|
+
|
17
|
+
def initialize(root_cause, source="")
|
18
|
+
super("Syntax Error")
|
19
|
+
raise ArgumentError, 'root_cause' unless root_cause.is_a?(::Exception)
|
20
|
+
@root_cause = root_cause
|
21
|
+
@source = source
|
22
|
+
end
|
23
|
+
|
24
|
+
def message
|
25
|
+
if @root_cause.backtrace.first =~ /:(\d+):in `/ ||
|
26
|
+
@root_cause.backtrace.first =~ /:(\d+)$/
|
27
|
+
line = $1.to_i
|
28
|
+
end
|
29
|
+
"%s from %s:%d" % [super(), @source, line]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
4
33
|
class Configuration
|
34
|
+
ValidationError = Fuguta::ValidationError
|
5
35
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@
|
36
|
+
def self.usual_paths(paths = nil)
|
37
|
+
if paths
|
38
|
+
@usual_paths = paths
|
39
|
+
else
|
40
|
+
@usual_paths
|
11
41
|
end
|
12
42
|
end
|
13
43
|
|
44
|
+
def usual_paths
|
45
|
+
self.class.usual_paths
|
46
|
+
end
|
47
|
+
|
14
48
|
def self.walk_tree(conf, &blk)
|
15
49
|
raise ArgumentError, "conf must be a 'Configuration'. Got '#{conf.class}'." unless conf.is_a?(Configuration)
|
16
50
|
|
@@ -32,14 +66,20 @@ module Fuguta
|
|
32
66
|
@conf = conf
|
33
67
|
end
|
34
68
|
|
35
|
-
def load(path)
|
36
|
-
buf =
|
37
|
-
|
69
|
+
def load(path = nil)
|
70
|
+
buf = case path
|
71
|
+
when NilClass
|
72
|
+
raise "No path given and usual_paths not set" unless @conf.usual_paths
|
73
|
+
|
74
|
+
path = @conf.usual_paths.find { |path| File.exists?(path) } ||
|
75
|
+
raise("None of the usual paths existed: #{@conf.usual_paths.join(", ")}")
|
76
|
+
|
77
|
+
File.read(path)
|
38
78
|
when String
|
39
79
|
raise "does not exist: #{path}" unless File.exists?(path)
|
40
|
-
|
80
|
+
File.read(path)
|
41
81
|
when IO
|
42
|
-
path.lines.
|
82
|
+
path.lines.join
|
43
83
|
else
|
44
84
|
raise "Unknown type: #{path.class}"
|
45
85
|
end
|
@@ -92,7 +132,7 @@ module Fuguta
|
|
92
132
|
l.load(File.expand_path(path, base_conf_dir))
|
93
133
|
end
|
94
134
|
}
|
95
|
-
|
135
|
+
|
96
136
|
self
|
97
137
|
end
|
98
138
|
end
|
@@ -224,7 +264,7 @@ module Fuguta
|
|
224
264
|
end
|
225
265
|
}
|
226
266
|
end
|
227
|
-
|
267
|
+
|
228
268
|
def alias_param (alias_name, ref_name)
|
229
269
|
# getter
|
230
270
|
self.class_eval %Q{
|
@@ -295,9 +335,12 @@ module Fuguta
|
|
295
335
|
|
296
336
|
l = Loader.new(c)
|
297
337
|
|
298
|
-
paths.
|
299
|
-
l.load
|
300
|
-
|
338
|
+
if paths.empty?
|
339
|
+
l.load
|
340
|
+
else
|
341
|
+
paths.each { |path| l.load(path) }
|
342
|
+
end
|
343
|
+
|
301
344
|
l.validate
|
302
345
|
|
303
346
|
c
|
@@ -380,6 +423,8 @@ module Fuguta
|
|
380
423
|
def validate(errors)
|
381
424
|
end
|
382
425
|
|
426
|
+
SYNTAX_ERROR_SOURCES=[ScriptError, NameError].freeze
|
427
|
+
|
383
428
|
def parse_dsl(&blk)
|
384
429
|
dsl = self.class.const_get(:DSL, false)
|
385
430
|
raise "DSL module was not found" unless dsl && dsl.is_a?(Module)
|
@@ -388,7 +433,11 @@ module Fuguta
|
|
388
433
|
cp_class.__send__(:include, dsl)
|
389
434
|
cp = cp_class.new(self)
|
390
435
|
|
391
|
-
|
436
|
+
begin
|
437
|
+
cp.instance_eval(&blk)
|
438
|
+
rescue *SYNTAX_ERROR_SOURCES => e
|
439
|
+
raise Fuguta::SyntaxError.new(e, cp.instance_variable_get(:@loading_path))
|
440
|
+
end
|
392
441
|
|
393
442
|
self
|
394
443
|
end
|
data/lib/fuguta/version.rb
CHANGED
File without changes
|
File without changes
|
File without changes
|
data/spec/configuration_spec.rb
CHANGED
@@ -15,23 +15,64 @@ describe Fuguta::Configuration do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
19
|
-
it "loads conf file" do
|
20
|
-
conf = Test1.load(File.expand_path('../test1.conf', __FILE__))
|
21
|
-
expect(conf.param1).to eq(1)
|
22
|
-
expect(conf.param2).to eq(2)
|
23
|
-
end
|
24
18
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
19
|
+
class UsualPathTest < Test1
|
20
|
+
usual_paths([ "../test1.conf", "../conf_files/test1.conf" ].map do |path|
|
21
|
+
File.expand_path(path, __FILE__)
|
22
|
+
end)
|
29
23
|
end
|
30
24
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
describe "#load" do
|
26
|
+
let(:conf_path) { File.expand_path('../conf_files', __FILE__) }
|
27
|
+
|
28
|
+
it "allows nested imports/loads" do
|
29
|
+
conf = NestTest1.load("#{conf_path}/nest-test1.conf")
|
30
|
+
expect(conf.param1).to eq(10)
|
31
|
+
expect(conf.param2).to eq(20)
|
32
|
+
expect(conf.param3).to eq(30)
|
33
|
+
end
|
34
|
+
|
35
|
+
context "with a single path (string) parameter" do
|
36
|
+
it "loads conf file from that path" do
|
37
|
+
conf = Test1.load("#{conf_path}/test1.conf")
|
38
|
+
expect(conf.param1).to eq(1)
|
39
|
+
expect(conf.param2).to eq(2)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "with multiple paths passed as parameters" do
|
44
|
+
it "the config files override each other" do
|
45
|
+
conf = Test1.load("#{conf_path}/test1.conf", "#{conf_path}/test2.conf")
|
46
|
+
expect(conf.param1).to eq(10)
|
47
|
+
expect(conf.param2).to eq(20)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "with no arguments" do
|
52
|
+
context "when usual_paths is set" do
|
53
|
+
it "loads configurations from the first existing path set in usual_paths" do
|
54
|
+
conf = UsualPathTest.load
|
55
|
+
expect(conf.param1).to eq(1)
|
56
|
+
expect(conf.param2).to eq(2)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when usual_paths is not set" do
|
61
|
+
it "raises an error" do
|
62
|
+
expect { Test1.load }.to raise_error(
|
63
|
+
RuntimeError,
|
64
|
+
"No path given and usual_paths not set"
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when loading a config file with a faulty syntax' do
|
71
|
+
it "throws syntax error" do
|
72
|
+
expect {
|
73
|
+
Test1.load("#{conf_path}/syntax-error.conf")
|
74
|
+
}.to raise_error(Fuguta::SyntaxError)
|
75
|
+
end
|
76
|
+
end
|
36
77
|
end
|
37
78
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fuguta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Axsh co. LTD
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A configuration framework for Ruby programs
|
14
14
|
email:
|
@@ -24,11 +24,12 @@ files:
|
|
24
24
|
- fuguta.gemspec
|
25
25
|
- lib/fuguta.rb
|
26
26
|
- lib/fuguta/version.rb
|
27
|
+
- spec/conf_files/nest-test1.conf
|
28
|
+
- spec/conf_files/syntax-error.conf
|
29
|
+
- spec/conf_files/test1.conf
|
30
|
+
- spec/conf_files/test2.conf
|
27
31
|
- spec/configuration_spec.rb
|
28
|
-
- spec/nest-test1.conf
|
29
32
|
- spec/spec_helper.rb
|
30
|
-
- spec/test1.conf
|
31
|
-
- spec/test2.conf
|
32
33
|
homepage: https://github.com/axsh/fuguta
|
33
34
|
licenses:
|
34
35
|
- LGPLv3
|