Class: CascadingConfiguration::Core::Encapsulation
- Inherits:
-
Module
- Object
- Module
- CascadingConfiguration::Core::Encapsulation
- Includes:
- ParallelAncestry
- Defined in:
- lib/cascading_configuration/core/encapsulation.rb
Defined Under Namespace
Classes: InstanceStruct
Instance Attribute Summary collapse
-
#encapsulation_name ⇒ Object
readonly
encapsulation_name #.
Class Method Summary collapse
-
.encapsulation(encapsulation_or_name, ensure_exists = false) ⇒ Object
self.encapsulation #.
Instance Method Summary collapse
-
#configuration_names(instance) ⇒ Object
configuration_names #.
-
#configuration_variables(instance) ⇒ Object
configuration_variables #.
-
#configurations(instance) ⇒ Object
configurations #.
-
#get_configuration(instance, configuration_name) ⇒ Object
get_configuration #.
-
#has_configuration?(instance, configuration_name) ⇒ Boolean
has_configuration? #.
-
#has_configuration_value?(instance, configuration_name) ⇒ Boolean
has_configuration_value? #.
-
#has_configurations?(instance) ⇒ Boolean
has_configurations? #.
-
#initialize(encapsulation_name) ⇒ Encapsulation
constructor
initialize #.
-
#is_parent_for_configuration?(existing_parent, configuration_name, parent) ⇒ Boolean
is_parent_for_configuration? #.
-
#match_parent(instance, configuration_name, &match_block) ⇒ Object
match_parent #.
-
#parent_for_configuration(instance, configuration_name) ⇒ Object
parent_for_configuration #.
-
#register_child_for_parent(child, parent) ⇒ Object
register_child_for_parent #.
-
#register_configuration(instance, configuration_name, configuration_module) ⇒ Object
register_configuration #.
-
#register_parent_for_configuration(child, parent, configuration_name) ⇒ Object
register_parent_for_configuration #.
-
#remove_configuration(instance, configuration_name) ⇒ Object
remove_configuration #.
-
#remove_configuration_variable(instance, configuration_name) ⇒ Object
remove_configuration_variable #.
-
#set_configuration(instance, configuration_name, value) ⇒ Object
set_configuration #.
Constructor Details
#initialize(encapsulation_name) ⇒ Encapsulation
initialize #
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 50 def initialize( encapsulation_name ) super() @encapsulation_name = encapsulation_name encapsulation_instance = self # We manage reference to self in singleton from here to avoid duplicative efforts. self.class.class_eval do @encapsulations[ encapsulation_name ] = encapsulation_instance const_set( encapsulation_name.to_s.to_camel_case, encapsulation_instance ) end @instances_hash = { } end |
Instance Attribute Details
#encapsulation_name ⇒ Object (readonly)
encapsulation_name #
72 73 74 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 72 def encapsulation_name @encapsulation_name end |
Class Method Details
.encapsulation(encapsulation_or_name, ensure_exists = false) ⇒ Object
self.encapsulation #
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 16 def self.encapsulation( encapsulation_or_name, ensure_exists = false ) encapsulation_instance = nil case encapsulation_or_name when nil encapsulation_instance = ::CascadingConfiguration::Core::Module::DefaultEncapsulation when self encapsulation_instance = encapsulation_or_name when ::Symbol, ::String unless encapsulation_instance = @encapsulations[ encapsulation_or_name ] if ensure_exists exception_string = 'No encapsulation defined for :' << encapsulation_or_name.to_s exception_string << '.' raise ::ArgumentError, exception_string end end end return encapsulation_instance end |
Instance Method Details
#configuration_names(instance) ⇒ Object
configuration_names #
88 89 90 91 92 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 88 def configuration_names( instance ) return configurations( instance ).keys end |
#configuration_variables(instance) ⇒ Object
configuration_variables #
240 241 242 243 244 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 240 def configuration_variables( instance ) return configuration_struct( instance ).configuration_variables_hash ||= { } end |
#configurations(instance) ⇒ Object
configurations #
112 113 114 115 116 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 112 def configurations( instance ) return configuration_struct( instance ).configurations_hash ||= { } end |
#get_configuration(instance, configuration_name) ⇒ Object
get_configuration #
270 271 272 273 274 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 270 def get_configuration( instance, configuration_name ) return configuration_variables( instance )[ configuration_name ] end |
#has_configuration?(instance, configuration_name) ⇒ Boolean
has_configuration? #
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 122 def has_configuration?( instance, configuration_name ) has_configuration = nil unless has_configuration = configurations( instance ).has_key?( configuration_name ) or instance == instance.class has_configuration = configurations( instance.class ).has_key?( configuration_name ) end return has_configuration end |
#has_configuration_value?(instance, configuration_name) ⇒ Boolean
has_configuration_value? #
260 261 262 263 264 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 260 def has_configuration_value?( instance, configuration_name ) return configuration_variables( instance ).has_key?( configuration_name ) end |
#has_configurations?(instance) ⇒ Boolean
has_configurations? #
78 79 80 81 82 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 78 def has_configurations?( instance ) return ! configurations( instance ).empty? end |
#is_parent_for_configuration?(existing_parent, configuration_name, parent) ⇒ Boolean
is_parent_for_configuration? #
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 176 def is_parent_for_configuration?( existing_parent, configuration_name, parent ) is_parent_for_configuration = false this_parent = existing_parent begin if this_parent.equal?( parent ) is_parent_for_configuration = true end end while this_parent = parent_for_configuration( this_parent, configuration_name ) return is_parent_for_configuration end |
#match_parent(instance, configuration_name, &match_block) ⇒ Object
match_parent #
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 290 def match_parent( instance, configuration_name, & match_block ) matched_parent = nil this_parent = instance begin if match_block.call( this_parent ) matched_parent = this_parent break end end while this_parent = parent_for_configuration( this_parent, configuration_name ) return matched_parent end |
#parent_for_configuration(instance, configuration_name) ⇒ Object
parent_for_configuration #
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 221 def parent_for_configuration( instance, configuration_name ) unless instance.equal?( ::Class ) or parent = parent_for_configuration_hash( instance )[ configuration_name ] instance_class = instance.class if has_configuration?( instance_class, configuration_name ) parent = instance_class end end return parent end |
#register_child_for_parent(child, parent) ⇒ Object
register_child_for_parent #
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 149 def register_child_for_parent( child, parent ) super # Modules already have configurations and if parent already has configurations unless parent.is_a?( ::Module ) or has_parents?( parent ) register_child_for_parent( parent, parent.class ) end parent_configurations = configurations( parent ) parent_configurations.each do |this_name, this_configuration_module| register_configuration( child, this_name, this_configuration_module ) register_parent_for_configuration( child, parent, this_name ) this_configuration_module.create_configuration( self, child, this_name ) end parent_configurations.each do |this_name, this_configuration_module| this_configuration_module.initialize_configuration( self, child, this_name ) end end |
#register_configuration(instance, configuration_name, configuration_module) ⇒ Object
register_configuration #
98 99 100 101 102 103 104 105 106 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 98 def register_configuration( instance, configuration_name, configuration_module ) # Record that a configuration is defined for this instance. # # This doesn't suggest that a configuration exists for this instance and name, only that it has # been registered to acknowledge it could exist. return configurations( instance )[ configuration_name ] = configuration_module end |
#register_parent_for_configuration(child, parent, configuration_name) ⇒ Object
register_parent_for_configuration #
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 198 def register_parent_for_configuration( child, parent, configuration_name ) parents_hash = parent_for_configuration_hash( child ) # if we already have a parent, check to see if new parent is an ancestor of it # if so, keep existing parent unless existing_parent = parents_hash[ configuration_name ] and is_parent_for_configuration?( existing_parent, configuration_name, parent ) parents_hash[ configuration_name ] = parent parents( child ).unshift( parent ) end return self end |
#remove_configuration(instance, configuration_name) ⇒ Object
remove_configuration #
139 140 141 142 143 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 139 def remove_configuration( instance, configuration_name ) return configurations( instance ).delete( configuration_name ) end |
#remove_configuration_variable(instance, configuration_name) ⇒ Object
remove_configuration_variable #
280 281 282 283 284 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 280 def remove_configuration_variable( instance, configuration_name ) return configuration_variables( instance ).delete( configuration_name ) end |
#set_configuration(instance, configuration_name, value) ⇒ Object
set_configuration #
250 251 252 253 254 |
# File 'lib/cascading_configuration/core/encapsulation.rb', line 250 def set_configuration( instance, configuration_name, value ) return configuration_variables( instance )[ configuration_name ] = value end |