ficus 0.0.2 → 0.0.3

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 CHANGED
@@ -1,3 +1,6 @@
1
+ .rspec
2
+ vendor
3
+ .idea
1
4
  *.gem
2
5
  *.rbc
3
6
  .bundle
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  A simple YAML configuration DSL that does runtime validation.
4
4
 
5
+ ## Important Note
6
+ This uses the [`recursive-open-struct`](https://github.com/aetherknight/recursive-open-struct) but due to gem missing a [crucial fix](https://github.com/aetherknight/recursive-open-struct/commit/0c16caa1b9a19d12e97829f02083f0b7d21f0100)
7
+ I have simply added the latest version with the fix to the `lib`. When it's fixed in rubygems, I will add it as a gem dependency.
8
+
5
9
  ## Installation
6
10
 
7
11
  Add this line to your application's Gemfile:
@@ -32,6 +36,13 @@ section_2:
32
36
 
33
37
  optional_section:
34
38
  key5: value5
39
+
40
+ pattern_section_1:
41
+ key6: value6
42
+
43
+ pattern_section_2:
44
+ key6: value6
45
+
35
46
  ```
36
47
 
37
48
  And now we can use Ficus to load the config and validate it at the same time.
@@ -58,6 +69,10 @@ config = Ficus.load 'config.yml' do
58
69
  section 'not_defined', :optional => true do
59
70
  require 'key6'
60
71
  end
72
+
73
+ section /^pattern_section_.+/ do
74
+ require 'key6'
75
+ end
61
76
  end
62
77
 
63
78
  config.section_1.key1 # value1
data/Rakefile CHANGED
@@ -1 +1,3 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
@@ -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.2'
7
+ spec.version = '0.0.3'
8
8
  spec.authors = ["Drew Fradette"]
9
9
  spec.email = ["drew.fradette@gmail.com"]
10
10
  spec.description = 'A runtime validation configuration DSL'
@@ -15,7 +15,7 @@ class Ficus < RecursiveOpenStruct
15
15
  errors = log.select{|v| v =~ /^\[ERR\]/}
16
16
  if errors.size > 0
17
17
  log.each{|v| puts v} if ENV['DEBUG']
18
- raise ConfigError.new("Unable to start due to invalid settings")
18
+ raise ConfigError.new('Unable to start due to invalid settings')
19
19
  end
20
20
  config
21
21
  end
@@ -26,13 +26,26 @@ class Ficus < RecursiveOpenStruct
26
26
  end
27
27
 
28
28
  def section(name, args = {}, &block)
29
- section = self.send(name)
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?
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!
36
49
  end
37
50
  end
38
51
 
@@ -70,9 +70,72 @@ describe Ficus do
70
70
  end
71
71
  end
72
72
 
73
- def config_file
73
+ it 'should validate conformant subsections' do
74
+ hash = {
75
+ :subsections => {
76
+ :section1 => {:key1=>'value1'},
77
+ :section2 => {:key1=>'value1'},
78
+ }
79
+ }
80
+
81
+ config_file(hash) do |config|
82
+ config = Ficus.load(config) do
83
+ section 'subsections' do
84
+ section /^section/ do
85
+ required :key1
86
+ end
87
+ end
88
+ end
89
+ config.subsections.section1.key1.should eq 'value1'
90
+ config.subsections.section2.key1.should eq 'value1'
91
+ end
92
+ end
93
+
94
+ it 'should invalidate nonconformant subsections' do
95
+ hash = {
96
+ :subsections => {
97
+ :section1 => {:key1=>'value1'},
98
+ :section2 => {},
99
+ }
100
+ }
101
+
102
+ config_file(hash) do |config|
103
+ expect {
104
+ Ficus.load(config) do
105
+ section 'subsections' do
106
+ section /^section/ do
107
+ required :key1
108
+ end
109
+ end
110
+ end
111
+ }.to raise_error Ficus::ConfigError
112
+ end
113
+ end
114
+
115
+ it 'should validate all subsections' do
116
+ hash = {
117
+ :subsections => {
118
+ :section1 => {:key1=>'value1'},
119
+ :section2 => {},
120
+ }
121
+ }
122
+
123
+ config_file(hash) do |config|
124
+ expect {
125
+ Ficus.load(config) do
126
+ section 'subsections' do
127
+ section :all do
128
+ required :key1
129
+ end
130
+ end
131
+ end
132
+ }.to raise_error Ficus::ConfigError
133
+ end
134
+ end
135
+
136
+ def config_file(hash=@config)
74
137
  Tempfile.open('config.yml') do |config|
75
- config.write @config.to_yaml
138
+ config.write hash.to_yaml
76
139
  config.close
77
140
 
78
141
  yield config.path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ficus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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: 2013-10-09 00:00:00.000000000 Z
12
+ date: 2013-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -107,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  segments:
109
109
  - 0
110
- hash: -571402621650926821
110
+ hash: 248691854082786453
111
111
  required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
113
113
  requirements:
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  segments:
118
118
  - 0
119
- hash: -571402621650926821
119
+ hash: 248691854082786453
120
120
  requirements: []
121
121
  rubyforge_project:
122
122
  rubygems_version: 1.8.25