Sha256: e7e3ee86e38849535e3d64a7c2e9f7795a21c86754621e4d6cd342d7c2fdb891

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/hash'

module Kookaburra
  # This is the mechanism for sharing state between Cucumber steps.
  # If you're using instance variables, YOU'RE DOING IT WRONG.
  class TestData
    Defaults = HashWithIndifferentAccess.new

    def initialize
      @data = Hash.new do |hash, key|
        hash[key] = Hash.new { |hash, key| hash[key] = HashWithIndifferentAccess.new }
      end
    end

    def __collection(collection_key)
      @data[collection_key]
    end

    def __fetch_data(collection_key, value_key)
      __collection(collection_key).fetch(value_key)
    rescue IndexError => e
      raise e.exception("Key #{value_key.inspect} not found in #{collection_key}")
    end

    def __get_data(collection_key, value_key)
      __collection(collection_key)[value_key]
    end

    def __set_data(collection_key, value_key, value_hash = {})
      __collection(collection_key)[value_key] = HashWithIndifferentAccess.new(value_hash)
    end

    class << self
      def provide_collection(name)
        class_eval <<-RUBY
        def #{name}(key = :default)
          __get_data(:#{name}, key)
        end
        def fetch_#{name}(key = :default)
          __fetch_data(:#{name}, key)
        end
        def set_#{name}(key, value_hash = {})
          __set_data(:#{name}, key, value_hash)
        end
        RUBY
      end

      def set_default(key, value)
        Kookaburra::TestData::Defaults[key] = value
      end

      def default(key)
        Defaults[key]
      end
    end

    def default(key)
      (@defaults ||= Defaults.deep_dup)[key]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kookaburra-0.13.0 lib/kookaburra/test_data.rb
kookaburra-0.12.0 lib/kookaburra/test_data.rb
kookaburra-0.11.0 lib/kookaburra/test_data.rb
kookaburra-0.10.0 lib/kookaburra/test_data.rb
kookaburra-0.9.1 lib/kookaburra/test_data.rb
kookaburra-0.9.0 lib/kookaburra/test_data.rb
kookaburra-0.8.0 lib/kookaburra/test_data.rb
kookaburra-0.7.2 lib/kookaburra/test_data.rb
kookaburra-0.7.1 lib/kookaburra/test_data.rb