lab42_config 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7807c37c337523025ea0c138b49835f28c43951
4
- data.tar.gz: 6b392b4c90ad4a8fd700d09e4ec458e4bce10aa5
3
+ metadata.gz: 64b84267e5b2b195d488a712cb28076a64fd234d
4
+ data.tar.gz: df2c418509d5288734b27f6c451aa6e8e8a634c4
5
5
  SHA512:
6
- metadata.gz: 612ad33c38bf8d82bc9bddc9d647ec8b3084f1625bf9c50bdc5bdf0a1a86d8d6a1cdc9cf461666458c7a0eda83aa6d81bfbb557052f9f2c7a80551a64bcf085b
7
- data.tar.gz: c51dd4e9c0b52f2f0042f851d3ad6581b94d740ee30c0ad34275bf8a694bfa3ebe67121d0a9407b4b2b43c6c4f520d2175849af387b8ce66d45eaa6923244aa9
6
+ metadata.gz: 08304305fec3ecbfbe0f7a8d26db2b8a7d497585d969656aeecd0522719ef85b26d9312f08e64f5543b5c3b291a60e6ea4cf48bab0dc63664655a34d69ff6b47
7
+ data.tar.gz: c6487500c3c04cbccc4d0591ce73113ce7c9b44009004f17cfe0298b8bea6a802f073f331fd09a02daf2cfee81dc5c7a4945f1ea76cf6d1b756aea59039dba60
data/README.md CHANGED
@@ -1,8 +1,19 @@
1
1
  # lab42\_config
2
2
 
3
+ [![Build Status](https://travis-ci.org/RobertDober/lab42_config.svg?branch=master)](https://travis-ci.org/RobertDober/lab42_config)
4
+
3
5
  Refactor your configuration away
4
6
 
5
7
  ## Configure a Class
6
8
 
7
- For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_config/blob/master/demo/class.md).
9
+ ```ruby
10
+ class Commiter
11
+ include Lab42::Config
12
+ config do
13
+ dvs "github"
14
+ status :commiter
15
+ end
16
+ end
17
+ ```
8
18
 
19
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_config/blob/master/demo/class.md).
@@ -1,4 +1,5 @@
1
1
  require_relative 'config/class_methods'
2
+ require_relative 'config/class_level_config'
2
3
 
3
4
  module Lab42
4
5
  module Config
@@ -7,7 +8,7 @@ module Lab42
7
8
  into.extend ClassMethods
8
9
  end
9
10
  def configuration
10
- self.class.__config__
11
+ @_lab42_config__configuration__ = self.class.__config__.instance_level_config self
11
12
  end
12
13
 
13
14
  end # module Config
@@ -0,0 +1,51 @@
1
+ require_relative 'instance_level_config'
2
+
3
+ require 'forwarder'
4
+
5
+ module Lab42
6
+ module Config
7
+ Lazy = Struct.new :behavior do
8
+ def to_proc; behavior end
9
+ end
10
+
11
+ class Values
12
+ extend Forwarder
13
+ forward_all :[], :[]=, :fetch, to: :__values__
14
+
15
+ def get_with_context key, rcv
16
+ val = fetch( key ){ raise NoMethodError, "undefined method `#{key}'" }
17
+ if Lazy === val
18
+ rcv.instance_exec &val
19
+ else
20
+ val
21
+ end
22
+ end
23
+
24
+ private
25
+ def __values__
26
+ @__values__ ||= {}
27
+ end
28
+ end
29
+
30
+ class ClassLevelConfig < BasicObject
31
+
32
+ def instance_level_config rcv
33
+ InstanceLevelConfig.new __values__, rcv
34
+ end
35
+
36
+
37
+ private
38
+ def method_missing name, *args, &blk
39
+ super if args.size > 1 || ( args.empty? && blk.! )
40
+
41
+ __values__[ name ] = args.first || Lazy.new( blk )
42
+ end
43
+
44
+ def __values__
45
+ @__values__ ||= Values.new
46
+ end
47
+
48
+
49
+ end # class Instance
50
+ end # module Config
51
+ end # module Lab42
@@ -1,14 +1,30 @@
1
- require_relative 'instance'
1
+ require_relative 'class_level_config'
2
2
 
3
3
  module Lab42
4
4
  module Config
5
5
  module ClassMethods
6
6
  def config &blk
7
7
  __config__.instance_exec &blk
8
- __config__.freeze!
9
8
  end
10
9
  def __config__
11
- @__config__ ||= Lab42::Config::Instance.new
10
+ @__config__ ||= Lab42::Config::ClassLevelConfig.new
11
+ end
12
+
13
+ def instance_config *names
14
+ names.each do | name |
15
+ ivar_name = "@__lab42_config_local__#{name}__"
16
+ # define getter
17
+ define_method name do
18
+ instance_variable_defined?( ivar_name ) ?
19
+ instance_variable_get( ivar_name ) :
20
+ instance_variable_set( ivar_name, configuration.__send__( name ) )
21
+ end
22
+ # define setter
23
+ define_method "#{name}=" do |val|
24
+ instance_variable_set( ivar_name, val )
25
+ end
26
+
27
+ end
12
28
  end
13
29
  end # module ClassMethods
14
30
  end # module Config
@@ -0,0 +1,21 @@
1
+ require 'forwarder'
2
+
3
+ module Lab42
4
+ module Config
5
+
6
+ class InstanceLevelConfig < BasicObject
7
+
8
+ private
9
+ def initialize values, receiver
10
+ @values = values
11
+ @receiver = receiver
12
+ end
13
+
14
+ def method_missing name, *args, &blk
15
+ super unless args.empty? && blk.!
16
+ @values.get_with_context name, @receiver
17
+ end
18
+
19
+ end # class Instance
20
+ end # module Config
21
+ end # module Lab42
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module Config
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end # module Lab42
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarder2
@@ -118,8 +118,9 @@ files:
118
118
  - LICENSE
119
119
  - README.md
120
120
  - lib/lab42/config.rb
121
+ - lib/lab42/config/class_level_config.rb
121
122
  - lib/lab42/config/class_methods.rb
122
- - lib/lab42/config/instance.rb
123
+ - lib/lab42/config/instance_level_config.rb
123
124
  - lib/lab42/config/version.rb
124
125
  homepage: https://github.com/RobertDober/lab42_config
125
126
  licenses:
@@ -1,23 +0,0 @@
1
- module Lab42
2
- module Config
3
- class Instance < BasicObject
4
-
5
- def freeze!
6
- __values__.freeze
7
- end
8
- private
9
- def method_missing name, *args, &blk
10
- super if args.size > 1
11
- return __values__.fetch( name ){ super } if args.empty? && blk.!
12
-
13
- __values__[ name ] = args.first || instance_exec(&blk)
14
- end
15
-
16
- def __values__
17
- @__values__ ||= {}
18
- end
19
-
20
-
21
- end # class Instance
22
- end # module Config
23
- end # module Lab42