Sha256: 432f750a2e4d2bcd072ed9bcee9a070730613f25ff8bb0788981bcba560fd9d8

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module Spec
  module Example
    module BeforeAndAfterHooks
      
      # Translate hooks(before and after) keywords
      #
      def register_hooks
        natural_language.before_and_after_keywords.each do |key, values|
          values.collect { |value| alias_method value, key }
        end
      end
  
      # OverWriting a methor for rspec to work with hooks parameters
      #
      def before_parts(scope)

        scope = grep_language_and_scope(scope) || scope

        case scope
        when :each; before_each_parts
        when :all; before_all_parts
        when :suite; before_suite_parts
        end
      end
      
      # OverWriting a methor for rspec to work with hooks parameters
      #
      def after_parts(scope)
        
        scope = grep_language_and_scope(scope)
        
        case scope
        when :each; after_each_parts
        when :all; after_all_parts
        when :suite; after_suite_parts
        end
      end
      
      # Grep the tranlated scope
      #
      # pt:
      #   hooks:
      #     each: cada
      #
      #  grep_language_and_scope(:cada) # => :each
      #  grep_language_and_scope(:each) # => :each
      #
      def grep_language_and_scope(scope)
        if SpecI18n.spec_language
          hooks = natural_language.hooks_params_keywords
          scope = grep_the_scope(scope, hooks)
        else
          scope
        end
      end

      # Receive a String Scope and return the scope in english for
      # the rspec run the right method
      #
      # grep_the_scope(:cada, { 'each' => [ 'cada' ] }) # => :each
      # 
      #
      def grep_the_scope(scope, hooks)
        scopes = [:each, :all, :suite]
        return scope if scopes.include?(scope)
        hooks.each do |scope_in_english, language_hooks|
          return scope_in_english.to_sym if language_hooks.include?(scope.to_s)
        end
      end

    end
  end
end

include Spec::Example::BeforeAndAfterHooks

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-i18n-1.2.1 lib/spec-i18n/example/before_and_after_hooks.rb
rspec-i18n-1.2.0 lib/spec-i18n/example/before_and_after_hooks.rb