Sha256: 843df25de322bc0828b466ab615adb87c3360652998ade74b85cdff9e7d77382

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2019 Eric Crane.  All rights reserved.
#
# An object that contains a collection of other objects.
#
require 'tty-table'
require 'pastel'

module GlooLang
  module Objs
    class Container < GlooLang::Core::Obj

      KEYWORD = 'container'.freeze
      KEYWORD_SHORT = 'can'.freeze

      #
      # The name of the object type.
      #
      def self.typename
        return KEYWORD
      end

      #
      # The short name of the object type.
      #
      def self.short_typename
        return KEYWORD_SHORT
      end

      # ---------------------------------------------------------------------
      #    Messages
      # ---------------------------------------------------------------------

      #
      # Get a list of message names that this object receives.
      #
      def self.messages
        return super + %w[count delete_children show_key_value_table]
      end

      #
      # Count the number of children in the container.
      #
      def msg_count
        i = child_count
        $engine.heap.it.set_to i
        return i
      end

      #
      # Delete all children in the container.
      #
      def msg_delete_children
        self.delete_children
      end

      #
      # Show the given table data.
      #
      def msg_show_key_value_table
        data = self.children.map { |o| [ o.name, o.value ] }
        pastel = ::Pastel.new
        table = TTY::Table.new rows: data
        pad = [ 0, 1, 0, 1 ]
        rendered = table.render( :ascii, indent: 2, padding: pad ) do |r|
          r.border.style = :blue
          r.filter = proc do |val, _row_index, col_index|
            col_index.zero? ? pastel.blue( val ) : pastel.white( val )
          end
        end
        puts "\n  #{rendered}\n\n"
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gloo-lang-0.9.4 lib/gloo_lang/objs/basic/container.rb
gloo-lang-0.9.3 lib/gloo_lang/objs/basic/container.rb