configdsl 1.0.0 → 1.1.0
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/README.md +41 -6
- data/configdsl.gemspec +4 -4
- data/examples/example.rb +1 -1
- data/examples/example_lazy.rb +37 -0
- data/examples/test.cdsl.rb +1 -1
- data/lib/configdsl/version.rb +2 -2
- data/lib/configdsl.rb +91 -6
- metadata +10 -13
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ConfigDSL
|
2
2
|
|
3
|
-
A tasty DSL for configuration files. Get rid of those
|
3
|
+
A tasty Ruby-based DSL for configuration files. Get rid of those silly YAML and JSON configs.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -43,7 +43,7 @@ list do
|
|
43
43
|
three_two one + " 2"
|
44
44
|
three_three "three"
|
45
45
|
end
|
46
|
-
see "
|
46
|
+
see "will_be_overwritten"
|
47
47
|
end
|
48
48
|
|
49
49
|
list 5 do |index|
|
@@ -184,15 +184,51 @@ puts ; puts "Program was stared at: "
|
|
184
184
|
p ConfigDSL[:starting_time]
|
185
185
|
```
|
186
186
|
|
187
|
+
#### Keys as methods
|
188
|
+
|
189
|
+
You can access all config values using method chaining syntax instead of using the square brackets.
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
ConfigDSL[:list][:three][:three_two]
|
193
|
+
```
|
194
|
+
|
195
|
+
is equal to
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
ConfigDSL.list.three.three_two
|
199
|
+
```
|
200
|
+
|
201
|
+
However, there is a little problem with this approach.
|
202
|
+
Data container objects do have their own methods besides the methods that they provide to get data values.
|
203
|
+
|
204
|
+
Notice that `ConfigDSL.data` is also a reserved method (it returns internal first-level data container).
|
205
|
+
|
187
206
|
#### Check out the example!
|
188
207
|
|
189
208
|
There is an `examples` dir. Check it out to see how it works for yourself!
|
190
209
|
|
191
|
-
###
|
210
|
+
### Lazy Values
|
211
|
+
|
212
|
+
You can have lazy values in your configuration code! By default, those values are evaluated when you call them for the first time.
|
213
|
+
This can be very useful when you want to load config before everything else. Storing some config data in the database? This is for you.
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
ConfigDSL.execute do
|
217
|
+
lazy_varaible lazy!{ Time.now }
|
218
|
+
standart_variable Time.now
|
219
|
+
end
|
220
|
+
|
221
|
+
sleep 2
|
222
|
+
|
223
|
+
ConfigDSL.standart_variable # => 2012-11-11 21:37:09 +0400
|
224
|
+
ConfigDSL.lazy_varaible # => 2012-11-11 21:37:11 +0400
|
225
|
+
```
|
226
|
+
|
227
|
+
Can be tricky! Make sure you understand what Lazy Values do and what they don't do!
|
192
228
|
|
193
|
-
|
194
|
-
Just make sure to require it before you read the first value.
|
229
|
+
### Use Hashie::Mash - Deprecated!
|
195
230
|
|
231
|
+
Hashie::Mash can not be used as a layer-level storage anymore (because of lazy values implementation).
|
196
232
|
|
197
233
|
### Ruby on Rails
|
198
234
|
|
@@ -201,7 +237,6 @@ To be implemented. For now you can use it like standalone as initializer.
|
|
201
237
|
### To-Do List
|
202
238
|
|
203
239
|
- Ruby on Rails integration
|
204
|
-
- Lazy config values
|
205
240
|
|
206
241
|
## Contributing
|
207
242
|
|
data/configdsl.gemspec
CHANGED
@@ -4,16 +4,16 @@ require File.expand_path('../lib/configdsl/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["MOZGIII"]
|
6
6
|
gem.email = ["mike-n@narod.ru"]
|
7
|
-
gem.description = %q{A convinient DSL for your app configuration!}
|
8
|
-
gem.summary = %q{A convinient DSL for your app configuration!}
|
9
|
-
gem.homepage = ""
|
7
|
+
gem.description = %q{A convinient Ruby-based DSL for your app configuration!}
|
8
|
+
gem.summary = %q{A convinient Ruby-based DSL for your app configuration!}
|
9
|
+
gem.homepage = "http://github.com/MOZGIII/ConfigDSL"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
13
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
14
|
gem.name = "configdsl"
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version =
|
16
|
+
gem.version = ConfigDSL::VERSION
|
17
17
|
|
18
18
|
gem.add_runtime_dependency "activesupport"
|
19
19
|
end
|
data/examples/example.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
$: << "../lib"
|
2
|
+
require 'configdsl'
|
3
|
+
|
4
|
+
ConfigDSL.execute do
|
5
|
+
lazy_varaible lazy!{ Time.now }
|
6
|
+
standart_variable Time.now
|
7
|
+
|
8
|
+
lower_level_also_works do
|
9
|
+
lazy_time lazy!{ Time.now }
|
10
|
+
end
|
11
|
+
|
12
|
+
delta lazy!(Time.now){ |config_reading_time| Time.now.to_i - config_reading_time.to_i }
|
13
|
+
end
|
14
|
+
|
15
|
+
# We sleep for time to change
|
16
|
+
sleep 2
|
17
|
+
|
18
|
+
print "Standart: "
|
19
|
+
p ConfigDSL.standart_variable
|
20
|
+
|
21
|
+
print "Lazy: "
|
22
|
+
p ConfigDSL.lazy_varaible
|
23
|
+
|
24
|
+
print "Lazy (another way to get): "
|
25
|
+
p ConfigDSL[:lazy_varaible]
|
26
|
+
|
27
|
+
print "Lazy (force LazyValue object instead of its content): "
|
28
|
+
p ConfigDSL.original_reader(:lazy_varaible)
|
29
|
+
|
30
|
+
print "Non-toplevel lazy variables (only evaluated on request): "
|
31
|
+
p ConfigDSL.lower_level_also_works
|
32
|
+
|
33
|
+
print "And this value processed: "
|
34
|
+
p ConfigDSL.lower_level_also_works.lazy_time
|
35
|
+
|
36
|
+
print "Delta between lazy and standart Time.now (should -> 2): "
|
37
|
+
p ConfigDSL.delta
|
data/examples/test.cdsl.rb
CHANGED
data/lib/configdsl/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "1.
|
1
|
+
module ConfigDSL
|
2
|
+
VERSION = "1.1.0"
|
3
3
|
end
|
data/lib/configdsl.rb
CHANGED
@@ -4,14 +4,51 @@ require "forwardable"
|
|
4
4
|
|
5
5
|
module ConfigDSL
|
6
6
|
module Memory
|
7
|
+
class MemoryLayer < Hash
|
8
|
+
def self.make_alias_method(meth)
|
9
|
+
"original_#{meth}".to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
ALIASED_METHODS = [
|
13
|
+
:[], :fetch
|
14
|
+
]
|
15
|
+
|
16
|
+
ALIASED_METHODS.each do |meth|
|
17
|
+
original_method = make_alias_method(meth)
|
18
|
+
|
19
|
+
begin
|
20
|
+
alias_method original_method, meth
|
21
|
+
|
22
|
+
define_method meth do |*args, &block|
|
23
|
+
lazy_value = __send__(original_method, *args, &block)
|
24
|
+
lazy_value = lazy_value.value if lazy_value.kind_of?(LazyValue)
|
25
|
+
lazy_value
|
26
|
+
end
|
27
|
+
rescue NameError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Give a more handy name
|
32
|
+
alias_method :original_reader, make_alias_method(:[])
|
33
|
+
|
34
|
+
# Allow methods invocation to read values
|
35
|
+
def method_missing(meth, *args, &block)
|
36
|
+
return self[meth] if has_key?(meth)
|
37
|
+
super
|
38
|
+
end
|
39
|
+
|
40
|
+
# Allow methods invocation to read values, second part
|
41
|
+
def respond_to?(meth)
|
42
|
+
return true if has_key?(meth)
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
7
47
|
class << self
|
8
48
|
# Creates a new layer-level storage element
|
9
49
|
def layers_factory
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# Fallback to standart ruby Hash
|
14
|
-
Hash.new
|
50
|
+
@factory_base_class ||= MemoryLayer
|
51
|
+
@factory_base_class.new
|
15
52
|
end
|
16
53
|
|
17
54
|
# Main data container
|
@@ -61,7 +98,11 @@ module ConfigDSL
|
|
61
98
|
extend ActiveSupport::Concern
|
62
99
|
|
63
100
|
def self.debug?
|
64
|
-
false
|
101
|
+
@debug ||= false
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.debug!(value = true)
|
105
|
+
@debug = value
|
65
106
|
end
|
66
107
|
|
67
108
|
def context
|
@@ -72,6 +113,10 @@ module ConfigDSL
|
|
72
113
|
puts text if DSL.debug?
|
73
114
|
end
|
74
115
|
|
116
|
+
def lazy!(*args, &block)
|
117
|
+
LazyValue.new(block, args)
|
118
|
+
end
|
119
|
+
|
75
120
|
def varibales_hook(meth, *args, &block)
|
76
121
|
debug "Hooked #{meth}"
|
77
122
|
debug "Context is #{context}"
|
@@ -162,4 +207,44 @@ module ConfigDSL
|
|
162
207
|
data.respond_to?(meth)
|
163
208
|
end
|
164
209
|
end
|
210
|
+
|
211
|
+
class LazyValue
|
212
|
+
def self.default_options
|
213
|
+
{
|
214
|
+
caching: true
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
attr_reader :block, :args, :options
|
219
|
+
|
220
|
+
def initialize(block, args = [], options = {})
|
221
|
+
@block = block
|
222
|
+
@args = args
|
223
|
+
@options = self.class.default_options.merge(options)
|
224
|
+
@cached = false
|
225
|
+
end
|
226
|
+
|
227
|
+
def value(new_args = nil)
|
228
|
+
return @cache if caching? && cached?
|
229
|
+
value = block.call(*(new_args ? new_args : args))
|
230
|
+
if caching?
|
231
|
+
@cache = value
|
232
|
+
@cached = true
|
233
|
+
end
|
234
|
+
value
|
235
|
+
end
|
236
|
+
|
237
|
+
def caching?
|
238
|
+
!!options[:caching]
|
239
|
+
end
|
240
|
+
|
241
|
+
def flush_cache!
|
242
|
+
@cache = nil # clean pointer so that GC can do it's work immediately
|
243
|
+
@cached = true
|
244
|
+
end
|
245
|
+
|
246
|
+
def cached?
|
247
|
+
@cached
|
248
|
+
end
|
249
|
+
end
|
165
250
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configdsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-11 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirement: &28949232 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,13 +21,8 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
|
-
description: A convinient DSL for your app configuration!
|
24
|
+
version_requirements: *28949232
|
25
|
+
description: A convinient Ruby-based DSL for your app configuration!
|
31
26
|
email:
|
32
27
|
- mike-n@narod.ru
|
33
28
|
executables: []
|
@@ -41,10 +36,11 @@ files:
|
|
41
36
|
- Rakefile
|
42
37
|
- configdsl.gemspec
|
43
38
|
- examples/example.rb
|
39
|
+
- examples/example_lazy.rb
|
44
40
|
- examples/test.cdsl.rb
|
45
41
|
- lib/configdsl.rb
|
46
42
|
- lib/configdsl/version.rb
|
47
|
-
homepage:
|
43
|
+
homepage: http://github.com/MOZGIII/ConfigDSL
|
48
44
|
licenses: []
|
49
45
|
post_install_message:
|
50
46
|
rdoc_options: []
|
@@ -64,8 +60,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
60
|
version: '0'
|
65
61
|
requirements: []
|
66
62
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.8.
|
63
|
+
rubygems_version: 1.8.10
|
68
64
|
signing_key:
|
69
65
|
specification_version: 3
|
70
|
-
summary: A convinient DSL for your app configuration!
|
66
|
+
summary: A convinient Ruby-based DSL for your app configuration!
|
71
67
|
test_files: []
|
68
|
+
has_rdoc:
|