Class: TDriver::BehaviourFactory
- Defined in:
- lib/tdriver/base/behaviour/factory.rb
Class Method Summary collapse
-
.apply_behaviour(rule) ⇒ Object
TODO: document me.
-
.apply_behaviour!(*args) ⇒ Object
remove me when migration ready.
-
.collect_behaviours(rule) ⇒ Object
TODO: document me.
-
.init(options) ⇒ Object
initialize behaviours factory.
- .remove_behaviours(object) ⇒ Object
-
.reset ⇒ Object
reset class configuration.
-
.reset_cache ⇒ Object
TODO: document me.
-
.to_xml(rule) ⇒ Object
TODO: document me.
Class Method Details
.apply_behaviour(rule) ⇒ Object
TODO: document me
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 90 def apply_behaviour( rule ) # verify that rule is given as hash rule.check_type Hash, 'wrong argument type $1 for TDriver::BehaviourFactory#apply_behaviour rule argument (expected $2)' # empty collected indexes variable collected_indexes = [] # retrieve object from hash _object = rule[ :object ] # generate cache key, drop :object value from hash cache_key = rule.reject{ | key, value | key == :object }.hash # retrieve behaviour from cache if found if @behaviours_cache.has_key?( cache_key ) behaviours = @behaviours_cache[ cache_key ] else # collect behaviours that meets given rules behaviours = collect_behaviours( rule ) # store behaviour collection to cache for future reuse @behaviours_cache[ cache_key ] = behaviours end # iterate through each collected behaviour behaviours.each do | behaviour | begin # retrieve module from hash _module = behaviour[ :module ] unless _module.kind_of?( Module ) # retrieve behaviour module _module = MobyUtil::KernelHelper.get_constant( _module.to_s ) # store pointer to module (implementation) back to hash behaviour[ :module ] = _module end # extend target object with behaviour module _object.extend( _module ) # store behaviour indexes collected_indexes << behaviour[ :index ] rescue NameError raise NameError, "Implementation for #{ behaviour[ :name ] } behaviour does not exist. (#{ _module })" rescue raise RuntimeError, "Error while applying #{ behaviour[ :name ] } (#{ _module }) behaviour to target object due to #{ $!. } (#{ $!.class })" end end # behaviours.each # retrieve objects behaviour index array if already set collected_indexes = _object.instance_variable_get( :@object_behaviours ) | collected_indexes if _object.instance_variable_defined?( :@object_behaviours ) # add behaviour information to test object _object.instance_variable_set( :@object_behaviours, collected_indexes ) end |
.apply_behaviour!(*args) ⇒ Object
remove me when migration ready
272 273 274 275 276 277 278 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 272 def apply_behaviour!( *args ) warn_caller '$1:$2 warning: deprecated method apply_behaviour!; please use TDriver::BehaviourFactory.apply_behaviour instead' apply_behaviour( *args ) end |
.collect_behaviours(rule) ⇒ Object
TODO: document me
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 164 def collect_behaviours( rule ) # retrieve enabled plugins from PluginService enabled_plugins = TDriver::PluginService.enabled_plugins # default value for rule if not defined rule.default = [ '*' ] # store as local variable for less AST lookups _index = rule.fetch( :index, [] ) _object_type = rule[ :object_type ] _input_type = rule[ :input_type ] _env = rule[ :env ] _version = rule[ :version ] _name = rule[ :name ] _any = [ '*' ] # collect behaviours enabled_plugins.inject([]){ | result, plugin | @behaviours_per_plugin.fetch( plugin, [] ).each do | behaviour | # match other rules if no exact index given if _index.empty? case _name when behaviour[ :name ] # exact match with name result << behaviour when _any # compare rules and behaviour attributes if !( _object_type & behaviour[ :object_type ] ).empty? && !( _input_type & behaviour[ :input_type ] ).empty? && !( _env & behaviour[ :env ] ).empty? && !( _version & behaviour[ :version ] ).empty? result << behaviour end else false end else # index given result << behaviour if Array( _index ).include?( behaviour[ :index ] ) end end result } =begin @behaviours.select do | behaviour | # skip if required plugin is not registered or enabled; compare requires array and enabled_plugins array next unless ( behaviour[ :requires ] - enabled_plugins ).empty? # match other rules if no exact index given if _index.empty? case _name when behaviour[ :name ] # exact match with name true when _any # compare rules and behaviour attributes !( _object_type & behaviour[ :object_type ] ).empty? && !( _input_type & behaviour[ :input_type ] ).empty? && !( _env & behaviour[ :env ] ).empty? && !( _version & behaviour[ :version ] ).empty? else false end else # index given true if Array( _index ).include?( behaviour[ :index ] ) end end # behaviours.select =end end |
.init(options) ⇒ Object
initialize behaviours factory
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 29 def init( ) # verify that argument is type of hash .check_type Hash, 'wrong argument type $1 for TDriver::BehaviourFactory#init options argument (expected $2)' # load behaviour configuration files load_behaviours( .require_key( :path, 'required key $1 not found from TDriver::BehaviourFactory#init options argument' ) ) end |
.remove_behaviours(object) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 51 def remove_behaviours( object ) object.check_type [ MobyBase::TestObject, MobyBase::SUT ], 'wrong argument type $1 for target object (expected $2)' # add behaviour information to test object behaviour_index = object.instance_variable_get( :@object_behaviours ) collect_behaviours( :index => behaviour_index ).collect{ | behaviour | _module = behaviour[ :module ] instance_methods = [ _module.instance_methods( false ), _module.private_instance_methods( false ), _module.protected_instance_methods( false ) ].inject([]){ | result, methods | result.concat( methods ) } # remove behaviour instance methods from target object instance_methods.each do | method_name | object.instance_eval( "undef :#{ method_name.to_s }") # if respond_to?(:#{ method_name.to_s })" ) end behaviour_index.delete( behaviour[ :index ] ) } behaviour_index = object.instance_variable_set( :@object_behaviours, behaviour_index ) object end |
.reset ⇒ Object
reset class configuration
44 45 46 47 48 49 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 44 def reset # reset default values initialize_class end |
.reset_cache ⇒ Object
TODO: document me
321 322 323 324 325 326 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 321 def reset_cache # reset behaviour cache @behaviours_cache = {} end |
.to_xml(rule) ⇒ Object
TODO: document me
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/tdriver/base/behaviour/factory.rb', line 281 def to_xml( rule ) MobyUtil::XML.build{ | xml | # root element xml.behaviours{ # iterate each behaviour that meets the rule collect_behaviours( rule ).each{ | behaviour | # behaviour element xml.behaviour( :name => behaviour[ :name ], :object_type => behaviour[ :object_type ].sort.join(";") ){ # behaviour methods element xml.object_methods{ behaviour[ :methods ].each_pair{ | key, value | xml.object_method( :name => key.to_s ){ xml.description value[ :description ] xml.example value[ :example ] } # object_method } # methods.each_pair } # object_methods } # behaviour } # behaviours.each } # root }.to_xml end |