Class: ActiveGuide::Group

Inherits:
Item
  • Object
show all
Defined in:
lib/active_guide/group.rb

Instance Attribute Summary collapse

Attributes inherited from Item

#accept_block, #after_block, #before_block, #name, #parent

Instance Method Summary collapse

Methods inherited from Item

#accept, #after, #before, #guide, #unique_name

Constructor Details

#initialize(group, name, options = {}, &block) ⇒ Group


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

def initialize(group, name, options = {}, &block)
  super group, name, options
  @items = []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items


3
4
5
# File 'lib/active_guide/group.rb', line 3

def items
  @items
end

Instance Method Details

#add_item(item) ⇒ Object


34
35
36
37
38
39
# File 'lib/active_guide/group.rb', line 34

def add_item(item)
  unless item.is_a?(ActiveGuide::Item) || item.is_a?(ActiveGuide::Result)
    raise "Invalid item. Got #{item.inspect}:#{item.class.name}"
  end
  @items << item
end

#group(name, options = {}, &block) ⇒ Object


15
16
17
# File 'lib/active_guide/group.rb', line 15

def group(name, options = {}, &block)
  add_item Group.new(self, name, options, &block)
end

#question(name, options = {}, &block) ⇒ Object


19
20
21
# File 'lib/active_guide/group.rb', line 19

def question(name, options = {}, &block)
  add_item Question.new(self, name, options, &block)
end

#result(name, type = :numeric) ⇒ Object


11
12
13
# File 'lib/active_guide/group.rb', line 11

def result(name, type = :numeric)
  add_item Result.new(self, name, type)
end

#test(name, *args, &block) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/active_guide/group.rb', line 23

def test(name, *args, &block)
  options = args.extract_options!
  if block_given?
    add_item Test.new(self, name, options, &block)
  elsif (proc = args.shift) && proc.respond_to?(:call)
    add_item Test.new(self, name, options.merge(validate: proc))
  else
    raise "Cannot do anything with test #{name}"
  end
end