lib/taza/site.rb in taza-0.8.0 vs lib/taza/site.rb in taza-0.8.2

- old
+ new

@@ -43,46 +43,47 @@ # Google.new.search.set "taza" # # Sites can take a couple of parameters in the constructor: # :browser => a browser object to act on instead of creating one automatically # :url => the url of where to start the site - def initialize(params={}) + def initialize(params={},&block) @module_name = self.class.parent.to_s @class_name = self.class.to_s.split("::").last define_site_pages + define_flows config = Settings.config(@class_name) if params[:browser] @browser = params[:browser] else @browser = Browser.create(config) @i_created_browser = true end @browser.goto(params[:url] || config[:url]) + execute_block_and_close_browser(browser,&block) if block_given? + end - if block_given? + def execute_block_and_close_browser(browser) + begin + yield self + rescue => site_block_exception + ensure begin - yield self - rescue => site_block_exception - ensure - begin - @@before_browser_closes.call(browser) - rescue => before_browser_closes_block_exception - end - close_browser_and_raise_if site_block_exception || before_browser_closes_block_exception + @@before_browser_closes.call(browser) + rescue => before_browser_closes_block_exception + "" # so basically rcov has a bug where it would insist this block is uncovered when empty end + close_browser_and_raise_if site_block_exception || before_browser_closes_block_exception end end def self.settings # :nodoc: Taza::Settings.site_file(self.name.to_s.split("::").last) end def close_browser_and_raise_if original_error # :nodoc: begin - if @i_created_browser - @browser.close - end + @browser.close if @i_created_browser ensure raise original_error if original_error end end @@ -100,10 +101,16 @@ end EOS end end + def define_flows # :nodoc: + Dir.glob(flows_path) do |file| + require file + end + end + # This is used to call a flow belonging to the site # # Example: # Google.new do |google| # google.flow(:perform_search, :query => "taza") @@ -116,17 +123,16 @@ # def run(params={}) # google.search.set params[:query] # google.submit.click # end # end - def flow(name,params={}) - require File.join(path,'flows',name.to_s.underscore) - flow_class = "#{@module_name}::#{name.to_s.camelize}".constantize - flow_class.new(self).run(params) - end def pages_path # :nodoc: - File.join(path,'pages','*.rb') + File.join(path,'pages','**','*.rb') + end + + def flows_path # :nodoc: + File.join(path,'flows','*.rb') end def path # :nodoc: File.join(base_path,'lib','sites',@class_name.underscore) end