Class: FeatureDefinitions

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_definitions.rb

Constant Summary collapse

IDENTITY =
Proc.new { |arg| arg }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FeatureDefinitions

Returns a new instance of FeatureDefinitions.



17
18
19
20
21
22
23
# File 'lib/feature_definitions.rb', line 17

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

Instance Attribute Details

#test_procObject (readonly)

Returns the value of attribute test_proc.



2
3
4
# File 'lib/feature_definitions.rb', line 2

def test_proc
  @test_proc
end

Class Method Details

.context=(context) ⇒ Object



25
26
27
# File 'lib/feature_definitions.rb', line 25

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

.define_feature(name, &feature_test_block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/feature_definitions.rb', line 4

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

Instance Method Details

#contextObject



29
30
31
# File 'lib/feature_definitions.rb', line 29

def context
  Thread.current[:FeatureDefinitionsTLS]
end

#enabled?(&block) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/feature_definitions.rb', line 33

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

#eval_test_procObject



41
42
43
44
45
46
47
# File 'lib/feature_definitions.rb', line 41

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