cascading-configuration-hash 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,8 @@ class CascadingConfiguration::CompositingHash < Hash
9
9
 
10
10
  def initialize( cascading_name, working_instance )
11
11
  @cascading_name = cascading_name
12
- @cascading_variable = working_instance.cascading_variable_name( @cascading_name )
12
+ @working_class = ( working_instance.is_a?( Module ) ? working_instance : working_instance.class )
13
+ @cascading_variable = @working_class::AccessorSupportModule.cascading_variable_name( @cascading_name )
13
14
  @working_instance = working_instance
14
15
  # if first ancestor can have a composite array, register self with it in case it gets updated in the future
15
16
  if ( first_ancestor = @working_instance.first_ancestor ).respond_to?( :composite_hash_for_cascading_configuration )
@@ -79,11 +80,13 @@ class CascadingConfiguration::CompositingHash < Hash
79
80
  # replace #
80
81
  #############
81
82
 
83
+ alias_method :super_replace, :replace
82
84
  def replace( other_hash )
83
85
  # clear current values
84
86
  clear
85
87
  # merge replacement settings
86
88
  merge!( other_hash )
89
+ update_self_as_cascading_composite
87
90
  return self
88
91
  end
89
92
 
@@ -106,7 +109,7 @@ class CascadingConfiguration::CompositingHash < Hash
106
109
  alias_method :super_clear, :clear
107
110
  def clear
108
111
  # add all existing values to exclude array
109
- self.keys.each do |this_key|
112
+ keys.each do |this_key|
110
113
  delete( this_key )
111
114
  end
112
115
  update_removing_composite_elements( *self.keys )
@@ -10,7 +10,8 @@ module CascadingConfiguration::CompositingHash::Instance
10
10
  ##################################
11
11
 
12
12
  def composite_hash_variable_name( configuration_name )
13
- return ( cascading_variable_name( configuration_name ).to_s + '__' + 'composite_hash' ).to_sym
13
+ klass = ( is_a?( Module ) ? self : self.class )
14
+ return ( klass::AccessorSupportModule.cascading_variable_name( configuration_name ).to_s + '__' + 'composite_hash' ).to_sym
14
15
  end
15
16
 
16
17
  end
@@ -12,22 +12,14 @@ module CascadingConfiguration::Hash
12
12
  class_or_module.instance_eval do
13
13
  include CascadingConfiguration::Variable
14
14
  extend module_self
15
- extend CascadingConfiguration::Hash::Accessors
16
- extend CascadingConfiguration::Hash::ClassInstance
15
+ extend module_self::Accessors
16
+ extend module_self::ClassInstance
17
17
  include CascadingConfiguration::CompositingHash::Instance
18
18
  # module support
19
- unless class_or_module.is_a?( Class )
19
+ unless is_a?( Class )
20
20
  extend CascadingConfiguration::Hash::ModuleInstance
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
- #########################
26
- # ancestors_to_Object #
27
- #########################
28
-
29
- def ancestors_to_Object
30
- return ancestors_to_class_or_module_interpolating_module_branches( Object, CascadingConfiguration::Hash::ModuleInstance )
31
- end
32
-
33
25
  end
@@ -3,22 +3,6 @@ module CascadingConfiguration::Hash::Accessors
3
3
 
4
4
  extend CascadingConfiguration::InternalModuleStub
5
5
 
6
- ###################
7
- # self.extended #
8
- ###################
9
-
10
- def self.extended( class_or_module )
11
- class_or_module.instance_eval do
12
- # accessor support in module to permit method overriding with super
13
- unless const_defined?( :AccessorSupportModule )
14
- accessor_support_module = Module.new { extend CascadingConfiguration::InternalModuleStub }
15
- const_set( :AccessorSupportModule, accessor_support_module )
16
- include class_or_module::AccessorSupportModule
17
- extend class_or_module::AccessorSupportModule
18
- end
19
- end
20
- end
21
-
22
6
  ######################################
23
7
  # define_configuration_hash_setter #
24
8
  ######################################
@@ -7,9 +7,10 @@ module CascadingConfiguration::Hash::ClassInstance
7
7
  # attr_configuration_hash #
8
8
  #############################
9
9
 
10
- def attr_configuration_hash( *property_names )
10
+ def attr_configuration_hash( *property_names, & compositing_block )
11
11
 
12
12
  property_names.each do |this_property_name|
13
+ self::AccessorSupportModule.set_compositing_proc( this_property_name, compositing_block ) if block_given?
13
14
  # define configuration setter
14
15
  define_configuration_hash_setter( this_property_name )
15
16
  # define configuration getter
@@ -2,22 +2,21 @@
2
2
  module CascadingConfiguration::Hash::ModuleInstance
3
3
 
4
4
  extend CascadingConfiguration::InternalModuleStub
5
-
5
+
6
6
  ##############
7
7
  # included #
8
8
  ##############
9
9
 
10
10
  def included( class_or_module )
11
11
  super if method_defined?( :super )
12
+ # CascadingConfiguration::Hash
12
13
  module_self = self
13
14
  class_or_module.instance_eval do
14
- include CascadingConfiguration::Hash
15
- # when we extend a module (which has happened if we got here)
16
- # then we need to make sure when the module we are extending is included
17
- # * CascadingConfiguration is included
18
- # * the module we extended is used to extend the class/module that included
15
+ # whichever module included CascadingConfiguration::Hash
19
16
  extend module_self
17
+ # cascade CascadingConfiguration::Hash
18
+ include CascadingConfiguration::Hash
20
19
  end
21
20
  end
22
21
 
23
- end
22
+ end
@@ -12,10 +12,7 @@ class CascadingConfiguration::CompositingHash < Hash
12
12
  def update_self_as_cascading_composite
13
13
  # start fresh
14
14
  super_clear
15
- # add parent config
16
- super_merge!( @parent_composite_hash ) if @parent_composite_hash
17
- # add additional local config
18
- super_merge!( @local_cascading_hash )
15
+ update_composite_self_for_parent_hash( @parent_composite_hash, @local_cascading_hash )
19
16
  # remove local exclude
20
17
  unless @local_cascading_hash.exclude_array.empty?
21
18
  @local_cascading_hash.exclude_array.each do |this_excluded_element|
@@ -32,10 +29,32 @@ class CascadingConfiguration::CompositingHash < Hash
32
29
  ######################################
33
30
 
34
31
  def update_adding_composite_elements( elements_to_cascade )
35
- super_merge!( elements_to_cascade )
32
+ update_composite_self_for_parent_hash( self, elements_to_cascade )
36
33
  update_child_composite_arrays
34
+ return self
37
35
  end
38
36
 
37
+ ###########################################
38
+ # update_composite_self_for_parent_hash #
39
+ ###########################################
40
+
41
+ def update_composite_self_for_parent_hash( parent_hash, second_hash )
42
+ if compositing_proc = @working_class::AccessorSupportModule.get_compositing_proc( @cascading_name )
43
+ # if we have a compositing proc defined, use its result to replace
44
+ parent_hash_copy = Hash.new
45
+ parent_hash_copy.merge!( parent_hash ) if parent_hash
46
+ second_hash_copy = Hash.new
47
+ second_hash_copy.merge!( second_hash ) if second_hash
48
+ composite_result = compositing_proc.call( parent_hash_copy, second_hash_copy )
49
+ super_replace( composite_result )
50
+ else
51
+ # otherwise we simply merge
52
+ super_merge!( parent_hash ) if parent_hash
53
+ super_merge!( second_hash ) if second_hash
54
+ end
55
+ return self
56
+ end
57
+
39
58
  ########################################
40
59
  # update_removing_composite_elements #
41
60
  ########################################
@@ -8,7 +8,6 @@ describe CascadingConfiguration::Hash do
8
8
  #############################
9
9
 
10
10
  it 'can define a configuration hash, which is the primary interface' do
11
-
12
11
  module CascadingConfiguration::Hash::MockModule
13
12
  include CascadingConfiguration::Hash
14
13
  attr_configuration_hash :configuration_setting
@@ -55,7 +54,7 @@ describe CascadingConfiguration::Hash do
55
54
  end
56
55
 
57
56
  CascadingConfiguration::Hash::MockClassSub1.new.configuration_setting.should == { :a_configuration => :some_value,
58
- :another_configuration => :some_value }
57
+ :another_configuration => :some_value }
59
58
 
60
59
  object_instance_two = CascadingConfiguration::Hash::MockClassSub1.new
61
60
  object_instance_two.configuration_setting.should == { :a_configuration => :some_value,
@@ -68,13 +67,13 @@ describe CascadingConfiguration::Hash do
68
67
  # change ancestor setting
69
68
  CascadingConfiguration::Hash::MockClass.configuration_setting[ :a_yet_unused_configuration ] = :some_value
70
69
  CascadingConfiguration::Hash::MockClass.configuration_setting.should == { :a_configuration => :some_value,
71
- :a_yet_unused_configuration => :some_value }
70
+ :a_yet_unused_configuration => :some_value }
72
71
  object_instance_one.configuration_setting.should == { :a_configuration => :some_value,
73
72
  :a_yet_unused_configuration => :some_value,
74
73
  :some_other_configuration => :some_value }
75
74
  CascadingConfiguration::Hash::MockClassSub1.configuration_setting.should == { :a_configuration => :some_value,
76
- :a_yet_unused_configuration => :some_value,
77
- :another_configuration => :some_value }
75
+ :a_yet_unused_configuration => :some_value,
76
+ :another_configuration => :some_value }
78
77
  object_instance_two.configuration_setting.should == { :a_configuration => :some_value,
79
78
  :a_yet_unused_configuration => :some_value,
80
79
  :another_configuration => :some_value,
@@ -87,24 +86,44 @@ describe CascadingConfiguration::Hash do
87
86
  :some_other_configuration => :some_value }
88
87
  CascadingConfiguration::Hash::MockClassSub1.configuration_setting.freeze!
89
88
  CascadingConfiguration::Hash::MockClassSub1.configuration_setting.should == { :a_configuration => :some_value,
90
- :a_yet_unused_configuration => :some_value,
91
- :another_configuration => :some_value }
89
+ :a_yet_unused_configuration => :some_value,
90
+ :another_configuration => :some_value }
92
91
  CascadingConfiguration::Hash::MockClass.configuration_setting[ :non_cascading_configuration ] = :some_value
93
92
  CascadingConfiguration::Hash::MockClass.configuration_setting.should == { :a_configuration => :some_value,
94
- :a_yet_unused_configuration => :some_value,
95
- :non_cascading_configuration => :some_value }
93
+ :a_yet_unused_configuration => :some_value,
94
+ :non_cascading_configuration => :some_value }
96
95
  object_instance_one.configuration_setting.should == { :a_configuration => :some_value,
97
96
  :a_yet_unused_configuration => :some_value,
98
97
  :some_other_configuration => :some_value }
99
98
  CascadingConfiguration::Hash::MockClassSub1.configuration_setting.should == { :a_configuration => :some_value,
100
- :a_yet_unused_configuration => :some_value,
101
- :another_configuration => :some_value }
102
- object_instance_two.configuration_setting.should == { :a_configuration => :some_value,
103
- :a_yet_unused_configuration => :some_value,
104
- :another_configuration => :some_value,
105
- :some_other_configuration => :some_value }
106
-
99
+ :a_yet_unused_configuration => :some_value,
100
+ :another_configuration => :some_value }
101
+ object_instance_two.configuration_setting.should == { :a_configuration => :some_value,
102
+ :a_yet_unused_configuration => :some_value,
103
+ :another_configuration => :some_value,
104
+ :some_other_configuration => :some_value }
107
105
 
106
+ module CascadingConfiguration::Hash::MockModule
107
+ attr_configuration_hash :some_hash do |parent_hash, composite_hash|
108
+ parent_hash.each do |this_key, this_data|
109
+ if existing_value = composite_hash[ this_key ]
110
+ composite_hash[ this_key ] = this_data - existing_value
111
+ else
112
+ composite_hash[ this_key ] = this_data
113
+ end
114
+ end
115
+ composite_hash
116
+ end
117
+ self.some_hash = { :one => 1, :two => 2 }
118
+ self.some_hash.should == { :one => 1, :two => 2 }
119
+ end
120
+
121
+ module CascadingConfiguration::Hash::MockModule2
122
+ self.some_hash.should == { :one => 1, :two => 2 }
123
+ self.some_hash.replace( { :one => 1, :two => 2 } )
124
+ self.some_hash.should == { :one => 0, :two => 0 }
125
+ end
126
+
108
127
  end
109
128
 
110
129
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 0
9
- version: 1.1.0
8
+ - 1
9
+ version: 1.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Asher
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-14 00:00:00 -04:00
17
+ date: 2011-07-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency