Class: Crosstab::Banner

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

Instance Method Summary collapse

Methods inherited from Generic

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

Constructor Details

#initialize(&block) ⇒ Banner

Pass in a block and we’ll execute it within the context of this class. If no block is passed in, a “Total” column will be added automatically.

Example:

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


15
16
17
18
19
20
21
# File 'lib/crosstab/banner.rb', line 15

def initialize(&block)
  if block
    instance_eval(&block) 
  else
    column "Total"
  end
end

Instance Method Details

#column(name = nil, qualification = nil) ⇒ Object

Creates a new Crosstab::Column and appends it to the columns array.

Example:

columns
#=> []

column "Male", :a => 1

columns
#=> [Crosstab::Column...]


51
52
53
# File 'lib/crosstab/banner.rb', line 51

def column(name=nil,qualification=nil)
  columns << Crosstab::Column.new(name, qualification)
end

#columnsObject

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

Example:

columns
#=> []

column "Male", :a => 1

columns
#=> [Crosstab::Column...]


35
36
37
# File 'lib/crosstab/banner.rb', line 35

def columns
  @columns ||= []
end

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

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

Example:

columns
#=> []

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

columns
#=> [Crosstab::Column..., Crosstab::Column...]

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

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


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/crosstab/banner.rb', line 76

def group(name=nil, &block)
  if block
    old_columns = columns.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 columns
    (columns - old_columns).each do |col|
      col.group g 
    end
  end
end