ruby-conf 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.0
1
+ 1.4.1
data/lib/ruby-conf.rb CHANGED
@@ -5,44 +5,95 @@ class RubyConf
5
5
  @@configs = {}
6
6
 
7
7
  class Config
8
+ attr_reader :attributes
9
+
8
10
  def initialize
9
11
  @attributes = {}
10
12
  end
11
13
 
12
14
  def [](name)
13
- @attributes[name.to_sym]
15
+ value = @attributes[name.to_sym]
16
+
17
+ if value.is_a?(Proc)
18
+ value.call
19
+ else
20
+ value
21
+ end
22
+ end
23
+
24
+ def []=(name,value)
25
+ @attributes[name.to_sym] = value
26
+ end
27
+
28
+ def inherit(name, parent)
29
+ self[name] = Config.new
30
+ self[name].attributes.merge! parent.attributes.clone
31
+ end
32
+
33
+ def []=(name,value)
34
+ @attributes[name.to_sym] = value
14
35
  end
15
36
 
16
37
  def method_missing(name, *args, &block)
17
- case(args.size)
18
- when 0:
19
- if block_given?
20
- @attributes[name.to_sym] = RubyConf.define(&block)
21
- elsif @attributes.has_key? name.to_sym
22
- value = @attributes[name.to_sym]
23
-
24
- if value.is_a?(Proc)
25
- value.call
26
- else
27
- value
28
- end
29
- else
30
- super
31
- end
32
- when 1:
33
- @attributes[name.to_sym] = args.first
34
- else
35
- @attributes[name.to_sym] = args
38
+ if block_given?
39
+ _inherit(name, args)
40
+ _set_config_with_block(name,block)
41
+ return
36
42
  end
43
+
44
+ super if 0 == args.size && !@attributes.has_key?(name.to_sym)
45
+
46
+ _set_or_get_attribute(name, args)
37
47
  end
38
48
 
39
49
  def respond_to?(name)
40
50
  if @attributes.has_key? name.to_sym
41
51
  true
52
+ elsif @parent
53
+ @parent.respond_to?(name)
42
54
  else
43
55
  super
44
56
  end
45
57
  end
58
+
59
+ private
60
+
61
+ def _set_or_get_attribute(name, args)
62
+ case(args.size)
63
+ when 0:
64
+ # config.something
65
+ # => 'value'
66
+ self[name]
67
+ when 1:
68
+ # config.something "value"
69
+ self[name] = args.first
70
+ else
71
+ # config.something "value", "value2"
72
+ self[name] = args
73
+ end
74
+ end
75
+
76
+ def _update_config_with_block(name,block)
77
+ self[name].instance_eval(&block)
78
+ end
79
+
80
+ def _new_config_with_block(name, block)
81
+ self[name] = RubyConf.define(&block)
82
+ end
83
+
84
+ def _set_config_with_block(name, block)
85
+ if self[name].is_a?(Config)
86
+ _update_config_with_block(name,block)
87
+ else
88
+ _new_config_with_block(name,block)
89
+ end
90
+ end
91
+
92
+ def _inherit(name, args)
93
+ if args.size > 0 && args.first.is_a?(Hash) && args.first.has_key?(:inherits)
94
+ inherit name, args.first[:inherits]
95
+ end
96
+ end
46
97
  end
47
98
  # Define a configuration:
48
99
  #
data/ruby-conf.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-conf}
8
- s.version = "1.4.0"
8
+ s.version = "1.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Curtis Schofield & Hollin Wilkins"]
@@ -14,7 +14,7 @@ require 'ruby-conf'
14
14
 
15
15
  describe RubyConf do
16
16
  subject { RubyConf }
17
-
17
+
18
18
  describe ".define" do
19
19
  context "no arguments" do
20
20
  it "returns anonymous config" do
@@ -24,6 +24,36 @@ describe RubyConf do
24
24
  config.equality.should be_true
25
25
  end
26
26
  end
27
+ context ":inherit" do
28
+ let(:inherited_config) do
29
+ RubyConf.define "inherited_config" do
30
+ basic do
31
+ thing do
32
+ origin "swamp"
33
+ end
34
+ end
35
+
36
+ laboritory :inherits => basic do
37
+ thing do
38
+ strong true
39
+ end
40
+ end
41
+
42
+ city :inherits => basic do
43
+ thing do
44
+ origin "ocean"
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ it "pre-loads a config with a existing config" do
51
+ inherited_config.laboritory.thing.origin.should == inherited_config.basic.thing.origin
52
+ end
53
+ it "does not overwrite values" do
54
+ inherited_config.city.thing.origin.should == "ocean"
55
+ end
56
+ end
27
57
  context ":as" do
28
58
  it "creates a global Constant" do
29
59
  subject.define "rails_database", :as => :RailsDatabase do
@@ -34,6 +64,19 @@ describe RubyConf do
34
64
  ::RailsDatabase.production[:password].should_not be_nil
35
65
  end
36
66
  end
67
+ it "can reopen configs" do
68
+ config = subject.define do
69
+ godzilla do
70
+ spines false
71
+ awesome true
72
+ end
73
+ end
74
+ config.godzilla do
75
+ spines true
76
+ end
77
+ config.godzilla.spines.should == true
78
+ config.godzilla.awesome.should == true
79
+ end
37
80
  it "returns a named config" do
38
81
  config = subject.define "a_name" do
39
82
  something "hi"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-conf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 4
9
- - 0
10
- version: 1.4.0
9
+ - 1
10
+ version: 1.4.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Curtis Schofield & Hollin Wilkins