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

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.



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

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

.short_typenameObject

The short name of the object type.



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

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



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

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:



130
131
132
# File 'lib/gloo/objs/data/table.rb', line 130

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.



139
140
141
142
143
# File 'lib/gloo/objs/data/table.rb', line 139

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

#build_columns(result_data) ⇒ Object

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/gloo/objs/data/table.rb', line 208

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.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gloo/objs/data/table.rb', line 108

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.



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

def columns
  o = find_child HEADERS
  return [] unless o

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

#dataObject

Get the list of data elements.



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

def data
  o = find_child DATA
  return [] unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, 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.



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

def headers
  o = find_child HEADERS
  return [] unless o

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

#msg_renderObject



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

def msg_render
  return render
end

#msg_showObject

Show the table in the CLI.



160
161
162
163
# File 'lib/gloo/objs/data/table.rb', line 160

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.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gloo/objs/data/table.rb', line 178

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
    }

    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.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gloo/objs/data/table.rb', line 92

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