Skip to content

Commit e457d52

Browse files
committed
misc
1 parent fb2f22c commit e457d52

9 files changed

+891
-169
lines changed

.bundle/config

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
---
2-
BUNDLE_DISABLE_SHARED_GEMS: '1'
1+
--- {}

Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
source 'https://rubygems.org'
22

3-
# Specify your gem's dependencies in iqeo-conf.gemspec
3+
gem 'rake'
4+
5+
# Specify your gem's dependencies in iqeo.gemspec
46
gemspec

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
rake (0.9.2.2)
910

1011
PLATFORMS
1112
ruby
1213

1314
DEPENDENCIES
1415
iqeo-conf!
16+
rake

LICENSE

+674-22
Large diffs are not rendered by default.

README.md

+57-116
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,95 @@
1-
# Iqeo::Conf
1+
# Iqeo::Configuration
22

3-
TODO: Write a gem description
3+
A DSL for writing configuration files.
44

55
## Installation
66

77
Add this line to your application's Gemfile:
88

9-
gem 'iqeo-conf'
9+
...
10+
gem 'iqeo-conf'
11+
...
1012

1113
And then execute:
1214

13-
$ bundle
15+
...
16+
$ bundle
17+
...
1418

1519
Or install it yourself as:
1620

17-
$ gem install iqeo-conf
21+
...
22+
$ gem install iqeo-conf
23+
...
1824

1925
## Usage
2026

21-
TODO: Write usage instructions here
22-
23-
## Contributing
24-
25-
1. Fork it
26-
2. Create your feature branch (`git checkout -b my-new-feature`)
27-
3. Commit your changes (`git commit -am 'Added some feature'`)
28-
4. Push to the branch (`git push origin my-new-feature`)
29-
5. Create new Pull Request
27+
### Use a Configuration object directly
3028

31-
# example conf DSL
32-
33-
conf = Conf.new 'name_0' do
34-
35-
# values
36-
37-
var_str "String"
38-
var_int 123
39-
var_arr [ 1, 2, 3]
40-
var_hsh { :a => 1, :b => 2, :c => 3 }
41-
42-
# sub configs in different ways
43-
44-
var_conf_1 Conf.new 'name_1' do
45-
...
46-
end
47-
48-
var_conf_2 Conf.new do
49-
...
50-
end
51-
52-
var_conf_3 'name_3' do
53-
...
54-
end
55-
56-
var_conf_4 do
57-
...
58-
end
59-
60-
level 1
61-
62-
var_conf_5 'name_5' do
63-
64-
level 2
65-
66-
var_conf_6 'name_6' do
29+
...ruby
30+
conf = Iqeo::Configuration.new
31+
...
6732

68-
level 3
33+
Add some settings
6934

70-
var_conf_7 'name_7' do
35+
...ruby
36+
conf.one 1
37+
conf.two 2
38+
...
7139

72-
# Conf.inherit ?
73-
level 4
40+
Retrieve settings
7441

75-
end
42+
...ruby
43+
conf.one => 1
44+
conf.two => 2
45+
...
7646

77-
end
47+
### Configuration DSL - builder style
7848

79-
end
49+
...ruby
50+
conf = Iqeo::Configuration do |c|
8051

81-
other Conf.load 'file...' # etc...
52+
c.three 3
53+
c.four 4
8254

8355
end
56+
...
8457

58+
Retrieve settings
8559

86-
# include config files
87-
88-
## return either a single Conf hierarchy or an array of Conf depending upon the file contents
89-
90-
Conf.path = [ '*' , '**/*.rb', "config/config.rb" ]
91-
Conf.load => Conf... # no parameter defaults to Conf::path value
92-
Conf.load filename => Conf...
93-
Conf.load glob => Conf... / [Conf..., ...]
94-
Conf.load [filename,...] => Conf... / [Conf..., ...]
95-
Conf.load [glob,...] => Conf... / [Conf..., ...]
96-
97-
# usage
98-
99-
## retrieve values
100-
101-
conf.name => 'name_0'
102-
conf.var_str => "String"
103-
conf.var_int => 123
104-
conf.var_arr => [1,2,3]
105-
conf.var_hsh => { :a => 1, :b => 2, :c => 3 }
106-
107-
## retrieve sub-configurations
108-
109-
conf.var_conf_1 => Conf...
110-
conf.var_conf_1.name => 'name_1'
111-
112-
## nested configurations
113-
114-
conf.level => 1
115-
conf.var_conf_5.level => 2
116-
conf.var_conf_5.var_conf_6.level => 3
117-
conf.var_conf_5.var_conf_6.var_conf_7.level => 4
118-
119-
## set or create values
120-
121-
conf.existing_item "change to this"
122-
conf.new_item "new value"
60+
...ruby
61+
conf.three => 3
62+
conf.four => 4
63+
...
12364

124-
## indifferent keys
65+
### Configuration DSL - freestyle
12566

126-
conf.name => 'name_0'
127-
conf[:name] => 'name_0'
128-
conf['name'] => 'name_0'
67+
...ruby
68+
conf = Iqeo::Configuration do
12969

130-
## enumeration
70+
five 5
71+
six 6
13172

132-
conf.values => # array of values like a hash
73+
end
74+
...
13375

134-
conf.keys => # array of string keys
135-
conf.keys_as_strings => # array of string keys
136-
conf.keys_as_symbols => # array of symbol keys
76+
Retrieve settings
13777

138-
conf.to_hash => # hash of string key + value pairs
139-
conf.to_hash_with_strings => # hash of string key + value pairs
140-
conf.to_hash_with_symbols => # hash of symbol key + value pairs
78+
...ruby
79+
conf.five => 5
80+
conf.six => 6
81+
...
14182

142-
conf.each do |key,value| # key will be string
143-
puts key + " = " + value
144-
end
83+
## Contributing
14584

146-
conf.each_with_strings |key,value| # key will be string
147-
...
85+
1. Fork it
86+
2. Create your feature branch (`git checkout -b my-new-feature`)
87+
3. Commit your changes (`git commit -am 'Added some feature'`)
88+
4. Push to the branch (`git push origin my-new-feature`)
89+
5. Create new Pull Request
14890

149-
conf.each_with_symbols |key,value| # key will be hash
150-
...
91+
## License
15192

152-
# how about hashes with keys other than strings and symbols ?
153-
# conf.to_hash { |k| k.method ... }
93+
Licensed under GPL Version 3 license
94+
See LICENSE file
15495

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
#!/usr/bin/env rake
22
require "bundler/gem_tasks"
3+
require "rake/testtask"
4+
5+
Rake::TestTask.new do |t|
6+
t.libs << "test"
7+
t.test_files = FileList['test/test*.rb']
8+
t.verbose = true
9+
end

iqeo-conf.gemspec

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# -*- encoding: utf-8 -*-
2-
require File.expand_path('../lib/iqeo-conf/version', __FILE__)
2+
require File.expand_path('../lib/configuration/version', __FILE__)
33

44
Gem::Specification.new do |gem|
5-
gem.authors = ["TODO: Write your name"]
6-
gem.email = ["TODO: Write your email address"]
7-
gem.description = %q{TODO: Write a gem description}
5+
gem.authors = ["Gerard Fowley"]
6+
gem.email = ["[email protected]"]
7+
gem.description = %q{A flexible configuration DSL}
88
gem.summary = %q{TODO: Write a gem summary}
99
gem.homepage = ""
1010

1111
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
1212
gem.files = `git ls-files`.split("\n")
1313
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14-
gem.name = "iqeo-conf"
14+
gem.name = "iqeo"
1515
gem.require_paths = ["lib"]
16-
gem.version = Iqeo::Conf::VERSION
16+
gem.version = Iqeo::Configuration::VERSION
1717
end

lib/iqeo/configuration.rb

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
module Iqeo
2-
3-
module Conf
41

5-
class Configuration
2+
require_relative "configuration/version"
63

7-
# todo: tests - init without block, with block & param, with plain block
4+
module Iqeo
85

9-
def initialize &block
10-
@items = {}
11-
if block_given?
12-
if block.arity == 1
13-
yield self
14-
else
15-
instance_eval &block
16-
end
17-
end
18-
end
6+
class Configuration
197

20-
# todo: tests - with value, without value, multiple values ?, optional '=' with value ?
8+
# todo: tests - init without block, with block & param, with plain block
219

22-
def method_missing name, *value
23-
if value.empty?
24-
@items[name]
25-
else
26-
@items[name] = value.first
27-
end
10+
def initialize &block
11+
@items = {}
12+
if block_given?
13+
if block.arity == 1
14+
yield self
15+
else
16+
instance_eval &block
17+
end
2818
end
19+
end
20+
21+
# todo: tests - with value, without value, multiple values ?, optional '=' with value ?
2922

23+
def method_missing name, *value
24+
name = name.to_s.chomp('=').to_sym
25+
return @items[name] if value.empty?
26+
@items[name] = value.first
3027
end
3128

3229
end

0 commit comments

Comments
 (0)