Sha256: df5544e2167bc4c244fdf2468a62def9a0a3d7862f49e8590c3e73106886edc3

Contents?: true

Size: 793 Bytes

Versions: 1

Compression:

Stored size: 793 Bytes

Contents

class FeatureDefinitions
  attr_reader :test_proc

  def self.define_feature(name, &feature_test_block)
    feature = new(&feature_test_block)
    meta_class = class << self; self end
    meta_class.__send__(:define_method, name) do |&feature_impl_block|
      if block_given?
        feature.enabled?(&feature_impl_block)
      end
      feature
    end
  end

  PASSTHROUGH = Proc.new { |arg| arg }

  def initialize(&block)
    if block_given?
      @test_proc = block.to_proc
    else
      @test_proc = PASSTHROUGH
    end
  end

  @@context = nil

  def self.context=(context)
    @@context = context
  end

  def context
    @@context
  end

  def enabled?(&block)
    test_proc.call(context).tap do |verdict|
      if verdict and block_given?
        yield
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
feature_definitions-0.3.1 lib/feature_definitions.rb