Class: TableHelper::RowBuilder

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/table_helper/row.rb

Overview

Provides a blank class that can be used to build the cells for a row

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ RowBuilder

Creates a builder for the given row



9
10
11
# File 'lib/table_helper/row.rb', line 9

def initialize(row)
  @row = row
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

Proxies all missed methods to the row



14
15
16
# File 'lib/table_helper/row.rb', line 14

def method_missing(*args)
  @row.send(*args)
end

Instance Method Details

#define_cell(name) ⇒ Object

Defines the builder method for the given cell name. For example, if a cell with the name :title was defined, then the cell would be able to be read and written like so:

row.title               #=> Accesses the title
row.title "Page Title"  #=> Creates a new cell with "Page Title" as the content


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/table_helper/row.rb', line 24

def define_cell(name)
  method_name = name.gsub('-', '_')

  klass = class << self; self; end
  klass.class_eval do
    define_method(method_name) do |*args|
      if args.empty?
        @row.cells[name]
      else
        @row.cell(name, *args)
      end
    end
  end unless klass.method_defined?(method_name)
end

#undef_cell(name) ⇒ Object

Removes the definition for the given cell



40
41
42
43
44
45
46
47
# File 'lib/table_helper/row.rb', line 40

def undef_cell(name)
  method_name = name.gsub('-', '_')

  klass = class << self; self; end
  klass.class_eval do
    remove_method(method_name)
  end
end