class Blufin::YmlOutputter

Constants

AUTO
CUSTOM
SYSTEM

Public Class Methods

new(site) click to toggle source
# File lib/core/yml/yml_outputter.rb, line 11
def initialize(site)

    @site = Blufin::SiteResolver::validate_site(site)

    @auto_increment = []
    @auto_increment_indexed = []

    @resources = []

    @schema_fks_links = {}

    @yml_enum_scanner = Blufin::ScannerJavaEnums.new(@site)

end

Public Instance Methods

add_auto_increment_indexed_table(schema, table, amount) click to toggle source
# File lib/core/yml/yml_outputter.rb, line 206
def add_auto_increment_indexed_table(schema, table, amount)
    @auto_increment_indexed << "(#{amount}) \xe2\x86\x92 #{schema}.#{table}"
end
add_auto_increment_table(schema, table) click to toggle source
# File lib/core/yml/yml_outputter.rb, line 202
def add_auto_increment_table(schema, table)
    @auto_increment << "#{schema}.#{table}"
end
add_resource(key, data) click to toggle source
# File lib/core/yml/yml_outputter.rb, line 210
def add_resource(key, data)
    @resources << {key => data}
end
show_auto_increment() click to toggle source

Renders the AUTO_INCREMENT table. @return void

# File lib/core/yml/yml_outputter.rb, line 28
def show_auto_increment

    cnt = 0
    max = [@auto_increment.length, @auto_increment_indexed.length].max.to_i

    Blufin::Terminal::custom('SUMMARY', 23, Blufin::YmlSchemaValidator::FLAG_AUTO_INCREMENT, "Below you can see which tables have what #{Blufin::Terminal::format_highlight(Blufin::YmlSchemaValidator::FLAG_AUTO_INCREMENT)} values applied")

    table(:border => false) do

        row do
            column('', :width => 4)
            column('AUTO_INCREMENT', :width => 60, :color => 34)
            column('AUTO_INCREMENT(?)', :width => 60, :color => 34)
        end

        while cnt + 1 < max
            row do
                column('')
                column(@auto_increment[cnt], :color => 240)
                column(@auto_increment_indexed[cnt], :color => 240)
            end
            cnt = cnt + 1
        end

    end

    puts "\x1B[0m"
end
show_enums() click to toggle source

Renders a summary of all enums that are available (including their possible values). @return void

# File lib/core/yml/yml_outputter.rb, line 59
def show_enums

    terminal_width = Blufin::Terminal::get_terminal_width

    Blufin::Terminal::error('Cannot display data because terminal window is too narrow.', ["Current width is: #{Blufin::Terminal::format_highlight(terminal_width)}", "Minimum width is: #{Blufin::Terminal::format_highlight('90')}"], true) if terminal_width < 90 && all
    Blufin::Terminal::custom('SUMMARY', 58, 'ENUMS', "Below is a list of all available #{Blufin::Terminal::format_highlight('ENUMS')} for \x1B[38;5;106m#{Blufin::SiteResolver::get_site_title(@site)}\x1B[0m.")

    terminal_width = Blufin::Terminal::get_terminal_width

    all_enums = []

    auto_enums = @yml_enum_scanner.get_auto_enums
    custom_enums = @yml_enum_scanner.get_custom_enums
    system_enums = @yml_enum_scanner.get_system_enums

    auto_enums.keys.each { |enum| all_enums << enum }
    custom_enums.keys.each { |enum| all_enums << enum }
    system_enums.keys.each { |enum| all_enums << enum }

    max_enum_width = all_enums.max_by(&:length).length
    value_column_max_width = terminal_width - 30 - max_enum_width

    enum_column_width = ((max_enum_width + 5) < 10) ? 10 : (max_enum_width + 5)

    table(:border => false) do

        row do
            column('', :width => 17, :align => 'right')
            column('', :width => 1, :color => 255)
            column('', :width => enum_column_width, :color => 240, :align => 'left')
            column('', :width => value_column_max_width)
        end

        show_enums_common_table(custom_enums, value_column_max_width, AUTO)
        show_enums_common_table(auto_enums, value_column_max_width, CUSTOM)
        show_enums_common_table(system_enums, value_column_max_width, SYSTEM)

    end


end
show_resources(as_json = false) click to toggle source

Renders a summary of all resource end-points exposed by the API. @return void

# File lib/core/yml/yml_outputter.rb, line 103
def show_resources(as_json = false)

    if @resources.any?

        if as_json

            puts @resources.inspect

        else

            width_schema = []
            width_group = []
            width_resource = []
            width_type = []

            # Get column widths (to make as narrow as possible).
            @resources.each do |data|
                data.each do |key, resource|
                    width_schema << resource[:schema].length
                    width_group << resource[:resource].split('/')[0].length
                    width_resource << resource[:resource].length
                    width_type << resource[:type].length
                end
            end

            Blufin::Terminal::custom('SUMMARY', 161, 'RESOURCES', "Below is a summary of the final #{Blufin::Terminal::format_highlight('RESOURCE STRUCTURE')}")

            table(:border => false) do

                current_group = nil

                @resources.each do |data|

                    data.each do |key, resource|

                        group = resource[:resource].split('/')[0]
                        extra = ''
                        extra_color = 240

                        case resource[:type]
                            when Blufin::YmlSchemaValidator::RESOURCE_TYPE_PARENT
                                type_text = "\xe2\x80\x94"
                                type_color = 240
                            when Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT
                                type_text = Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT
                                type_color = 147
                            when Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST
                                type_text = Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST
                                type_color = 197
                            when Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LINK
                                type_text = Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LINK
                                type_color = 118
                            else
                                raise RuntimeError, "Type not found in \xe2\x86\x92 show_resources()."
                        end

                        # If this is a LINKED table, show list of tables where it's referenced from.
                        if resource[:type] == Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LINK
                            unless @schema_fks_links[key].nil?
                                extra = []
                                @schema_fks_links[key].each do |table|
                                    extra << table.split('.')[1]
                                end
                                extra = "\xe2\x86\x92 #{extra.join(', ')}"
                                extra_color = 220
                            end
                        end

                        row do
                            column((group != current_group) ? "#{resource[:schema]} \xe2\x86\x92" : '', :color => 250, :width => (width_schema.max.to_i) + 4, :align => 'right')
                            column((group != current_group) ? group : '', :color => 154, :width => (width_group.max.to_i) + 2)
                            column(resource[:resource], :color => 240, :width => (width_resource.max.to_i) + 2)
                            column("#{type_text}", :color => type_color, :width => width_type.max.to_i, :align => 'right')
                            column(extra, :color => extra_color, :width => 100)
                        end

                        current_group = group
                        current_schema = resource[:schema]

                    end

                end

                row do
                    column('')
                    column('')
                    column('')
                    column('')
                    column('')
                end

            end

        end

    end

end

Private Instance Methods

show_enums_common_table(enum_hash, value_column_width, type) click to toggle source
# File lib/core/yml/yml_outputter.rb, line 220
def show_enums_common_table(enum_hash, value_column_width, type)

    case type
        when AUTO
            text_type = 'ENUM_AUTO'
            color_one = 97
            color_two = 183
        when CUSTOM
            text_type = 'ENUM_CUSTOM'
            color_one = 34
            color_two = 118
        when SYSTEM
            text_type = 'ENUM_SYSTEM'
            color_one = 31
            color_two = 153
        else
            raise RuntimeError, "Unrecognized enum type: #{type}"
    end


    enum_hash.each do |enum, values|

        vals = values.dup
        vals_all = []
        row_output = []
        vals_per_row = vals.length.to_f

        vals.each { |val| vals_all << val }

        next unless vals_all.any?

        # The length of the longest ENUM value (plus 2) -- rounded up to nearest 5.
        vals_max = vals_all.max_by(&:length).length + 2
        vals_max = vals_max + (5 - (vals_max % 5)) if (vals_max % 5) > 0

        # The maximum number of values we can have in a row before we run out of Terminal.
        cols_max = (value_column_width / vals_max).to_f

        while vals_per_row > cols_max do
            vals_per_row = vals_per_row / 2
        end

        number_of_rows = (vals.length / vals_per_row).to_i

        raise RuntimeError, '(number_of_rows * vals_per_row) should not be less than vals.length' if (number_of_rows * vals_per_row) < vals.length

        number_of_rows.times do
            output = ''
            vals_per_row.ceil.times do
                output << vals.shift.to_s.ljust(vals_max, ' ')
            end
            row_output << output if output.strip.length > 0
        end

        row do
            column(text_type, :color => color_one)
            column('→')
            column("#{enum} ", :color => color_two)
            column(row_output[0], :color => 244)
        end

        row_output.shift

        if row_output.length > 1
            row_output.each do |val|
                row do
                    column('')
                    column('')
                    column('')
                    column(val, :color => 244)
                end
            end
        end

        row do
            column('')
            column('')
            column('')
            column('')
        end

    end

end