Class: Vigia::Sail::Group

Inherits:
RSpecObject show all
Defined in:
lib/vigia/sail/group.rb

Instance Attribute Summary

Attributes inherited from RSpecObject

#name, #options, #rspec

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RSpecObject

clean!, #contextual_object, inherited, #initialize, register, setup_and_run

Methods included from Hooks

#execute_hook, #hooks_for_object, #with_hooks

Constructor Details

This class inherits a constructor from Vigia::Sail::RSpecObject

Class Method Details

.setup_and_run_primary(rspec) ⇒ Object



5
6
7
8
9
# File 'lib/vigia/sail/group.rb', line 5

def setup_and_run_primary(rspec)
  name, options  = collection.select{ |k,v| v[:primary] == true }.first
  instance = new(name, options, rspec)
  instance.run
end

Instance Method Details

#childrenObject



25
26
27
# File 'lib/vigia/sail/group.rb', line 25

def children
  [ *options[:children] ] || []
end

#contextsObject



29
30
31
# File 'lib/vigia/sail/group.rb', line 29

def contexts
  [ *options[:contexts] ] || []
end

#create_group_instance(object, index) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/vigia/sail/group.rb', line 47

def create_group_instance(object, index)
  instance_name = "#{ name }#{ index }"
  Vigia::Sail::GroupInstance.new(instance_name, options, rspec).tap do |instance|
    instance.define_singleton_method("#{ name }", -> { object })
    instance.parent_object    = rspec.described_class.described_object unless options[:primary]
    instance.described_object = object
    instance.description      = (options[:description] || object.to_s)
    instance.group            = self
  end
end

#described_objectsObject



33
34
35
# File 'lib/vigia/sail/group.rb', line 33

def described_objects
  contextual_object(option_name: :describes) || []
end

#include_recursion(object, rspec_context) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/vigia/sail/group.rb', line 37

def include_recursion(object, rspec_context)
  describes = contextual_object(option_name: :recursion, context: object) || []
  return if describes.empty?
  
  self.clone.tap do |recursive_group|
    recursive_group.options[:describes] = describes
    recursive_group.instance_variable_set('@rspec', rspec_context)
  end.run
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vigia/sail/group.rb', line 12

def run
  described_objects.each_with_index do |object, index|
    group_instance = create_group_instance(object, index)
    rspec.describe group_instance do
      let(group_instance.group.name) { group_instance.described_object }

      group_instance.group.include_recursion(group_instance.described_object, self)

      group_instance.run(self)
    end
  end
end