class Blufin::YmlVueServiceWriter

Constants

PACKAGE
SERVICE

Public Class Methods

new(site, schema_resources) click to toggle source

Initialize the class. @return void

# File lib/core/yml_writers/yml_vue_service_writer.rb, line 10
def initialize(site, schema_resources)

    @schema_resources = schema_resources

    raise RuntimeError, 'Could not find valid @schema_resources.' if @schema_resources.nil? || !@schema_resources.is_a?(Hash)

    @site          = Blufin::SiteResolver::validate_site(site)
    @site_name     = Blufin::SiteResolver::get_site_name(@site)
    @site_domain   = Blufin::SiteResolver::get_site_domain(@site)
    @site_location = "#{Blufin::SiteResolver::get_site_location(@site)}/"

    # Wipe out all previous files.
    # Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE.each do |schema|
    #     path_to_wipe_out = "#{get_java_path(@site, schema, SERVICE, PACKAGE)}"
    #     if Blufin::Files::path_exists(path_to_wipe_out)
    #         if Blufin::Files::get_files_in_dir(path_to_wipe_out).any?
    #             Blufin::Terminal::command('rm *', path_to_wipe_out, false, false)
    #         end
    #     end
    # end

end

Public Instance Methods

write() click to toggle source

@return void

# File lib/core/yml_writers/yml_vue_service_writer.rb, line 34
def write

    @schema_resources.each do |schema_table, resource_data|

        # Only write PARENT resources as they will contain the others as 'dependents'.
        next unless resource_data[:tree].length < 2

        # Skip if this resource has no HTTP Methods.
        next unless Blufin::YmlCommon::has_at_least_one_http_method(resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)

        @schema = schema_table.split('.')[0]
        @table  = schema_table.split('.')[1]
        @type   = resource_data[:type]

        @import_statements = []

        table_name       = Blufin::Strings::snake_case_to_camel_case(@table)
        table_name_lower = Blufin::Strings::snake_case_to_camel_case_lower(@table)
        class_name       = "#{table_name}Resource"

        auth_level = Blufin::SiteAuth::get_auth_level_for_table(@schema, @table)

        # TODO - THIS CANNOT BE HARD-CODED TO FALSE/TRUE.
        is_public  = false
        is_secured = true

        is_account_aware = (auth_level == Blufin::SiteAuth::LEVEL_ACCOUNT || auth_level == Blufin::SiteAuth::LEVEL_ACCOUNT_USER)
        is_user_aware    = (auth_level == Blufin::SiteAuth::LEVEL_ACCOUNT_USER)

        # TODO - THIS CANNOT BE HARD-CODED TO FALSE.
        is_oauth = false

        # TODO - REMOVE (AFTER OAUTH IS FIXED -- HANDY DEBUG STRING)
        # puts "\x1B[38;5;154m#{@schema}.#{@table}\x1B[0m \xe2\x86\x92 \x1B[38;5;196m#{auth_level}\x1B[0m"

        content_inner = []
        content_inner << "import Axios from 'axios';"
        content_inner << ''
        content_inner << 'export default {'

        if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::GET)
            content_inner << "    get#{table_name}() {"
            content_inner << "        return Axios.get(`/#{@schema_resources["#{schema_table}"][:resource]}`).then((response) => {"
            content_inner << '            return response.data;'
            content_inner << '        }, (e) => {'
            content_inner << '            return Promise.reject(e.data);'
            content_inner << '        });'
            content_inner << '    },'
            content_inner << "    get#{table_name}(id) {"
            content_inner << "        return Axios.get(`/#{@schema_resources["#{schema_table}"][:resource]}/${id}`).then((response) => {"
            content_inner << '            return response.data;'
            content_inner << '        }, (e) => {'
            content_inner << '            return Promise.reject(e.data);'
            content_inner << '        });'
            content_inner << '    },'
        end

        if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::POST)
            content_inner << "    post#{table_name}(obj) {"
            content_inner << "        return Axios.post(`/#{@schema_resources["#{schema_table}"][:resource]}`, obj).then((response) => {"
            content_inner << '            return response.data;'
            content_inner << '        }, (e) => {'
            content_inner << '            return Promise.reject(e.data);'
            content_inner << '        });'
            content_inner << '    },'
        end

        if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::PATCH)
            content_inner << "    patch#{table_name}(obj) {"
            content_inner << "        return Axios.patch(`/#{@schema_resources["#{schema_table}"][:resource]}`, obj).then((response) => {"
            content_inner << '            return response.data;'
            content_inner << '        }, (e) => {'
            content_inner << '            return Promise.reject(e.data);'
            content_inner << '        });'
            content_inner << '    },'
        end

        if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::DELETE)
            content_inner << "    delete#{table_name}(id) {"
            content_inner << "        return Axios.delete(`/#{@schema_resources["#{schema_table}"][:resource]}/${id}`).then((response) => {"
            content_inner << '            return response.data;'
            content_inner << '        }, (e) => {'
            content_inner << '            return Promise.reject(e.data);'
            content_inner << '        });'
            content_inner << '    },'
        end

        if !resource_data[:dependents].nil? && resource_data[:dependents].any?
            resource_data[:dependents].each do |dependent|

                dcc  = Blufin::Strings::snake_case_to_camel_case(dependent)
                dccl = Blufin::Strings::snake_case_to_camel_case_lower(dependent)
                uri  = @schema_resources["#{@schema}.#{dependent}"][:resource]

                resource_data = @schema_resources["#{@schema}.#{dependent}"]

                if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::GET)
                    # TODO
                end

                if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::POST)
                    # TODO
                end

                if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::PATCH)
                    # TODO
                end

                if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::DELETE)
                    # TODO
                end

            end

        end

        content = []
        content = content.push(*content_inner)
        content[content.length - 1] = '    }'
        content << '}'

        # TODO - REMOVE
        # puts "\x1B[38;5;154m#{content.to_yaml}\x1B[0m" if schema_table == 'common.account'

        # TODO
        # full_file_path = "#{get_java_path(@site, @schema, SERVICE, PACKAGE)}/#{class_name}.java"

        # TODO
        # write_file_java(full_file_path, content).gsub(@site_location, '')

    end

end