Sha256: 4ce66fbcc3ae4abde306767b8ad27337ab78c259775033480ed8b52bb5e828da

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'dicer/delegator'

module Dicer
  class Context
    class Description
      def initialize(described_class, &block)
        @described_class = described_class
        @behaviors = {}
        @behaviors.default = []
        @delegators = {}

        instance_eval(&block) if block_given?
      end
      attr_reader :described_class, :behaviors

      def it_behaves_like(behavior)
        @behaviors[@role] << behavior
      end

      def role(name, &block)
        @role = name.to_s.to_sym
        @behaviors[@role] = [] unless @behaviors.has_key?(@role)
        instance_eval(&block) if block_given?
        @role = nil
      end

      def merge!(other)
        other.behaviors.each_pair do |role, behaviors|
          if @behaviors.has_key?(role)
            @behaviors[role] = @behaviors[role] | behaviors
          else
            @behaviors[role] = behaviors
          end
        end

        @behaviors.default = @behaviors.default | other.behaviors.default
      end

      def delegator
        @delegators.default ||= Dicer::Delegator.make(
          @described_class,
          @behaviors.default
        )
      end

      def delegator_for(role)
        role = role.to_s.to_sym

        if @delegators.has_key?(role)
          @delegators[role]
        else
          @delegators[role] = Dicer::Delegator.make(
            @described_class,
            @behaviors[role]
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dicer-0.3.0 lib/dicer/context/description.rb