mutaconf 0.0.7 → 0.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.
@@ -1 +1 @@
1
- 2.0.0
1
+ 1.9.3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.1.0
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Mutaconf
4
- VERSION = '0.0.7'
4
+ VERSION = '0.1.0'
5
5
 
6
6
  def self.dsl *args
7
7
  DSL.new *args
@@ -2,12 +2,45 @@
2
2
  module Mutaconf
3
3
 
4
4
  class DSL
5
- attr_accessor :keys, :target, :lenient
5
+ attr_accessor :lenient
6
6
 
7
7
  def initialize options = {}
8
- @keys ||= options[:keys] || [] # TODO: spec pre-set keys in parent class
9
- @target = Target.new options[:target]
8
+
9
+ @attr_targets = {}
10
+ @proxy_targets = {}
10
11
  @lenient = options[:lenient] if options.key?(:lenient)
12
+
13
+ if options[:attrs]
14
+ options[:attrs].each_pair do |target,attrs|
15
+ if !!attrs == attrs
16
+ @attr_targets.default = Target.new target
17
+ else
18
+ [ attrs ].flatten.each do |attr|
19
+ if !!attr == attr
20
+ @attr_targets.default = Target.new target
21
+ else
22
+ @attr_targets[attr] = Target.new target
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ if options[:proxy]
30
+ options[:proxy].each_pair do |target,attrs|
31
+ if !!attrs == attrs
32
+ @proxy_targets.default = target
33
+ else
34
+ [ attrs ].flatten.each do |attr|
35
+ if !!attr == attr
36
+ @proxy_targets.default = target
37
+ else
38
+ @proxy_targets[attr] = target
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
11
44
  end
12
45
 
13
46
  def configure source = nil, options = {}, &block
@@ -22,16 +55,13 @@ module Mutaconf
22
55
 
23
56
  instance_exec self, &block if block
24
57
 
25
- @target.object
58
+ self
26
59
  end
27
60
 
28
61
  private
29
62
 
30
63
  def configure_from_hash h, options = {}
31
- h.each_pair do |k,v|
32
- raise KeyError, k unless @keys.empty? or @keys.include?(k.to_sym) or @lenient
33
- @target.set k, v if @keys.empty? or @keys.include?(k.to_sym)
34
- end
64
+ h.each_pair{ |attr,value| send attr, value }
35
65
  end
36
66
 
37
67
  def configure_from_file f, options = {}
@@ -41,20 +71,29 @@ module Mutaconf
41
71
  end
42
72
 
43
73
  def configure_from_object o, options = {}
44
- @keys.each{ |key| @target.set key, o.send(key.to_sym) if o.respond_to? key.to_sym }
74
+ @attr_targets.each_pair do |attr,target|
75
+ send attr, o.send(attr.to_sym) if o.respond_to? attr.to_sym
76
+ end
77
+ end
78
+
79
+ def has? attr, method
80
+ @attr_targets.default or @attr_targets.key?(attr) or @proxy_targets.default or @proxy_targets.key?(method)
81
+ end
82
+
83
+ def set attr, value
84
+
45
85
  end
46
86
 
47
87
  def method_missing name, *args, &block
48
88
 
49
89
  m = name.to_s.match(/\A(\w+)\=?\Z/)
50
90
 
51
- # TODO: fail if property is not in @keys
52
- key = m[1]
53
- if key
54
- raise KeyError, key unless @target.has?(key) or @lenient
55
- return @target.get key if args.empty?
56
- raise KeyError, key unless @keys.empty? or @keys.include?(key.to_sym) or @lenient
57
- @target.set key, args.first if @keys.empty? or @keys.include?(key.to_sym)
91
+ attr, method = m[1].to_sym, m[0].to_sym
92
+ if attr
93
+ raise KeyError, attr unless has?(attr, method) or @lenient
94
+ return @attr_targets[attr].get attr if args.empty?
95
+ @attr_targets[attr].set attr, args.first if @attr_targets.default or @attr_targets.key?(attr)
96
+ @proxy_targets[attr].send *(args.unshift method) if @proxy_targets.default or @proxy_targets.key?(attr)
58
97
  else
59
98
  super
60
99
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mutaconf"
8
- s.version = "0.0.7"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["AlphaHydrae"]
12
- s.date = "2013-04-14"
12
+ s.date = "2013-04-29"
13
13
  s.description = "Create simple DSLs and read configuration from hashes or objects."
14
14
  s.email = "hydrae.alpha@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  ]
19
19
  s.files = [
20
20
  ".rspec",
21
- ".ruby-gemset",
22
21
  ".ruby-version",
23
22
  ".screenrc",
24
23
  "Gemfile",
@@ -37,6 +36,7 @@ Gem::Specification.new do |s|
37
36
  "spec/extract_spec.rb",
38
37
  "spec/fixtures/eval.rb",
39
38
  "spec/helper.rb",
39
+ "spec/proxy_spec.rb",
40
40
  "spec/source_spec.rb",
41
41
  "spec/subclass_spec.rb",
42
42
  "spec/target_spec.rb",
@@ -45,11 +45,11 @@ Gem::Specification.new do |s|
45
45
  s.homepage = "http://github.com/AlphaHydrae/mutaconf"
46
46
  s.licenses = ["MIT"]
47
47
  s.require_paths = ["lib"]
48
- s.rubygems_version = "2.0.3"
48
+ s.rubygems_version = "1.8.25"
49
49
  s.summary = "Configuration utilities."
50
50
 
51
51
  if s.respond_to? :specification_version then
52
- s.specification_version = 4
52
+ s.specification_version = 3
53
53
 
54
54
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
55
  s.add_development_dependency(%q<bundler>, [">= 0"])
@@ -4,7 +4,7 @@ describe Mutaconf::DSL do
4
4
 
5
5
  let(:target){ {} }
6
6
  let(:dsl){ Mutaconf::DSL.new options }
7
- let(:options){ { target: target } }
7
+ let(:options){ { attrs: { target => true } } }
8
8
 
9
9
  it "should configure properties with instance evaluation" do
10
10
  result = dsl.configure do
@@ -12,7 +12,7 @@ describe Mutaconf::DSL do
12
12
  c 'd'
13
13
  end
14
14
  expected = { a: 'b', c: 'd' }
15
- result.should be(target)
15
+ result.should be(dsl)
16
16
  target.should == expected
17
17
  end
18
18
 
@@ -23,13 +23,13 @@ describe Mutaconf::DSL do
23
23
  config.i = 'j'
24
24
  end
25
25
  expected = { e: 'f', g: 'h', i: 'j' }
26
- result.should be(target)
26
+ result.should be(dsl)
27
27
  target.should == expected
28
28
  end
29
29
 
30
30
  context "with restricted keys" do
31
31
 
32
- let(:options){ { target: target, keys: [ :a ] } }
32
+ let(:options){ { attrs: { target => [ :a ] } } }
33
33
 
34
34
  it "should raise a key error for a unknown key with instance evaluation" do
35
35
  lambda{ dsl.configure{ b 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
@@ -42,7 +42,7 @@ describe Mutaconf::DSL do
42
42
 
43
43
  context "with restricted keys in lenient mode" do
44
44
 
45
- let(:options){ { target: target, keys: [ :a, :e ], lenient: true } }
45
+ let(:options){ { attrs: { target => [ :a, :e ] }, lenient: true } }
46
46
 
47
47
  it "should configure restricted properties with instance evaluation" do
48
48
  result = dsl.configure do
@@ -52,7 +52,7 @@ describe Mutaconf::DSL do
52
52
  g 'h'
53
53
  end
54
54
  expected = { a: 'b', e: 'f' }
55
- result.should be(target)
55
+ result.should be(dsl)
56
56
  target.should == expected
57
57
  end
58
58
 
@@ -64,7 +64,7 @@ describe Mutaconf::DSL do
64
64
  config.g = 'h'
65
65
  end
66
66
  expected = { a: 'b', e: 'f' }
67
- result.should be(target)
67
+ result.should be(dsl)
68
68
  target.should == expected
69
69
  end
70
70
  end
@@ -0,0 +1,70 @@
1
+ require 'helper'
2
+
3
+ describe 'DSL proxies' do
4
+
5
+ let(:target){ OpenStruct.new }
6
+ let(:dsl){ Mutaconf::DSL.new options }
7
+ let(:options){ { proxy: { target => true } } }
8
+
9
+ it "should proxy method calls with instance evaluation" do
10
+ target.should_receive(:a).with('b')
11
+ target.should_receive(:c=).with('d')
12
+ result = dsl.configure do
13
+ a 'b'
14
+ self.c = 'd'
15
+ end
16
+ result.should be(dsl)
17
+ end
18
+
19
+ it "should proxy method calls with a configuration object" do
20
+ target.should_receive(:a=).with('b')
21
+ target.should_receive(:c=).with('d')
22
+ result = dsl.configure do |config|
23
+ config.a = 'b'
24
+ config.c = 'd'
25
+ end
26
+ result.should be(dsl)
27
+ end
28
+
29
+ context "with restricted keys" do
30
+
31
+ let(:options){ { proxy: { target => [ :a ] } } }
32
+
33
+ it "should raise a key error for a unknown key with instance evaluation" do
34
+ lambda{ dsl.configure{ b 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
35
+ end
36
+
37
+ it "should raise a key error for an unknown key with a configuration object" do
38
+ lambda{ dsl.configure{ |config| config.b = 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
39
+ end
40
+ end
41
+
42
+ context "with restricted keys in lenient mode" do
43
+
44
+ let(:options){ { proxy: { target => [ :a, :e ] }, lenient: true } }
45
+
46
+ it "should configure restricted properties with instance evaluation" do
47
+ target.should_receive(:a).with('b')
48
+ target.should_receive(:e).with('f')
49
+ result = dsl.configure do
50
+ a 'b'
51
+ c 'd'
52
+ e 'f'
53
+ g 'h'
54
+ end
55
+ result.should be(dsl)
56
+ end
57
+
58
+ it "should configure restricted properties with a configuration object" do
59
+ target.should_receive(:a=).with('b')
60
+ target.should_receive(:e=).with('f')
61
+ result = dsl.configure do |config|
62
+ config.a = 'b'
63
+ config.c = 'd'
64
+ config.e = 'f'
65
+ config.g = 'h'
66
+ end
67
+ result.should be(dsl)
68
+ end
69
+ end
70
+ end
@@ -5,25 +5,25 @@ describe Mutaconf::DSL do
5
5
 
6
6
  let(:target){ {} }
7
7
  let(:dsl){ Mutaconf::DSL.new options }
8
- let(:options){ { target: target } }
8
+ let(:options){ { attrs: { target => true } } }
9
9
 
10
10
  it "should configure properties from a hash source" do
11
11
  result = dsl.configure a: 'b', c: 'd'
12
12
  expected = { a: 'b', c: 'd' }
13
- result.should be(target)
13
+ result.should be(dsl)
14
14
  target.should == expected
15
15
  end
16
16
 
17
17
  it "should configure properties from a file with instance evaluation" do
18
18
  result = dsl.configure fixture(:eval)
19
19
  expected = { a: 'b', c: 'd', e: 'f', g: 'h' }
20
- result.should be(target)
20
+ result.should be(dsl)
21
21
  target.should == expected
22
22
  end
23
23
 
24
24
  context "with restricted keys" do
25
25
 
26
- let(:options){ { target: target, keys: [ :a ] } }
26
+ let(:options){ { attrs: { target => [ :a ] } } }
27
27
 
28
28
  it "should raise a key error for an unknown key from a hash source" do
29
29
  lambda{ dsl.configure b: 'c' }.should raise_error(Mutaconf::KeyError, /'b'/)
@@ -36,26 +36,26 @@ describe Mutaconf::DSL do
36
36
 
37
37
  context "with restricted keys in lenient mode" do
38
38
 
39
- let(:options){ { target: target, keys: [ :a, :e ], lenient: true } }
39
+ let(:options){ { attrs: { target => [ :a, :e ] }, lenient: true } }
40
40
 
41
41
  it "should configure restricted properties from a hash source" do
42
42
  result = dsl.configure a: 'b', c: 'd', e: 'f', g: 'h'
43
43
  expected = { a: 'b', e: 'f' }
44
- result.should be(target)
44
+ result.should be(dsl)
45
45
  target.should == expected
46
46
  end
47
47
 
48
48
  it "should configure restricted properties from an object" do
49
49
  result = dsl.configure OpenStruct.new(a: 'b', c: 'd', e: 'f', g: 'h')
50
50
  expected = { a: 'b', e: 'f' }
51
- result.should be(target)
51
+ result.should be(dsl)
52
52
  target.should == expected
53
53
  end
54
54
 
55
55
  it "should configure restricted properties from a file with instance evaluation" do
56
56
  result = dsl.configure fixture(:eval)
57
57
  expected = { a: 'b', e: 'f' }
58
- result.should be(target)
58
+ result.should be(dsl)
59
59
  target.should == expected
60
60
  end
61
61
  end
@@ -16,7 +16,7 @@ describe Mutaconf::DSL do
16
16
  end
17
17
 
18
18
  let(:target){ {} }
19
- let(:dsl){ CustomDSL.new target: target }
19
+ let(:dsl){ CustomDSL.new attrs: { target => true } }
20
20
 
21
21
  it "should work when subclassed" do
22
22
  result = dsl.configure do
@@ -25,7 +25,7 @@ describe Mutaconf::DSL do
25
25
  increase 5
26
26
  end
27
27
  expected = { a: 'b', c: 'd' }
28
- result.should be(target)
28
+ result.should be(dsl)
29
29
  target.should == expected
30
30
  dsl.value.should == 5
31
31
  end
metadata CHANGED
@@ -1,125 +1,142 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutaconf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - AlphaHydrae
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-04-14 00:00:00.000000000 Z
12
+ date: 2013-04-29 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: jeweler
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - '>='
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - '>='
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  - !ruby/object:Gem::Dependency
70
79
  name: gemcutter
71
80
  requirement: !ruby/object:Gem::Requirement
81
+ none: false
72
82
  requirements:
73
- - - '>='
83
+ - - ! '>='
74
84
  - !ruby/object:Gem::Version
75
85
  version: '0'
76
86
  type: :development
77
87
  prerelease: false
78
88
  version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
79
90
  requirements:
80
- - - '>='
91
+ - - ! '>='
81
92
  - !ruby/object:Gem::Version
82
93
  version: '0'
83
94
  - !ruby/object:Gem::Dependency
84
95
  name: gem-release
85
96
  requirement: !ruby/object:Gem::Requirement
97
+ none: false
86
98
  requirements:
87
- - - '>='
99
+ - - ! '>='
88
100
  - !ruby/object:Gem::Version
89
101
  version: '0'
90
102
  type: :development
91
103
  prerelease: false
92
104
  version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
93
106
  requirements:
94
- - - '>='
107
+ - - ! '>='
95
108
  - !ruby/object:Gem::Version
96
109
  version: '0'
97
110
  - !ruby/object:Gem::Dependency
98
111
  name: rake-version
99
112
  requirement: !ruby/object:Gem::Requirement
113
+ none: false
100
114
  requirements:
101
- - - '>='
115
+ - - ! '>='
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
107
122
  requirements:
108
- - - '>='
123
+ - - ! '>='
109
124
  - !ruby/object:Gem::Version
110
125
  version: '0'
111
126
  - !ruby/object:Gem::Dependency
112
127
  name: simplecov
113
128
  requirement: !ruby/object:Gem::Requirement
129
+ none: false
114
130
  requirements:
115
- - - '>='
131
+ - - ! '>='
116
132
  - !ruby/object:Gem::Version
117
133
  version: '0'
118
134
  type: :development
119
135
  prerelease: false
120
136
  version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
121
138
  requirements:
122
- - - '>='
139
+ - - ! '>='
123
140
  - !ruby/object:Gem::Version
124
141
  version: '0'
125
142
  description: Create simple DSLs and read configuration from hashes or objects.
@@ -131,7 +148,6 @@ extra_rdoc_files:
131
148
  - README.md
132
149
  files:
133
150
  - .rspec
134
- - .ruby-gemset
135
151
  - .ruby-version
136
152
  - .screenrc
137
153
  - Gemfile
@@ -150,6 +166,7 @@ files:
150
166
  - spec/extract_spec.rb
151
167
  - spec/fixtures/eval.rb
152
168
  - spec/helper.rb
169
+ - spec/proxy_spec.rb
153
170
  - spec/source_spec.rb
154
171
  - spec/subclass_spec.rb
155
172
  - spec/target_spec.rb
@@ -157,25 +174,26 @@ files:
157
174
  homepage: http://github.com/AlphaHydrae/mutaconf
158
175
  licenses:
159
176
  - MIT
160
- metadata: {}
161
177
  post_install_message:
162
178
  rdoc_options: []
163
179
  require_paths:
164
180
  - lib
165
181
  required_ruby_version: !ruby/object:Gem::Requirement
182
+ none: false
166
183
  requirements:
167
- - - '>='
184
+ - - ! '>='
168
185
  - !ruby/object:Gem::Version
169
186
  version: '0'
170
187
  required_rubygems_version: !ruby/object:Gem::Requirement
188
+ none: false
171
189
  requirements:
172
- - - '>='
190
+ - - ! '>='
173
191
  - !ruby/object:Gem::Version
174
192
  version: '0'
175
193
  requirements: []
176
194
  rubyforge_project:
177
- rubygems_version: 2.0.3
195
+ rubygems_version: 1.8.25
178
196
  signing_key:
179
- specification_version: 4
197
+ specification_version: 3
180
198
  summary: Configuration utilities.
181
199
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 43e6c84852a4d8d8c4f54d087be1f63e0c146a92
4
- data.tar.gz: 38435cb0e55d1124753e6c7130c34417005b856e
5
- SHA512:
6
- metadata.gz: 79216c2b4f5bef4127d3bcb7bfde2a976e6e57b3fc241d778a1afdaeeba705d1288fa06d50d9aaf7f19a83e590ab3a2e419444ed6bd0e48504e400b75a25abfa
7
- data.tar.gz: 19d3897186467a70ca13b64005f5745c384ecbff7fc39eb2890ca26499c83358c4e8ae89bb5f63a4311dd12e6f29fc52450639b0ef4c063befe881688451d6b4
@@ -1 +0,0 @@
1
- mutaconf