Best Test-prof_ruby code snippet using ClassMethods.run
acts_as_job.rb
Source:acts_as_job.rb
...16 # job completes.17 #18 # Instances of the including class also get the following instance methods:19 # - {#job} Returns the Job::Definition for the class20 # - {#job_run} Returns the Job::Run associated with this object instance.21 module ActsAsJob22 # Define methods to be added to the class that includes this module.23 module ClassMethods24 # @return The Job::Definition object used to hold attributes of this25 # job.26 def job_definition27 @__job__28 end29 alias_method :definition, :job_definition30 # Captures a description for the following task or job definition.31 #32 # @param desc [String] The description to associate with the next33 # task or job that is defined.34 def desc(desc)35 @__desc__ = desc36 end37 # Defines the method that is used to run this job.38 # This may be an existing method, in which case the name of the39 # method must be passed as the first argument.40 # Alternatively, a block may be supplied, which will be used to41 # create the job method.42 #43 # @param job_method [Symbol] The name of an existing method that is44 # to be the job entry point.45 # @param job_opts [Hash] Options that affect the job definition.46 # @option job_opts [Symbol] :method_name The name to be assigned to47 # the job method created from the supplied block. Default is48 # :execute.49 # @option job_opts [String] :description A description for the job.50 def job(job_method = nil, job_opts = @__desc__, &body)51 # If called as an accessor, just return the @__job__52 if job_method || job_opts || body53 unless job_method.is_a?(Symbol)54 job_opts = job_method55 job_method = (job_opts && job_opts.is_a?(Hash) &&56 job_opts[:method_name]) || :execute57 end58 job_desc = nil59 if job_opts.is_a?(Hash)60 job_desc = @__desc__61 elsif job_opts.is_a?(String)62 job_desc = job_opts63 job_opts = {}64 elsif job_opts.nil?65 job_opts = {}66 end67 @__desc__ = nil68 # Define job method if a body block was supplied69 define_method(job_method, &body) if body70 opts = job_opts.clone71 opts[:description] = job_desc unless opts[:description]72 opts[:method_name] = job_method73 # The @__job__ instance variable is crated when this module is included74 @__job__.set_from_options(opts)75 end76 @__job__77 end78 # Defines the method that is used to run a task.79 # This may be an existing method, in which case the name of the80 # method must be passed as the first argument.81 # Alternatively, a block may be supplied, which will be used to82 # create the task method.83 #84 # @param task_method [Symbol] The name for the method that is to be85 # this task. May be the name of an existing method (in which case86 # no block should be supplied), or the name to give to the method87 # that will be created from the supplied block.88 # @param task_opts [Hash] A hash containing options for the task89 # being defined.90 # @option task_opts [Symbol] :method_name The name for the method91 # if no symbol was provided as the first argument.92 # @option job_opts [String] :description A description for the task.93 def task(task_method, task_opts = @__desc__, &body)94 unless task_method.is_a?(Symbol)95 task_opts = task_method96 task_method = task_opts && task_opts[:method_name]97 end98 raise ArgumentError, "No method name specified for task" unless task_method99 task_desc = nil100 if task_opts.is_a?(Hash)101 task_desc = @__desc__102 elsif task_opts.is_a?(String)103 task_desc = task_opts104 task_opts = {}105 elsif task_opts.nil?106 task_opts = {}107 end108 @__desc__ = nil109 # Define task method if a body block was supplied110 define_method(task_method, &body) if body111 opts = task_opts.clone112 opts[:description] = task_desc unless opts[:description]113 # Create a new TaskDefinition class for the task114 task_defn = Task::Definition.new(self, task_method)115 task_defn.set_from_options(opts)116 task_defn117 end118 # Defines a handler to be invoked if the job encounters an unhandled119 # exception.120 def on_failure(mthd = nil, &blk)121 Events.subscribe(self, 'job_run.failure'){ |obj, jr, ex| obj.send(mthd, jr, ex) } if mthd122 Events.subscribe(self, 'job_run.failure'){ |obj, jr, ex| obj.instance_exec(jr, ex, &blk) } if blk123 end124 # Defines a handler to be invoked if the job ends successfully.125 def on_success(mthd = nil, &blk)126 Events.subscribe(self, 'job_run.success'){ |obj, jr| obj.send(mthd, jr) } if mthd127 Events.subscribe(self, 'job_run.success'){ |obj, jr| obj.instance_exec(jr, &blk) } if blk128 end129 # Defines a handler to be invoked on completion of the job, whether130 # the job completes successfully or fails. The handler may be specified131 # as either a method name and/or via a block. Multiple calls to this132 # method can be made to register multiple callbacks if desired.133 #134 # @param mthd [Symbol] The name of an existing method on the including135 # class. This method will be called with the Job::Run object that136 # represents the completing job run.137 #138 def on_completion(mthd = nil, &blk)139 Events.subscribe(self, 'job_run.post-execute'){ |obj, jr, ok| obj.send(jr, ok, mthd) } if mthd140 Events.subscribe(self, 'job_run.post-execute'){ |obj, jr, ok| obj.instance_exec(jr, ok, &blk) } if blk141 end142 end143 # Hook used to extend the including class with class methods defined in144 # the ActsAsJob::ClassMethods module.145 #146 # Creates a Job::Definition object to hold details of the job, and stores147 # it away in a @__job__ class instance variable.148 def self.included(base)149 caller.find{ |f| !(f =~ /batch-kit.framework/) } =~ /^((?:[a-zA-Z]:)?[^:]+)/150 job_file = File.realpath($1)151 job_defn = Job::Definition.new(base, job_file)152 base.instance_variable_set :@__job__, job_defn153 base.extend(ClassMethods)154 Events.publish(base, 'acts_as_job.included', job_defn)155 end156 # @return [Job::Definition] The JobDefinition for this job instance.157 def job158 self.class.job_definition159 end160 # @return [Job::Run] The JobRun for this job instance.161 def job_run162 @__job_run__163 end164 end165end...
base.rb
Source:base.rb
...111 # end112 # 113 # end114 # 115 # App.run!116 #117 class Base < Sinatra::Base118 include Server::Routing119 include Server::Helpers120 include Server::Conditions121 # Default interface to run the Web Server on122 DEFAULT_HOST = '0.0.0.0'123 # Default port to run the Web Server on124 DEFAULT_PORT = 8000125 use Rack::UserAgent126 set :host, DEFAULT_HOST127 set :port, DEFAULT_PORT128 before do129 @request = Request.new(@env)130 @response = Response.new131 end132 not_found { [404, {'Content-Type' => 'text/html'}, ['']] }133 #134 # Run the web server.135 #136 # @param [Hash] options Additional options.137 #138 # @option options [String] :host139 # The host the server will listen on.140 #141 # @option options [Integer] :port142 # The port the server will bind to.143 #144 # @option options [String] :server145 # The Web Server to run on.146 #147 # @option options [Boolean] :background (false)148 # Specifies wether the server will run in the background or run149 # in the foreground.150 #151 # @api public152 #153 def self.run!(options={})154 set(options)155 handler = detect_rack_handler156 handler_name = handler.name.gsub(/.*::/, '')157 # rubocop:disable Lint/ShadowingOuterLocalVariable158 runner = lambda { |handler,server|159 begin160 handler.run(server,Host: bind, Port: port) do |server|161 trap(:INT) { quit!(server,handler_name) }162 trap(:TERM) { quit!(server,handler_name) }163 set :running, true164 end165 rescue Errno::EADDRINUSE166 warn "ronin-web-server: address is already in use: #{bind}:#{port}"167 end168 }169 # rubocop:enable Lint/ShadowingOuterLocalVariable170 if options[:background]171 Thread.new(handler,self,&runner)172 else173 runner.call(handler,self)174 end175 return self176 end177 #178 # Stops the web server.179 #180 # @param [#call] server181 # The Rack Handler server.182 #183 # @param [String] handler_name184 # The name of the handler.185 #186 # @api semipublic187 #...
provider.rb
Source:provider.rb
...42 # since we upgrade the Chef::Runner and Chef::RunContext globally to >= 12.14 style classes, we need to also43 # fix the use_inline_resources LWRPBase wrapper that creates a sub-resource collection with the ugpraded code44 # from the Chef::Provider subclasses that do similar things in post-12.5 chef.45 def recipe_eval_with_update_check(&block)46 old_run_context = run_context47 @run_context = run_context.create_child48 return_value = instance_eval(&block)49 Chef::Runner.new(run_context).converge50 return_value51 ensure52 if run_context.resource_collection.any? { |r| r.updated? }53 new_resource.updated_by_last_action(true)54 end55 @run_context = old_run_context56 end57 end58 end59 end60 end61end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!