Sha256: d461e96b41ad3992b28256e311cad22ea5674ad7ed062e3101023f8d759752e4

Contents?: true

Size: 960 Bytes

Versions: 1

Compression:

Stored size: 960 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

  IDENTITY = Proc.new { |arg| arg }

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

  def self.context=(context)
    Thread.current[:FeatureDefinitionsTLS] = context
  end

  def context
    Thread.current[:FeatureDefinitionsTLS]
  end

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

  def eval_test_proc
    if test_proc.arity == 0
      context.instance_exec(&test_proc)
    else
      test_proc.call(context)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
feature_definitions-0.4.0 lib/feature_definitions.rb