Class: Crosstab::Table

Inherits:
Generic show all
Defined in:
lib/crosstab/table.rb

Instance Method Summary collapse

Methods inherited from Generic

#printed?, #qualification, #qualifies?, #title

Constructor Details

#initialize(&block) ⇒ Table

Pass in a block and we’ll execute it within the context of this class.

Example:

my_crosstab = Crosstab.new do
  table do
    row "Male", :a => 1
    row "Female", :a => 2
  end
end


13
14
15
# File 'lib/crosstab/table.rb', line 13

def initialize(&block)
  instance_eval(&block) if block
end

Instance Method Details

#group(name = nil, &block) ⇒ Object

DSL child setter, creates a new Group, and rows created within its block will be assigned to that group.

Example:

rows
#=> []

group "Gender" do
  row "Male", :a => 1
  row "Female", :a => 2
end

rows
#=> [Crosstab::Row..., Crosstab::Row...]

rows[0].group.title
# => "Gender"

rows[1].group.title
# => "Gender"


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/crosstab/table.rb', line 70

def group(name=nil, &block)
  if block
    old_rows = rows.dup # Save current state

    instance_eval(&block) # Execute block within current scope

    g = Crosstab::Group.new(name) # Create new group
    
    # Set group for all of the new rows
    (rows - old_rows).each do |row|
      row.group g 
    end
  end
end

#row(name, qualification) ⇒ Object

Creates a new Crosstab::Row and appends it to the rows array.

Example:

rows
#=> []

row "Male", :a => 1

rows
#=> [Crosstab::Row...]


45
46
47
# File 'lib/crosstab/table.rb', line 45

def row(name,qualification)
  rows << Crosstab::Row.new(name, qualification)
end

#rowsObject

attr_reader for the rows attribute which should contain an empty array, or a list of rows

Example:

rows
#=> []

row "Male", :a => 1

rows
#=> [Crosstab::Row...]


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

def rows
  @rows ||= []
end