Module: OmfRc::ResourceProxyDSL::ClassMethods
- Defined in:
- lib/omf_rc/resource_proxy_dsl.rb
Overview
Methods defined here will be available in resource/utility definition files
Instance Method Summary collapse
- #call_hook(hook_name, context, *params) ⇒ Object
-
#configure(name) {|resource, value| ... } ⇒ Object
Register a configurable property.
-
#extend_configure(configure_name) ⇒ Object
Extend existing configure definition.
-
#extend_hook(hook_name) ⇒ Object
Extend existing hook definition by alias existing method name as “orig_”.
-
#extend_request(request_name) ⇒ Object
Extend existing request definition.
-
#extend_work(work_name) ⇒ Object
Extend existing work definition by alias existing method name as “orig_”.
-
#hook(name) {|resource| ... } ⇒ Object
Register some hooks which can be called at certain stage of the operation.
-
#property(name, opts = {}) ⇒ Object
Define internal property.
-
#register_proxy(name, opts = {}) ⇒ Object
Register a named proxy entry with factory class, normally this should be done in the proxy module.
-
#request(name) {|resource| ... } ⇒ Object
Register a property that could be requested.
-
#utility(name) ⇒ Object
Include the utility by providing a name.
-
#work(name) {|resource| ... } ⇒ Object
Define an arbitrary method to do some work, can be included in configure & request block.
Instance Method Details
#call_hook(hook_name, context, *params) ⇒ Object
146 147 148 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 146 def call_hook(hook_name, context, *params) context.send(hook_name, *params) if context.respond_to? hook_name end |
#configure(name) {|resource, value| ... } ⇒ Object
Register a configurable property
Please note that the result of the last line in the configure block will be returned via ‘inform’ message. If you want to make sure that user get a proper notification about the configure operation, simply use the last line to return such notification
216 217 218 219 220 221 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 216 def configure(name, ®ister_block) define_method("configure_#{name.to_s}") do |*args, &block| args[0] = Hashie::Mash.new(args[0]) if args[0].class == Hash register_block.call(self, *args, block) if register_block end end |
#extend_configure(configure_name) ⇒ Object
Extend existing configure definition
Slightly different to extend_hook, the actual method_name defined by a configure property is “configure_”
335 336 337 338 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 335 def extend_configure(configure_name) configure_name = configure_name.to_s alias_method "orig_configure_#{configure_name}", "configure_#{configure_name}" end |
#extend_hook(hook_name) ⇒ Object
Extend existing hook definition by alias existing method name as “orig_”
298 299 300 301 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 298 def extend_hook(hook_name) hook_name = hook_name.to_s alias_method "orig_#{hook_name}", hook_name end |
#extend_request(request_name) ⇒ Object
Extend existing request definition
344 345 346 347 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 344 def extend_request(request_name) request_name = request_name.to_s alias_method "orig_request_#{request_name}", "request_#{request_name}" end |
#extend_work(work_name) ⇒ Object
Extend existing work definition by alias existing method name as “orig_”
307 308 309 310 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 307 def extend_work(work_name) work_name = work_name.to_s alias_method "orig_#{work_name}", work_name end |
#hook(name) {|resource| ... } ⇒ Object
Register some hooks which can be called at certain stage of the operation
Currently the system supports these hooks:
-
before_ready, called when a resource created, before creating an associated pubsub topic
-
before_release, called before a resource released
-
before_create, called before parent creates the child resource. (in the context of parent resource)
-
after_create, called after parent creates the child resource.
-
after_initial_configured, called after child resource created, and initial set of properties have been configured.
The sequence of execution is:
-
before_create
-
before_ready
-
after_create
-
after_initial_configured
-
before_release
139 140 141 142 143 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 139 def hook(name, ®ister_block) define_method(name) do |*args, &block| register_block.call(self, *args, block) if register_block end end |
#property(name, opts = {}) ⇒ Object
Define internal property. Refer to options section to see supported options.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 367 def property(name, opts = {}) opts = Hashie::Mash.new(opts) define_method("default_property_#{name}") do |*args, &block| self.property[name] ||= opts[:default] end if opts.access.instance_of? Array access = opts.access elsif opts.access.instance_of? Symbol access = case opts.access when :configure [:configure, :request] when :init_only [:init, :request] when :read_only [:request] else raise ArgumentError, "Unknown property access mode '#{opts.access}'" end end access ||= DEFAULT_PROP_ACCESS access.each do |a| case a when :configure define_method("configure_#{name}") do |val| self.property[name] = val end when :init define_method("initialise_#{name}") do |val| self.property[name] = val end when :request define_method("request_#{name}") do self.property[name] end else raise ArgumentError, "Unnown access type '#{a}'" end end end |
#register_proxy(name, opts = {}) ⇒ Object
Register a named proxy entry with factory class, normally this should be done in the proxy module
53 54 55 56 57 58 59 60 61 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 53 def register_proxy(name, opts = {}) name = name.to_sym opts = Hashie::Mash.new(opts) if opts[:create_by] && !opts[:create_by].kind_of?(Array) opts[:create_by] = [opts[:create_by]] end opts[:proxy_module] = self OmfRc::ResourceFactory.register_proxy(name => opts) end |
#request(name) {|resource| ... } ⇒ Object
Register a property that could be requested
243 244 245 246 247 248 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 243 def request(name, ®ister_block) define_method("request_#{name.to_s}") do |*args, &block| args[0] = Hashie::Mash.new(args[0]) if args[0].class == Hash register_block.call(self, *args, block) if register_block end end |
#utility(name) ⇒ Object
Include the utility by providing a name
The utility file can be added to the default utility directory UTIL_DIR, or defined inline.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 164 def utility(name) name = name.to_s begin # In case of module defined inline include "OmfRc::Util::#{name.camelize}".constantize extend "OmfRc::Util::#{name.camelize}".constantize rescue NameError begin # Then we try to require the file and include the module require "#{UTIL_DIR}/#{name}" include "OmfRc::Util::#{name.camelize}".constantize extend "OmfRc::Util::#{name.camelize}".constantize rescue LoadError => le logger.error le. rescue NameError => ne logger.error ne. end end end |
#work(name) {|resource| ... } ⇒ Object
Define an arbitrary method to do some work, can be included in configure & request block
266 267 268 269 270 |
# File 'lib/omf_rc/resource_proxy_dsl.rb', line 266 def work(name, ®ister_block) define_method(name) do |*args, &block| register_block.call(self, *args, block) if register_block end end |