File: callback_facade.rb

package info (click to toggle)
ruby-rghost 0.9.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: ruby: 3,374; makefile: 6; sh: 1
file content (131 lines) | stat: -rw-r--r-- 3,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module RGhost::Grid
  # The callbacks for the grid are defined here. Let's see them in action.
  # ===Grid::CallbackFacade examples
  #  grid=Grid::Matrix.new :column_padding => 1
  #  grid.column :title => "Id", :width => 1
  #  grid.column :title => "Name", :width => 3, :align => :center
  #  grid.column :title => "Date", :width => 3, :align => :right, :title_align => :center,  :format => lambda{|v| v.strftime("%d/%m/%Y")}
  #  values=('A'..'E').to_a.map{|v|  [v,"Name #{v}", Time.now]}
  #
  # even_row:
  #  grid.even_row do |e|
  #   e.background_row(:size => grid.width)
  #  end
  # link:images/grid01.png
  #
  # Now before_row to create a top and bottom line:
  #  grid.before_row do |b|
  #   b.horizontal_line(:top,    :size => grid.width )
  #   b.horizontal_line(:bottom, :size => grid.width)
  #  end
  #
  # link:images/grid02.png
  #
  # before_column:
  #  grid.before_column do |v|
  #   v.vertical_line_row
  #  end
  #
  # link:images/grid03.png
  #
  # after_column:
  #  grid.after_column {|c|  c.vertical_line_row  }
  #
  #
  # link:images/grid04.png
  #
  # Moving to the header
  #  grid.header.before_create do |b|
  #   b.horizontal_line(:top, :size => grid.width)
  #  end
  #
  # link:images/grid05.png
  #
  # Finishing the grid lines:
  #  grid.header.before_column do |b|
  #    b.vertical_line_row
  #  end
  #
  #
  #  grid.header.after_column do |b|
  #   b.vertical_line_row
  #  end
  #
  # link:images/grid06.png
  #
  # Now a adding a bold font to the header
  #  grid.header.before_create do |b|
  #    b.horizontal_line(:top, :size => grid.width)
  #    b.use_tag :bold
  #   end
  #
  # link:images/grid07.png
  #
  # Oops. Not quite what we expected, the entire grid used bold
  # face. We need to use a header callback to reset the font.
  #
  #  grid.header.after_create do
  #     b.use_tag :normal
  #   end
  #
  # link:images/grid08.png
  #
  # Don't forget
  #
  #  doc=Document.new
  #  doc.set grid
  module CallbackFacade
    # Executes before processing row. Responds to :only and :except
    # options.
    def before_row(options = {}, &)
      new_dynamic_callback(:before_row, options, &)
    end

    # Executes on creating an odd rows. Responds to :only and :except
    # options.
    def odd_row(options = {}, &)
      new_dynamic_callback(:odd_row, options, &)
    end

    # Executes upon creating even rows. Responds to :only and :except
    # options.
    def even_row(options = {}, &)
      new_dynamic_callback(:even_row, options, &)
    end

    # Executes before creating a column. Responds to :only and :except
    # options.
    def before_column(options = {}, &)
      new_dynamic_callback(:before_column, options, &)
    end

    # Executes after a column was created. Responds to :only and
    # :except options.
    def after_column(options = {}, &)
      new_dynamic_callback(:after_column, options, &)
    end

    # Executes when creating an odd column. Responds to :only and
    # :except options.
    def odd_column(options = {}, &)
      new_dynamic_callback(:odd_column, options, &)
    end

    # Executes upon creating an even column. Responds to :only and
    # :except options.
    def even_column(options = {}, &)
      new_dynamic_callback(:even_column, options, &)
    end

    private

    def new_dynamic_callback(name, options = {}, &)
      @callbacks.set RGhost::Callback.new(name, options, &)
    end

    def new_static_callback(name, &)
      callback_body = RGhost::PsFacade.new(&)
      @callbacks.set RGhost::Function.new(name, callback_body)
    end
  end
end