Class: Gloo::Objs::Outline

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/str_utils/outline.rb

Constant Summary collapse

KEYWORD =
'outline'.freeze
KEYWORD_SHORT =
'outline'.freeze
OBJ_SOURCE =
'object_source'.freeze
DATA =
'data'.freeze
SEPARATOR =
'separator_char'.freeze
DEFAULT_SEPARATOR =
'/'.freeze
ENTITY_PATH =
'entity_path'.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_responds_to?, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #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

.doc_dataObject

Get the object's documentation data.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/gloo/objs/str_utils/outline.rb', line 260

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'Convert strings with a path to a hierarchical outline.',
    :children => [
      'object_source (alias) — Points to the data source. Should be a container with objects, each of which has a name and optionally an id.',
      'entity_path (string) — The path to the entity, used to generate links to entities that have IDs.',
      "separator_char (string) — Optional. If not specified, the '/' character is used. The character used to separate the path elements.",
      'data (string) — The generated outline: a hierarchical outline of OL and LI elements. For children with IDs, links to entities are included.'
    ],
    :messages => [
      'generate — Generate the outline from the list of strings.'
    ],
    :examples => <<~EXAMPLES.strip
      #
      # Convert flat elements to hierarchal outline.
      #

      outline [container] :

        on_load [script] :
          tell outline.util to generate
          show outline.util.data

        src_data [container] :
          1 [container] :
            id [int] : 1
            name [string] : a/b/c
          2 [container] :
            id [int] : 2
            name [string] : a/b/d
          3 [container] :
            id [int] : 3
            name [string] : a/b/e
          4 [container] :
            id [int] : 4
            name [string] : x
          5 [container] :
            id [int] : 5
            name [string] : x/y
          6 [container] :
            id [int] : 6
            name [string] : x/y/z
          7 [container] :
            id [int] : 7
            name [string] : last

        util [outline] :
          object_source [alias] : outline.src_data
          entity_path [string] : /entity/
          separator_char [string] : /
          data [string] :
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



121
122
123
# File 'lib/gloo/objs/str_utils/outline.rb', line 121

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

.short_typenameObject

The short name of the object type.



30
31
32
# File 'lib/gloo/objs/str_utils/outline.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/str_utils/outline.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:



96
97
98
# File 'lib/gloo/objs/str_utils/outline.rb', line 96

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.



105
106
107
108
109
110
111
# File 'lib/gloo/objs/str_utils/outline.rb', line 105

def add_default_children
  fac = @engine.factory
  fac.create_alias OBJ_SOURCE, nil, self
  fac.create_string ENTITY_PATH, nil, self
  fac.create_string SEPARATOR, '/', self
  fac.create_string DATA, nil, self
end

#add_item(in_container, name, id) ⇒ Object

Add the item to the container. Used to create the leaf of the tree. It might be a branch later on.



178
179
180
181
# File 'lib/gloo/objs/str_utils/outline.rb', line 178

def add_item( in_container, name, id )
  item = { name: name, children: [], id: id }
  in_container[ :children ] << item
end

#add_topic(id, name) ⇒ Object

Add the item to the topics array, but break multi-segment topics into component segments.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/gloo/objs/str_utils/outline.rb', line 158

def add_topic id, name
  segments = name.split( separator_char )
  path_segments = segments[0..-2]
  last_segment = segments.last
  parent = @root

  if segments.count > 1
    path_segments.each do |segment|
      parent = find_segment( parent, segment )
    end
  end

  add_item( parent, last_segment, id )
end

#dataObject

Get the data value of the object. This might be encrypted or decrypted based on what action was last taken.



58
59
60
61
62
# File 'lib/gloo/objs/str_utils/outline.rb', line 58

def data
  o = find_child DATA
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#entity_pathObject

Get the entity path.



67
68
69
70
71
# File 'lib/gloo/objs/str_utils/outline.rb', line 67

def entity_path
  o = find_child ENTITY_PATH
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#find_segment(in_container, name) ⇒ Object

Find the segment. If not found, create it.



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/gloo/objs/str_utils/outline.rb', line 187

def find_segment( in_container, name )
  in_container[ :children ].each do |item|
    if item[ :name ] == name
      return item
    end
  end

  # Container not found, create it.
  item = { name: name, id: nil, children: [] }
  in_container[ :children ] << item
  return item
end

#msg_generateObject

Generate an outline from the source objects.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gloo/objs/str_utils/outline.rb', line 128

def msg_generate
  @root = { name: 'root', id: nil, children: [] }
  
  # Get Topics from the source container
  src = obj_source
  return unless src
  
  # Build the outline structure
  src.children.each do |o|
    id = o.children[0].value
    name = o.children[1].value
    add_topic id, name
  end

  # Generate the HTML from the outline structure
  html = render_outline
  
  # Update the data value
  update_data( html )
end

#obj_sourceObject

Get the source value of the object. Returns nil if there is none.



38
39
40
41
42
# File 'lib/gloo/objs/str_utils/outline.rb', line 38

def obj_source
  o = find_child OBJ_SOURCE
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#render_outlineObject

Render Outline



208
209
210
211
212
213
214
# File 'lib/gloo/objs/str_utils/outline.rb', line 208

def render_outline
  @html = ""
  @path = entity_path
  render_outline_r( @root[ :children ] )

  return @html
end

#render_outline_r(in_container) ⇒ Object

Render the outline recursively.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gloo/objs/str_utils/outline.rb', line 219

def render_outline_r( in_container )
  @html << "<ul>\n"
  in_container.each do |item|
    if item[ :id ] && @path
      url = "#{@path}#{item[ :id ]}"
      @html << "<li><a href='#{url}'>#{item[ :name ]}</a></li>\n"
    else
      @html << "<li>#{item[ :name ]}</li>\n"
    end
    if item[ :children ] && ( item[ :children ].count > 0 )
      render_outline_r( item[ :children ] )
    end
  end
  @html << "</ul>\n"
end

#separator_charObject

Get the separator character.



47
48
49
50
51
# File 'lib/gloo/objs/str_utils/outline.rb', line 47

def separator_char
  o = find_child SEPARATOR
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value || DEFAULT_SEPARATOR
end

#show_items_r(in_container, indent) ⇒ Object

Debugging Tool shows topic hierarchy in console. This is a recursive function.



244
245
246
247
248
249
250
251
# File 'lib/gloo/objs/str_utils/outline.rb', line 244

def show_items_r( in_container, indent )
  in_container.each do |item|
    puts "#{' ' * indent}#{item[ :name ]} (#{item[ :id ]})"
    if item[ :children ] && ( item[ :children ].count > 0 )
      show_items_r( item[ :children ], indent + 3 )
    end
  end
end

#update_data(new_val) ⇒ Object

Update the data value of the object.



76
77
78
79
80
81
82
83
84
# File 'lib/gloo/objs/str_utils/outline.rb', line 76

def update_data( new_val )
  o = find_child DATA
  return unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return unless o

  o.set_value new_val
end