configur 1.0.1 → 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.
- checksums.yaml +4 -4
- data/README.md +23 -5
- data/lib/configur.rb +12 -9
- data/spec/configur_spec.rb +55 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6698a8297a088e84fa914a38a43c6e5c38817ca
|
4
|
+
data.tar.gz: 614cc98e6c27554e001e4b44f7ce31aed8272650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34edbee88944d39e76b77cbbf54f2e0237ac26bb8be8af8c90124f5d17d2b3a68fa14e99f7cfc1463c1e80a91f16b1c0cc15fb331c8edcc86d885b194c5da388
|
7
|
+
data.tar.gz: fc73ac7e23d6080544cf1bd2be9af947bd7684cf19e4260bbd5a4e2655a369d1cdb206590834394bb4aec114c835f652f7d7b810e1bdcd001b9948cec70dd9b1
|
data/README.md
CHANGED
@@ -22,23 +22,41 @@ No explanation, just an example to show you how easy it is:
|
|
22
22
|
|
23
23
|
require 'configur'
|
24
24
|
|
25
|
-
|
25
|
+
module MyModule
|
26
26
|
extend Configur
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
MyModule.configur do |config|
|
30
30
|
config.debug = true
|
31
31
|
config.app_name = 'myawesomeapp'
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
For a class, here is the corresponding snippet
|
35
|
+
|
36
|
+
require 'configur'
|
37
|
+
|
38
|
+
class MyClass
|
39
|
+
include Configur
|
40
|
+
end
|
41
|
+
|
42
|
+
my_class = MyClass.new
|
43
|
+
|
44
|
+
my_class.configur do |config|
|
45
|
+
config.debug = true
|
46
|
+
config.app_name = 'myawesomeapp'
|
47
|
+
end
|
35
48
|
|
36
49
|
## Methods
|
37
50
|
|
38
51
|
After configuring your class or module, you can "query" with the following methods:
|
39
52
|
|
40
|
-
puts
|
41
|
-
puts
|
53
|
+
puts MyModule.get_config :app_name
|
54
|
+
puts MyModule.get_configs
|
55
|
+
|
56
|
+
or
|
57
|
+
|
58
|
+
puts my_class.get_config :app_name
|
59
|
+
puts my_class.get_configs
|
42
60
|
|
43
61
|
Easy, isn't it?
|
44
62
|
|
data/lib/configur.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
# File: configur.rb
|
4
|
-
# Time-stamp: <2014-11-
|
4
|
+
# Time-stamp: <2014-11-27 16:26:10 pierre>
|
5
5
|
# Copyright (C) 2014 Pierre Lecocq
|
6
6
|
# Description: Configur gem module
|
7
7
|
|
8
8
|
# Configur module
|
9
9
|
module Configur
|
10
10
|
# Version constant
|
11
|
-
VERSION = [1,
|
11
|
+
VERSION = [1, 1, 0].join '.'
|
12
12
|
|
13
13
|
# Configur
|
14
14
|
def configur(&block)
|
15
|
-
@@
|
15
|
+
@@_cdata ||= {}
|
16
|
+
@@_oid = self.object_id
|
16
17
|
|
17
18
|
def block.method_missing(m, *args, &block)
|
18
|
-
name = m.to_s
|
19
|
-
|
19
|
+
name = m.to_s
|
20
|
+
super if name[-1] != '='
|
21
|
+
Configur.set_config @@_oid, name.gsub('=', ''), args[0]
|
20
22
|
end
|
21
23
|
|
22
24
|
yield block
|
@@ -24,16 +26,17 @@ module Configur
|
|
24
26
|
|
25
27
|
# Get all configs
|
26
28
|
def get_configs
|
27
|
-
@@
|
29
|
+
@@_cdata[self.object_id] || {}
|
28
30
|
end
|
29
31
|
|
30
32
|
# Get a config value
|
31
33
|
def get_config(name)
|
32
|
-
@@
|
34
|
+
@@_cdata[self.object_id][name.to_sym] || nil
|
33
35
|
end
|
34
36
|
|
35
37
|
# Set a config value
|
36
|
-
def self.set_config(name, value)
|
37
|
-
@@
|
38
|
+
def self.set_config(oid, name, value)
|
39
|
+
@@_cdata[oid] ||= {}
|
40
|
+
@@_cdata[oid][name.to_sym] = value
|
38
41
|
end
|
39
42
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# File: configur_spec.rb
|
4
|
+
# Time-stamp: <2014-11-27 16:33:40 pierre>
|
5
|
+
# Copyright (C) 2014 Pierre Lecocq
|
6
|
+
# Description: Configure spec test file
|
7
|
+
|
8
|
+
require_relative '../lib/configur'
|
9
|
+
|
10
|
+
describe Configur do
|
11
|
+
describe 'Module configuration' do
|
12
|
+
# Set module
|
13
|
+
my_test_module = Module.new
|
14
|
+
my_test_module.extend Configur
|
15
|
+
|
16
|
+
# Test extension
|
17
|
+
it { expect(my_test_module.methods).to include :configur }
|
18
|
+
|
19
|
+
# Set module configuration
|
20
|
+
my_test_module.configur do |c|
|
21
|
+
c.name = 'My test module'
|
22
|
+
c.autor = 'Pierre Lecocq'
|
23
|
+
end
|
24
|
+
|
25
|
+
# Test configuration queries
|
26
|
+
it {expect(my_test_module.get_config :name).to eql 'My test module'}
|
27
|
+
it {expect(my_test_module.get_config :unknown).to be nil }
|
28
|
+
it {expect(my_test_module.get_configs).to be_a Hash }
|
29
|
+
it {expect(my_test_module.get_configs.keys.length).to eql 2 }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'Class configuration' do
|
33
|
+
# Set class
|
34
|
+
|
35
|
+
class MyClass
|
36
|
+
include Configur
|
37
|
+
end
|
38
|
+
my_test_class = MyClass.new
|
39
|
+
|
40
|
+
# Test extension
|
41
|
+
it { expect(my_test_class.methods).to include :configur }
|
42
|
+
|
43
|
+
# Set class configuration
|
44
|
+
my_test_class.configur do |c|
|
45
|
+
c.name = 'My test class'
|
46
|
+
c.autor = 'Pierre Lecocq'
|
47
|
+
end
|
48
|
+
|
49
|
+
# Test configuration queries
|
50
|
+
it {expect(my_test_class.get_config :name).to eql 'My test class'}
|
51
|
+
it {expect(my_test_class.get_config :unknown).to be nil }
|
52
|
+
it {expect(my_test_class.get_configs).to be_a Hash }
|
53
|
+
it {expect(my_test_class.get_configs.keys.length).to eql 2 }
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configur
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Lecocq
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- README.md
|
22
22
|
- configur.gemspec
|
23
23
|
- lib/configur.rb
|
24
|
+
- spec/configur_spec.rb
|
24
25
|
homepage: https://github.com/pierre-lecocq/configur
|
25
26
|
licenses:
|
26
27
|
- MIT
|