Class: Funktional::ShouldBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/funktional/context/should_block.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(should_name, context, &blk) ⇒ ShouldBlock

Returns a new instance of ShouldBlock.



19
20
21
22
# File 'lib/funktional/context/should_block.rb', line 19

def initialize(should_name, context, &blk)
  raise 'block required' unless block_given?
  @should_name, @blk, @context = should_name, blk, context
end

Class Method Details

.build(options, context, &blk) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/funktional/context/should_block.rb', line 4

def self.build(options, context, &blk)
  return self.new(name = options, context, &blk) if options.is_a? String
  
  options = {:render_404 => 'public/404'} if options == :render_404
  
  case options.keys.first
    when :create
      ShouldCreateBlock.new(options[:create], context)
    when :delete
      ShouldDeleteBlock.new(options[:delete], context)
    else
      DelegatingShouldBlock.new(options, context, &blk)
  end
end

Instance Method Details

#to_testObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/funktional/context/should_block.rb', line 24

def to_test
  context, blk, before = @context, @blk, @before
  
  @context.test_unit_class.send(:define_method, build_test_name) do
    begin
      context.run_parent_setup_blocks(self)
      before.bind(self).call if before
      
      context.run_current_setup_blocks(self)
      blk.bind(self).call
    ensure
      context.run_all_teardown_blocks(self)
    end
  end
end