Class: RGhost::Grid::Base

Inherits:
PsObject show all
Includes:
CallbackFacade, RubyToPs
Defined in:
lib/rghost/grid/base_grid.rb

Overview

d.set grid

Direct Known Subclasses

CSV, Matrix, Rails

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CallbackFacade

#after_column, #before_column, #before_row, #even_column, #even_row, #odd_column, #odd_row

Methods included from RubyToPs

#array_to_stack, #hash_to_array, #pack_string, #ps_escape, #string_eval, #to_array, #to_bool, #to_string, #to_string_array

Methods inherited from PsObject

#<<, #call, #graphic_scope, #raw, #set, #to_s

Constructor Details

#initialize(options = {}) ⇒ Base

Options

  • :headings - To disable headers set this attribute to false.

  • :column_padding - Padding between column content and its edge.

  • :width - Width for all columns.

  • :align - Align for all data(:left, :center and :right).

  • :title_align - Align for all the header’s name(:left, :center and :right).



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rghost/grid/base_grid.rb', line 53

def initialize(options={})
  
  @header=RGhost::Grid::Header.new( (options[:headings] == false)?false:true  )
  @header.default_options(options)
  @callbacks=RGhost::PsObject.new
  @column_padding= options[:column_padding] || 0.1
  @record_count=1
  @data=[]
  @data[0]=[]
  @data_index=0
  @max_stack=RGhost::Config::GS[:stack_elements]
end

Instance Attribute Details

#column_paddingObject

Returns the value of attribute column_padding.



43
44
45
# File 'lib/rghost/grid/base_grid.rb', line 43

def column_padding
  @column_padding
end

#headerObject (readonly)

Returns the value of attribute header.



42
43
44
# File 'lib/rghost/grid/base_grid.rb', line 42

def header
  @header
end

Instance Method Details

#col(title = "", options = {}) ⇒ Object

Defines properties of a column. Parameters are the same as for new, plus some additional ones like :format.

  • :format - Format of the data. You can format data in four

    different ways with Rghost, passing in a Symbol
    a String a Class or Proc.
    

:format Parameters type

  • Symbol - Searches for a method defined as Grid::FieldFormat::method_name

:format => :eurodate
  • Class - A class that inherits Grid::FieldFormat::Custom with a overridden format method.

:format => MyFormat
  • String - Formats using the same parameters used in sprintf

:format => "%0.2f"
  • Proc - A block. In the example a text limited to 9 characters.

:format => lambda {|s| s.gsub(/^(.{9}).*$/,'\1...')}

Customizing formats

Replace spaces with a double dash.

class MyFormat < DataGrid::FieldFormat::Custom
 def format
    @value.to_s.gsub(/ /,'--')
 end
end

Using

grid.column :title => "Name", :format => MyFormat

Below, the columns with their proper formats.

grid.column :title => "Code",:format => "(%d)", :width => 1
grid.column :title => "Name",  :format => MyFormat
grid.column :title => "Date",  :format => lambda {|date| date.strftime("%d/%m/%Y") }
values=[
  [1,"Name 1", Time.now],
  [2,"Name 2", Time.now],
  [3,"Name 3", Time.now],
  [4,"Name 4", Time.now],
  [5,"Name 5", Time.now]
]
grid.data(values)

Add the Grid to a document

d=Document.new
d.set grid



112
113
114
115
116
117
118
119
120
# File 'lib/rghost/grid/base_grid.rb', line 112

def col(title="", options={})
  if title.is_a? Hash
    
    @header.col(title[:title],title)
  else
    
    @header.col(title,options)
  end
end

#column(title = "", options = {}) ⇒ Object

Alias for col



122
123
124
125
# File 'lib/rghost/grid/base_grid.rb', line 122

def column(title="", options={})
  col(title,options)

end

#data(data) ⇒ Object

Defines data to grid processor.



167
168
169
# File 'lib/rghost/grid/base_grid.rb', line 167

def data(data)

end

#format_field(value, type) ⇒ Object

:nodoc:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rghost/grid/base_grid.rb', line 127

def format_field(value,type) #:nodoc:
  case type
  when Symbol
    RGhost::Grid::FieldFormat.send(type,value)
  when String
    RGhost::Grid::FieldFormat.string(type % value)
  when NilClass
    RGhost::Grid::FieldFormat.string(value)
  when Class
    type.new(value).gs_format
  when Proc
    RGhost::Grid::FieldFormat.string(type.call(value))

  else raise TypeError.new("type=#{type}, value type=#{value.class}")
  end

end

#proc_line(line) ⇒ Object

:nodoc:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rghost/grid/base_grid.rb', line 147

def proc_line(line) #:nodoc:
  h=@header.data_types
  rec=[]
  line.each_with_index do |v,i|
    #puts "#{i} == #{h[i]} = #{v}, #{format_field(v,h[i])}"
    rec << format_field(v,h[i])
  end
  @data[@data_index] <<  "[#{rec.join(' ')}]\n"


  if @record_count == @max_stack
    @record_count=0
    @data_index+=1
    @data[@data_index]=[]
  end
  @record_count+=1


end

#psObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rghost/grid/base_grid.rb', line 172

def ps


  grid_names=[]
  p=RGhost::PsObject.new
  p.set RGhost::Variable.new(:col_padding,RGhost::Units::parse(@column_padding))
  @data.each do |ary|
    r=(rand*99999).to_i
    p.raw "/data#{r}[\n#{ary.join('')}\n] def"
    grid_names << r
  end
  p.raw "#{@header.ps} #{@callbacks}"

  g=RGhost::Graphic.new do
    raw :before_table_create
    raw grid_names.map{|m| " data#{m} table_proc \n" }.join('')
    raw :after_table_create
  end
  p.set g
  p.raw :nrdp
  p


end

#style(type = :border_lines) ⇒ Object

Grid has 3 preset styles :bottom_lines, :border_lines and old_forms. To set any of them, use:

grid.style(:border_lines)

:border_lines - instance of Grid::Style::BorderLines

:bottom_lines - instance of Grid::Style::BottomLines

:old_forms - instance of Grid::Style::OldForms



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rghost/grid/base_grid.rb', line 213

def style(type=:border_lines)
  st=case type
  when :border_lines
    RGhost::Grid::Style::BorderLines.new
  when :old_forms
    RGhost::Grid::Style::OldForms.new
  when :bottom_lines
    RGhost::Grid::Style::BottomLines.new
  else raise NameError.new("Why? #{type} ?")
  end

  st.set_style(self)

end

#widthObject



144
145
146
# File 'lib/rghost/grid/base_grid.rb', line 144

def width
  @header.size
end