ficus 0.0.3 → 0.0.4

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ba933b40ba1c5b1704bb479441920d3a4612b54
4
+ data.tar.gz: a12978dd4388dc3b75850403351f47f983b27030
5
+ SHA512:
6
+ metadata.gz: 1a5c5445fa90f0aabd3a2a68440a7e38a8f4e568a49859b318a8927d42a78b8987972e32857c678479f7d0738c4476d2a0de9db657c1dc38f0ef2f5c7f6668fc
7
+ data.tar.gz: 7d9c0752e7c5e781198d92d941fec2bbc8474c291f6a96ebd7358b71483ecc6de5e4034a58d7fae6e5ab86a2af75a07f12686a8d12014206380ea4b8283edea0
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "ficus"
7
- spec.version = '0.0.3'
7
+ spec.version = '0.0.4'
8
8
  spec.authors = ["Drew Fradette"]
9
9
  spec.email = ["drew.fradette@gmail.com"]
10
10
  spec.description = 'A runtime validation configuration DSL'
@@ -1,60 +1,7 @@
1
1
  require 'yaml'
2
+ require 'logger'
2
3
  require 'recursive-open-struct'
3
4
 
4
- class Ficus < RecursiveOpenStruct
5
- class ConfigError < StandardError; end
6
- class << self
7
- attr_accessor :log, :verbose
8
-
9
- def load(file, &block)
10
- @log = []
11
- yaml = YAML.load File.read(file)
12
- config = Ficus.new(yaml, :recurse_over_arrays => true)
13
- config.instance_eval(&block) if block_given?
14
-
15
- errors = log.select{|v| v =~ /^\[ERR\]/}
16
- if errors.size > 0
17
- log.each{|v| puts v} if ENV['DEBUG']
18
- raise ConfigError.new('Unable to start due to invalid settings')
19
- end
20
- config
21
- end
22
-
23
- def log(log = {})
24
- @log << log
25
- end
26
- end
27
-
28
- def section(name, args = {}, &block)
29
- sections(name).each do |section|
30
- if section.nil?
31
- level = args[:optional] ? 'WARN' : 'ERR'
32
- Ficus.log "[#{level}] Section #{name} is not defined"
33
- else
34
- section.parent = self.parent ? "#{self.parent}.#{name}" : name
35
- section.instance_eval &block if block_given?
36
- end
37
- end
38
- end
39
-
40
- def sections(name)
41
- if name == :all
42
- sections(/.*/)
43
- elsif name.is_a? String
44
- [self.send(name)]
45
- elsif name.is_a? Regexp
46
- self.marshal_dump.keys.map{|k|
47
- self.send(k) unless k == :parent
48
- }.compact!
49
- end
50
- end
51
-
52
- def optional(name, default)
53
- self.send("#{name}=", default) if self.send(name).nil?
54
- end
55
-
56
- def required(name)
57
- prefix = self.parent ? "#{self.parent}." : nil
58
- Ficus.log "[ERR] Option #{prefix}#{name} is not defined" if self.send(name).nil?
59
- end
60
- end
5
+ require 'ficus/loader'
6
+ require 'ficus/dsl'
7
+ require 'ficus/exceptions'
@@ -0,0 +1,36 @@
1
+ class Ficus < RecursiveOpenStruct
2
+ def section(name, args = {}, &block)
3
+ sections(name).each do |s|
4
+ s.parent = self.parent ? "#{self.parent}.#{name}" : name
5
+ s.instance_eval(&block) if block_given?
6
+ end
7
+ rescue NoSection
8
+ if args[:optional] == true
9
+ Ficus.warning("Section #{name} is not defined")
10
+ else
11
+ Ficus.error("Section #{name} is not defined")
12
+ end
13
+ end
14
+
15
+ def sections(name)
16
+ if name == :all
17
+ sections(/.*/)
18
+ elsif name.is_a? Regexp
19
+ matches = self.marshal_dump.keys
20
+ matches.map { |k| self.send(k) unless k == :parent }.compact!
21
+ else
22
+ s = self.send(name)
23
+ raise NoSection.new if s.nil?
24
+ [s]
25
+ end
26
+ end
27
+
28
+ def optional(name, default)
29
+ self.send("#{name}=", default) if self.send(name).nil?
30
+ end
31
+
32
+ def required(name)
33
+ prefix = self.parent ? "#{self.parent}." : nil
34
+ Ficus.error "Option #{prefix}#{name} is not defined" if self.send(name).nil?
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ class Ficus < RecursiveOpenStruct
2
+ class ConfigError < StandardError; end
3
+ class NoSection < StandardError; end
4
+ end
@@ -0,0 +1,38 @@
1
+ class Ficus < RecursiveOpenStruct
2
+ class << self
3
+ attr_accessor :verbose, :logger, :errors, :warnings
4
+
5
+ # Load the configuration file and validate.
6
+ def load(file, &block)
7
+ @errors, @warnings, @config = [], [], nil
8
+ config(file).instance_eval(&block) if block_given?
9
+
10
+ warnings.each { |e| logger.warn e }
11
+ errors.each { |e| logger.error e }
12
+ raise ConfigError.new('Unable to start due to invalid settings') if errors.size > 0
13
+ config(file)
14
+ end
15
+
16
+ def error(msg)
17
+ @errors << msg
18
+ end
19
+
20
+ def warning(msg)
21
+ @warnings << msg
22
+ end
23
+
24
+ protected
25
+ def config(file)
26
+ if @config.nil?
27
+ yaml = YAML.load File.read(file)
28
+ @config = Ficus.new(yaml, :recurse_over_arrays => true)
29
+ end
30
+ @config
31
+ end
32
+
33
+ def logger
34
+ @logger ||= Logger.new(STDERR).tap { |l| l.level = Logger::WARN }
35
+ end
36
+ end
37
+ end
38
+
@@ -1,14 +1,13 @@
1
1
  require 'spec_helper'
2
-
3
2
  require 'tempfile'
4
3
  require 'yaml'
5
-
6
4
  require 'ficus'
7
5
 
8
6
  describe Ficus do
9
7
  before :each do
8
+ Ficus.logger = logger
10
9
  @config = {
11
- :general => {:key1=>'value1',:key2=>'value2',:key3=>'value3'},
10
+ :general => {:key1=>'value1', :key2=>'value2', :key3=>'value3'},
12
11
  :misc => {
13
12
  :key4 => 'value4',
14
13
  :list => {:item1 => 'value1', :item2 => 'value2'}
@@ -19,7 +18,6 @@ describe Ficus do
19
18
  it 'should load the config without any validation' do
20
19
  config_file do |config|
21
20
  config = Ficus.load(config)
22
-
23
21
  config.to_h.should eq @config
24
22
  end
25
23
  end
@@ -29,32 +27,32 @@ describe Ficus do
29
27
  config = Ficus.load(config) do
30
28
  section 'not_real', :optional => true
31
29
  end
32
- Ficus.log.count{|v| v =~ /^\[WARN\]/}.should eq 1
30
+ Ficus.warnings.size.should eq 1
33
31
  end
34
32
  end
35
33
 
36
34
  it 'should load the config with a error about a missing section' do
37
35
  config_file do |config|
38
- expect {
36
+ expect do
39
37
  config = Ficus.load(config) do
40
38
  section 'not_real'
41
39
  end
42
- }.to raise_error Ficus::ConfigError
40
+ end.to raise_error Ficus::ConfigError
43
41
 
44
- Ficus.log.count{|v| v =~ /^\[ERR\]/}.should eq 1
42
+ Ficus.errors.size.should eq 1
45
43
  end
46
44
  end
47
45
 
48
46
  it 'should load the config but fail to validate' do
49
47
  config_file do |config|
50
- expect {
48
+ expect do
51
49
  config = Ficus.load(config) do
52
50
  section 'general', :optional => true do
53
51
  required 'fake_param'
54
52
  end
55
53
  end
56
- }.to raise_error Ficus::ConfigError
57
- Ficus.log.count{|v| v =~ /^\[ERR\]/}.should eq 1
54
+ end.to raise_error Ficus::ConfigError
55
+ Ficus.errors.size.should eq 1
58
56
  end
59
57
  end
60
58
 
@@ -100,7 +98,7 @@ describe Ficus do
100
98
  }
101
99
 
102
100
  config_file(hash) do |config|
103
- expect {
101
+ expect do
104
102
  Ficus.load(config) do
105
103
  section 'subsections' do
106
104
  section /^section/ do
@@ -108,7 +106,7 @@ describe Ficus do
108
106
  end
109
107
  end
110
108
  end
111
- }.to raise_error Ficus::ConfigError
109
+ end.to raise_error Ficus::ConfigError
112
110
  end
113
111
  end
114
112
 
@@ -121,7 +119,7 @@ describe Ficus do
121
119
  }
122
120
 
123
121
  config_file(hash) do |config|
124
- expect {
122
+ expect do
125
123
  Ficus.load(config) do
126
124
  section 'subsections' do
127
125
  section :all do
@@ -129,7 +127,7 @@ describe Ficus do
129
127
  end
130
128
  end
131
129
  end
132
- }.to raise_error Ficus::ConfigError
130
+ end.to raise_error Ficus::ConfigError
133
131
  end
134
132
  end
135
133
 
@@ -137,8 +135,11 @@ describe Ficus do
137
135
  Tempfile.open('config.yml') do |config|
138
136
  config.write hash.to_yaml
139
137
  config.close
140
-
141
138
  yield config.path
142
139
  end
143
140
  end
141
+
142
+ def logger
143
+ Logger.new(STDOUT).tap { |log| log.level = Logger::FATAL }
144
+ end
144
145
  end
@@ -1,9 +1,9 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__), '..')
2
2
 
3
3
  require 'rspec'
4
+ require 'logger'
4
5
 
5
6
  if RUBY_VERSION.to_f >= 1.9
6
- puts 'Enabling coverage'
7
7
  require 'simplecov'
8
8
  SimpleCov.add_filter 'vendor'
9
9
  SimpleCov.add_filter 'spec'
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ficus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Drew Fradette
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-11 00:00:00.000000000 Z
11
+ date: 2013-10-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,49 +27,43 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: simplecov
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: A runtime validation configuration DSL
@@ -89,39 +80,35 @@ files:
89
80
  - Rakefile
90
81
  - ficus.gemspec
91
82
  - lib/ficus.rb
83
+ - lib/ficus/dsl.rb
84
+ - lib/ficus/exceptions.rb
85
+ - lib/ficus/loader.rb
92
86
  - lib/recursive-open-struct.rb
93
87
  - spec/ficus_spec.rb
94
88
  - spec/spec_helper.rb
95
89
  homepage: https://github.com/drewfradette/ruby-ficus
96
90
  licenses:
97
91
  - MIT
92
+ metadata: {}
98
93
  post_install_message:
99
94
  rdoc_options: []
100
95
  require_paths:
101
96
  - lib
102
97
  required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
98
  requirements:
105
- - - ! '>='
99
+ - - '>='
106
100
  - !ruby/object:Gem::Version
107
101
  version: '0'
108
- segments:
109
- - 0
110
- hash: 248691854082786453
111
102
  required_rubygems_version: !ruby/object:Gem::Requirement
112
- none: false
113
103
  requirements:
114
- - - ! '>='
104
+ - - '>='
115
105
  - !ruby/object:Gem::Version
116
106
  version: '0'
117
- segments:
118
- - 0
119
- hash: 248691854082786453
120
107
  requirements: []
121
108
  rubyforge_project:
122
- rubygems_version: 1.8.25
109
+ rubygems_version: 2.0.3
123
110
  signing_key:
124
- specification_version: 3
111
+ specification_version: 4
125
112
  summary: ''
126
113
  test_files:
127
114
  - spec/ficus_spec.rb