Sha256: bbe0238dd9165feb8b1f10748c4a680110fff522ec289800e6ea97d8b48d8885

Contents?: true

Size: 1.29 KB

Versions: 24

Compression:

Stored size: 1.29 KB

Contents

require 'active_support/core_ext/class/attribute'

module Kookaburra
  class UIDriver
    module HasStrategies
      class Strategy
        class_attribute :tag
        attr_reader :ui_component
        def initialize(ui_component)
          @ui_component, @tag = ui_component
        end
      end

      module ClassMethods
        def strategy(tag, &proc)
          Class.new(::Kookaburra::UIDriver::HasStrategies::Strategy).tap { |klass|
            klass.tag = tag
            klass.module_eval &proc
            self.strategy_classes << klass
          }
        end

        def use_strategy_for(*method_names)
          def_delegators :current_strategy, *method_names
        end
      end

      module InstanceMethods
        def strategies
          @strategies ||= strategy_classes.map { |klass| klass.new(self) }
        end

        def current_strategy
          strategies.detect(&:applies?) or raise 'No applicable strategy!'
        end

        def strategy_tag
          current_strategy.tag
        end
      end

      def self.included(receiver)
        receiver.class_attribute :strategy_classes
        receiver.strategy_classes = []

        receiver.extend Forwardable
        receiver.extend         ClassMethods
        receiver.send :include, InstanceMethods
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
kookaburra-0.3.0 lib/kookaburra/ui_driver/mixins/has_strategies.rb
kookaburra-0.2.0 lib/kookaburra/ui_driver/mixins/has_strategies.rb
kookaburra-0.1.1 lib/kookaburra/ui_driver/mixins/has_strategies.rb
kookaburra-0.1.0 lib/kookaburra/ui_driver/mixins/has_strategies.rb