Class: Gloo::Objs::Table

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/data/table.rb

Constant Summary collapse

KEYWORD =
'table'.freeze
KEYWORD_SHORT =
'tbl'.freeze
HEADERS =
'headers'.freeze
DATA =
'data'.freeze
CELLS =
'cells'.freeze
STYLES =
'styles'.freeze
ALWAYS_ROWS =
'always_rows'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #root?, #send_message, #set_parent, #set_value, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



165
166
167
# File 'lib/gloo/objs/data/table.rb', line 165

def self.messages
  return super + %w[show render]
end

.short_typenameObject

The short name of the object type.



30
31
32
# File 'lib/gloo/objs/data/table.rb', line 30

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



23
24
25
# File 'lib/gloo/objs/data/table.rb', line 23

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



142
143
144
# File 'lib/gloo/objs/data/table.rb', line 142

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



151
152
153
154
155
# File 'lib/gloo/objs/data/table.rb', line 151

def add_default_children
  fac = @engine.factory
  fac.create_can HEADERS, self
  fac.create_can DATA, self
end

#always_rowsObject

Always show rows, even if only 1 row is found.



48
49
50
51
52
53
# File 'lib/gloo/objs/data/table.rb', line 48

def always_rows
  o = find_child ALWAYS_ROWS

  return false unless o
  return o.value
end

#build_columns(result_data) ⇒ Object

Build the column list based on the result data and the headers defined in the table object.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/gloo/objs/data/table.rb', line 224

def build_columns result_data
  head_children = find_child HEADERS
  cell_renderers = find_child CELLS

  columns = []
  return columns unless result_data
  
  result_data.each_with_index do |c,index|
    visible = true
    name = c
    title = c
    display_index = index

    if head_children
      child = head_children.find_child c
      if child
        title = child.value
        display_index = head_children.child_index( c )
      else
        visible = false
      end
    end

    cell_renderer = nil
    if cell_renderers
      this_cr = cell_renderers.find_child( c )
      cell_renderer = this_cr.value if this_cr
    end

    columns << { 
      name: name, 
      title: title, 
      visible: visible, 
      data_index: index,
      display_index: display_index,
      cell_renderer: cell_renderer
    }
  end
  return columns.sort_by { |hsh| hsh[ :display_index ] }
end

#cell_renderersObject

Get cell renderer hash keyed by column name.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gloo/objs/data/table.rb', line 120

def cell_renderers
  h = {}
  o = find_child CELLS
  return h unless o

  o.children.each do |c|
    h[ c.name ] = c.value
  end

  return h
end

#columnsObject

Get the list of column names. Returns nil if there is none.



59
60
61
62
63
64
# File 'lib/gloo/objs/data/table.rb', line 59

def columns
  o = find_child HEADERS
  return [] unless o

  return o.children.map( &:name )
end

#dataObject

Get the list of data elements.



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
# File 'lib/gloo/objs/data/table.rb', line 69

def data
  o = find_child DATA
  return [] unless o
  
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return [] unless o
  
  if o.is_a? Gloo::Objs::Query
    @engine.log.debug "Table getting data from query."
    begin
      result = o.run_query
      return result
    rescue => e
      @engine.log_exception e
      return nil
    end
  else
    cols = self.columns

    if o.children&.first.children.empty?
      # It is a simgle row table.
      rows = [ cols.map { |h| o.find_child( h )&.value } ]
    else
      rows = o.children.map do |e|
        cols.map { |h| e.find_child( h )&.value }
      end
    end

    return [ cols, rows ]
  end
end

#headersObject

Get the list of headers. Returns nil if there is none.



38
39
40
41
42
43
# File 'lib/gloo/objs/data/table.rb', line 38

def headers
  o = find_child HEADERS
  return [] unless o

  return o.children.map( &:value )
end

#msg_renderObject



177
178
179
# File 'lib/gloo/objs/data/table.rb', line 177

def msg_render
  return render
end

#msg_showObject

Show the table in the CLI.



172
173
174
175
# File 'lib/gloo/objs/data/table.rb', line 172

def msg_show
  title = self.value
  @engine.platform.table.show headers, data[1], title
end

#render(render_ƒ) ⇒ Object

Render the table. The render_ƒ is ‘render_html’, ‘render_text’, ‘render_json’, etc.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/gloo/objs/data/table.rb', line 190

def render render_ƒ
  begin
    result = self.data
    head = self.headers 
    head = result[0] if head.empty?
    rows = result[1]

    columns = build_columns result[0]

    params = { 
      head: head, 
      cols: result[0],
      columns: columns,
      rows: rows,
      styles: self.styles,
      cell_renderers: self.cell_renderers
    }

    if self.always_rows
      params[ :always_rows ] = true
    end

    helper = Gloo::WebSvr::TableRenderer.new( @engine )
    return helper.data_to_table params
  rescue => e
    @engine.log_exception e
    return nil
  end
end

#stylesObject

Get the styles for the table, if any.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gloo/objs/data/table.rb', line 104

def styles
  style_h = {} 
  o = find_child STYLES
  return style_h unless o
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )

  o.children.each do |c|
    style_h[ c.name ] = c.value
  end

  return style_h
end