Class: Blufin::YmlCommon

Inherits:
Object
  • Object
show all
Defined in:
lib/core/yml/yml_common.rb

Constant Summary collapse

@@enum_scanner =
nil

Class Method Summary collapse

Class Method Details

.colorize_path_and_file(file, site_path) ⇒ Object

Darkens the path and highlights the filename.



188
189
190
191
# File 'lib/core/yml/yml_common.rb', line 188

def self.colorize_path_and_file(file, site_path)
    file_split = split_to_path_file(file, site_path)
    "\x1B[38;5;240m#{file_split[0].gsub(/\/\z/, '')} → \x1B[38;5;140m#{file_split[1]}"
end

.convert_filename_to_name(file) ⇒ Object

Converts /Users/Albert/Repos/repos-blufin//yml/worker/amazon.yml -> amazon.yml



168
169
170
171
# File 'lib/core/yml/yml_common.rb', line 168

def self.convert_filename_to_name(file)
    file_split = file.split('/')
    file_split[file_split.length - 1].gsub(/\.yml\z/, '')
end

.convert_line_array_to_string(array_of_lines) ⇒ Object

Converts an Array of lines to a string (for easier gsub replacement).

Raises:

  • (RuntimeError)


293
294
295
296
297
298
299
300
301
# File 'lib/core/yml/yml_common.rb', line 293

def self.convert_line_array_to_string(array_of_lines)
    raise RuntimeError, "Expected Array of lines, instead got: #{array_of_lines.class}" unless array_of_lines.is_a?(Array)
    string = ''
    array_of_lines.each_with_index do |line, idx|
        newline_or_not = (idx == (array_of_lines.length - 1)) ? '' : "\n"
        string         += "#{line}#{newline_or_not}"
    end
    string
end

.convert_regex_to_string(regex) ⇒ Object

Converts Regex to string, IE: =(?-mix:Az) -> Az

Raises:

  • (RuntimeError)


174
175
176
177
178
# File 'lib/core/yml/yml_common.rb', line 174

def self.convert_regex_to_string(regex)
    raise RuntimeError, "Expected Regex, instead got:#{regex.class}" unless regex.nil? || regex.is_a?(Regexp)
    regex_string = regex.to_s
    regex_string.gsub(/\A\(\?-mix:/, '').gsub(/\)\z/, '')
end

.convert_string_to_line_array(string) ⇒ Object

Converts a string to an Array of lines to a string.

Raises:

  • (RuntimeError)


305
306
307
308
309
310
# File 'lib/core/yml/yml_common.rb', line 305

def self.convert_string_to_line_array(string)
    raise RuntimeError, "Expected String, instead got: #{string.class}" unless string.is_a?(String)
    array_of_lines = []
    string.split("\n").each { |line| array_of_lines << line.gsub("\n", '') }
    array_of_lines
end

.create_boilerplate_files(content_hash, path, site, error_handler, default_nested_paths = %w(data logic))) ⇒ Object

Content Hash is and Hash of Arrays where the inner Array consists of:

Key = Snake Cased String (such as: amazon, ebay, magento, etc). Value = Contents -> for Blufin::YmlCommon::validate_file_and_contents() Value = Contents to Ignore -> for Blufin::YmlCommon::validate_file_and_contents()

Path parameter is where the root of where the directories will be, so if message handlers (for example) live at the following location: ../app-infrastructure/site-name-worker/src/main/java/io/site-name/worker/messages/amazon/AmazonMessageHandler.java

Then path would be: ../app-infrastructure/site-name-worker/src/main/java/io/site-name/worker/messages

Raises:

  • (RuntimeError)


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/core/yml/yml_common.rb', line 325

def self.create_boilerplate_files(content_hash, path, site, error_handler, default_nested_paths = %w(data logic))

    raise RuntimeError, "Expected Hash, instead got: #{content_hash.class}" unless content_hash.is_a?(Hash)
    raise RuntimeError, "Expected String, instead got: #{path.class}" unless path.is_a?(String)
    raise RuntimeError, "Expected YmlErrorHandler (or nil), instead got:#{error_handler.class}" unless error_handler.is_a?(Blufin::YmlErrorHandler) || error_handler.nil?
    raise RuntimeError, "Expected Array, instead got: #{default_nested_paths.class}" unless default_nested_paths.is_a?(Array)

    site      = Blufin::SiteResolver::validate_site(site)
    site_path = Blufin::SiteResolver::get_site_location(site)

    path      = path.gsub(/\/\z/, '')
    path_last = path.split('/')
    path_last = path_last[path_last.length - 1]

    expected_paths = []

    content_hash.each do |key, ca|

        key_camel_cased_lower = Blufin::Strings::snake_case_to_camel_case_lower(key)

        contents        = ca[0]
        contents_ignore = ca[1]
        path_inner      = "#{path}/#{key_camel_cased_lower}"
        file            = "#{path_inner}/#{ca[2]}"

        raise RuntimeError, "Expected String, instead got: #{contents.class}" unless contents.is_a?(String)
        raise RuntimeError, "Expected Array, instead got: #{contents_ignore.class}" unless contents_ignore.is_a?(Array)

        expected_paths << path_inner

        Blufin::YmlCommon::validate_file_and_contents(site, file, Blufin::YmlCommon::convert_string_to_line_array(contents), contents_ignore, error_handler)

        # Check for Rogue files (inner).
        existing_files = []
        Blufin::Files::get_files_in_dir(path_inner).each do |existing_file|
            begin
                existing_file_parts = existing_file.split("#{path_last}/#{key_camel_cased_lower}/")
                existing_file_parts = existing_file_parts[1].split('/')
                next unless existing_file_parts.length == 1
                existing_files << existing_file
            rescue
                next
            end
        end
        existing_files.each do |existing_file|
            unless existing_file == file
                error_handler.add_error(Blufin::YmlErrorHandler::FILE_NOT_EXPECTED, nil, nil, nil, Blufin::YmlCommon::remove_site_path(existing_file, site_path))
            end
        end

        # Check for Rogue directories (inner) -- and create default_nested paths (if not exist).
        existing_directories = Blufin::Files::get_dirs_in_dir(path_inner)
        existing_directories.each do |existing_directory|
            default_nested_paths_found = false
            default_nested_paths.each { |default_nested_path| default_nested_paths_found = true if existing_directory == "#{path_inner}/#{default_nested_path}" }
            error_handler.add_error(Blufin::YmlErrorHandler::DIRECTORY_NOT_EXPECTED, nil, nil, nil, Blufin::YmlCommon::remove_site_path(existing_directory, site_path)) unless default_nested_paths_found
        end

        # Create "default_nested_path" if no exists.
        default_nested_paths.each { |default_nested_path| Blufin::Files::create_directory("#{path_inner}/#{default_nested_path}") unless Blufin::Files::path_exists("#{path_inner}/#{default_nested_path}") }
    end

    # Check for Rogue directories.
    existing_directories = Blufin::Files::get_dirs_in_dir(path)
    existing_directories.each do |existing_directory|
        unless expected_paths.include?(existing_directory)
            error_handler.add_error(Blufin::YmlErrorHandler::DIRECTORY_NOT_EXPECTED, nil, nil, nil, Blufin::YmlCommon::remove_site_path(existing_directory, site_path))
        end
    end

    # Check for Missing directories (in theory this will never be reached as it's created above).
    expected_paths.each do |expected_path|
        unless existing_directories.include?(expected_path)
            error_handler.add_error(Blufin::YmlErrorHandler::DIRECTORY_NOT_FOUND, nil, nil, nil, Blufin::YmlCommon::remove_site_path(expected_path, site_path))
        end
    end

end

.decimal_extract_values(type) ⇒ Object

Extracts 13, 2 from DECIMAL(13,2)



133
134
135
136
137
# File 'lib/core/yml/yml_common.rb', line 133

def self.decimal_extract_values(type)
    decimal_amount = Blufin::Strings::extract_using_regex(type, /\(\d{1,2},\d{1,2}\)\z/, %w{( )})
    decimal_amount = decimal_amount.split(',')
    [decimal_amount[0].to_i, decimal_amount[1].to_i]
end

.description_without_formatting(description) ⇒ Object

Removes formatting (such as asterisks ‘*’) from description strings.



147
148
149
# File 'lib/core/yml/yml_common.rb', line 147

def self.description_without_formatting(description)
    description.gsub(/\s\*/, ' ').gsub(/\*\s/, ' ')
end

.enum_name_extractor(enum_string) ⇒ Object

Returns Array ‘Measure’ from ENUM_CUSTOM(‘Measure’)



66
67
68
69
70
71
72
73
74
# File 'lib/core/yml/yml_common.rb', line 66

def self.enum_name_extractor(enum_string)
    if enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM
        return enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_CUSTOM}\(/, '').gsub(/\)\z/, '').gsub("'", '')
    elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM
        return enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM}\(/, '').gsub(/\)\z/, '').gsub("'", '')
    else
        raise RuntimeError, "'#{enum_string}' doesn't match regex for ENUM, ENUM_CUSTOM or ENUM_SYSTEM."
    end
end

.enum_type_extractor(enum_string) ⇒ Object

Returns TYPE_ENUM, TYPE_ENUM_CUSTOM or TYPE_ENUM_SYSTEM from ENUM(‘a’,‘b’) string.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/core/yml/yml_common.rb', line 25

def self.enum_type_extractor(enum_string)
    if enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM
        Blufin::YmlSchemaValidator::TYPE_ENUM
    elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM
        Blufin::YmlSchemaValidator::TYPE_ENUM_CUSTOM
    elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM
        Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM
    else
        raise RuntimeError, "'enum_string' doesn't match regex --> #{enum_string}"
    end
end

.enum_value_extractor(enum_string, site) ⇒ Object

Returns Array [‘a’,‘b’] from ENUM(‘a’,‘b’)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/core/yml/yml_common.rb', line 39

def self.enum_value_extractor(enum_string, site)
    if enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM
        enum_string = enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM}\(/, '').gsub(/\)\z/, '')
        enum_array  = enum_string.split(',')
        enum_array  = enum_array.map { |value|
            value.gsub!(/\A'/, '').gsub!(/'\z/, '') if value.is_a?(String) && value != ''
        }
        return enum_array
    elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM
        enum_array      = []
        enum_string     = enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_CUSTOM}\(/, '').gsub(/\)\z/, '').gsub(/\A'/, '').gsub(/'\z/, '')
        enum_custom_all = get_enum_scanner(site).get_custom_enums
        enum_array      = enum_custom_all[enum_string] if enum_custom_all.has_key?(enum_string)
        return enum_array
    elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM
        enum_array      = []
        enum_string     = enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM}\(/, '').gsub(/\)\z/, '').gsub(/\A'/, '').gsub(/'\z/, '')
        enum_system_all = get_enum_scanner(site).get_system_enums
        enum_array      = enum_system_all[enum_string] if enum_system_all.has_key?(enum_string)
        return enum_array
    else
        raise RuntimeError, "'enum_string' doesn't match regex --> #{enum_string}"
    end
end

.extract_child_type_from_schema_data(schema_data, schema, table) ⇒ Object

Extracts OBJECT, OBJECT_LIST or nil from @schema_data Hash.



458
459
460
461
# File 'lib/core/yml/yml_common.rb', line 458

def self.extract_child_type_from_schema_data(schema_data, schema, table)
    ct = extract_child_type_from_schema_data_for_java(schema_data, schema, table)
    ct.nil? ? nil : ct.split('.')[1]
end

.extract_child_type_from_schema_data_for_java(schema_data, schema, table) ⇒ Object

Extracts DataType.OBJECT, DataType.OBJECT_LIST or nil from @schema_data Hash.



465
466
467
468
469
470
471
472
# File 'lib/core/yml/yml_common.rb', line 465

def self.extract_child_type_from_schema_data_for_java(schema_data, schema, table)
    schema_data[schema][table].each do |column_name, column_data|
        if column_data.has_key?(Blufin::YmlSchemaValidator::CHILD_TYPE)
            return column_data[Blufin::YmlSchemaValidator::CHILD_TYPE]
        end
    end
    nil
end

.extract_field_name(field) ⇒ Object

Takes something like app.field[] and returns -> field.



127
128
129
# File 'lib/core/yml/yml_common.rb', line 127

def self.extract_field_name(field)
    field.gsub(/\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\./, '').gsub(/(\[\]|\[link\])\z/, '')
end

.extract_flags(flags) ⇒ Object

Abstract method for extracting flags used in Schema definition.

Raises:

  • (RuntimeError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/core/yml/yml_common.rb', line 78

def self.extract_flags(flags)
    raise RuntimeError, "Expected String, instead got:#{flags.class}" unless flags.is_a?(String)
    errors                 = []
    sort_order             = 1
    definition_order       = {}
    flags_object           = Blufin::YmlSchemaFlags.new
    flags_object.flags_raw = flags
    flags.split(' ').each do |flag|
        flag_invalid = false
        case flag
            when Blufin::YmlSchemaValidator::FLAG_PRIMARY_KEY
                flags_object.primary_key            = true
                flags_object.primary_key_sort_order = sort_order
            when Blufin::YmlSchemaValidator::FLAG_INDEX
                flags_object.index            = true
                flags_object.index_sort_order = sort_order
            when Blufin::YmlSchemaValidator::FLAG_NULLABLE
                flags_object.nullable            = true
                flags_object.nullable_sort_order = sort_order
            when Blufin::YmlSchemaValidator::FLAG_UNIQUE
                flags_object.unique            = true
                flags_object.unique_sort_order = sort_order
            else
                if flag =~ /\A#{Blufin::YmlSchemaValidator::FLAG_AUTO_INCREMENT}\z/
                    flags_object.auto_increment            = true
                    flags_object.auto_increment_amount     = nil
                    flags_object.auto_increment_sort_order = sort_order
                elsif flag =~ /\A#{Blufin::YmlSchemaValidator::FLAG_AUTO_INCREMENT}\(\d{1,10}\)\z/
                    auto_increment_amount                  = flag.match(/\(\d{1,10}\)/)
                    auto_increment_amount                  = auto_increment_amount[0].gsub('(', '').gsub(')', '')
                    flags_object.auto_increment            = true
                    flags_object.auto_increment_amount     = auto_increment_amount
                    flags_object.auto_increment_sort_order = sort_order
                else
                    errors << ["Invalid 'flag' value.", flag]
                    flag_invalid = true
                end
        end
        unless flag_invalid
            definition_order[flag] = sort_order
            sort_order             = sort_order + 1
        end
    end
    flags_object.definition_order = definition_order
    [flags_object, errors]
end

.extract_group_artifact_version_from_array(array) ⇒ Object

Extracts the <groupID>, <artifactId> & <version> from -> [“group-id”=>“com“group-id”=>“com.zaxxer”, “artifact-id”=>“HikariCP”, “version”=>“3“version”=>“3.2“version”=>“3.2.0-BLUFIN”]

Raises:

  • (RuntimeError)


444
445
446
447
448
449
450
451
452
453
454
# File 'lib/core/yml/yml_common.rb', line 444

def self.extract_group_artifact_version_from_array(array)
    raise RuntimeError, "Expected Array, instead got: #{array.class}" unless array.is_a?(Array)
    obj = {}
    array.each do |fragment|
        raise RuntimeError, "Expected Hash, instead got: #{fragment.class}" unless fragment.is_a?(Hash)
        obj[Blufin::YmlMavenValidator::DEPENDENCIES_GROUP_ID]    = fragment[Blufin::YmlMavenValidator::DEPENDENCIES_GROUP_ID] if fragment.has_key?(Blufin::YmlMavenValidator::DEPENDENCIES_GROUP_ID)
        obj[Blufin::YmlMavenValidator::DEPENDENCIES_ARTIFACT_ID] = fragment[Blufin::YmlMavenValidator::DEPENDENCIES_ARTIFACT_ID] if fragment.has_key?(Blufin::YmlMavenValidator::DEPENDENCIES_ARTIFACT_ID)
        obj[Blufin::YmlMavenValidator::DEPENDENCIES_VERSION]     = fragment[Blufin::YmlMavenValidator::DEPENDENCIES_VERSION] if fragment.has_key?(Blufin::YmlMavenValidator::DEPENDENCIES_VERSION)
    end
    obj
end

.extract_jersey_path_reference(value) ⇒ Object

Extracts “reference” from “/reference.jpg”



182
183
184
# File 'lib/core/yml/yml_common.rb', line 182

def self.extract_jersey_path_reference(value)
    value.gsub(/\A(.)*\{/, '').gsub(/\}(.)*\z/, '')
end

Determines what a ‘LINK’ table should be named,

Raises:

  • (RuntimeError)


153
154
155
156
157
158
# File 'lib/core/yml/yml_common.rb', line 153

def self.get_link_table_name(source_table, column_name)
    column = column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '')
    raise RuntimeError, "Expected clean column name without brackets, instead got:#{column}" unless column[-1, 1] =~ /[a-z]/i
    target_table = column.split('.')[1]
    "#{source_table}_to_#{target_table}_link"
end

.has_at_least_one_http_method(resource_data, internal_or_oauth) ⇒ Object

Returns TRUE if resource has at least one HTTP method, FALSE otherwise.

Raises:

  • (RuntimeError)


421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/core/yml/yml_common.rb', line 421

def self.has_at_least_one_http_method(resource_data, internal_or_oauth)
    raise RuntimeError, "Expected 'internal' or 'oauth', instead got: #{internal_or_oauth}" unless [Blufin::YmlSchemaValidator::CONFIG_INTERNAL, Blufin::YmlSchemaValidator::CONFIG_OAUTH].include?(internal_or_oauth)
    raise RuntimeError, "Expected Hash, instead got: #{resource_data.class}" unless resource_data.is_a?(Hash)
    methods_key = (internal_or_oauth == Blufin::YmlSchemaValidator::CONFIG_INTERNAL) ? :methods_internal : :methods_oauth
    if resource_data.has_key?(methods_key)
        resource_data[methods_key].keys.each do |http_method|
            if [
                Blufin::YmlConfigValidator::GET,
                Blufin::YmlConfigValidator::POST,
                Blufin::YmlConfigValidator::PATCH,
                Blufin::YmlConfigValidator::DELETE
            ].include?(http_method)
                return true
            else
                raise RuntimeError, "Un-handled HTTP method: #{http_method}"
            end
        end
    end
    false
end

.has_http_method(http_method_name, resource_data, internal_or_oauth) ⇒ Object

Returns TRUE if resource has specified HTTP method, FALSE otherwise.

Raises:

  • (RuntimeError)


406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/core/yml/yml_common.rb', line 406

def self.has_http_method(http_method_name, resource_data, internal_or_oauth)
    raise RuntimeError, "Expected 'http_method_name' to be [#{Blufin::YmlConfigValidator::VALID_METHODS}], instead got: #{http_method_name}" unless Blufin::YmlConfigValidator::VALID_METHODS.include?(http_method_name)
    raise RuntimeError, "Expected 'internal' or 'oauth', instead got: #{internal_or_oauth}" unless [Blufin::YmlSchemaValidator::CONFIG_INTERNAL, Blufin::YmlSchemaValidator::CONFIG_OAUTH].include?(internal_or_oauth)
    raise RuntimeError, "Expected Hash, instead got: #{resource_data.class}" unless resource_data.is_a?(Hash)
    methods_key = (internal_or_oauth == Blufin::YmlSchemaValidator::CONFIG_INTERNAL) ? :methods_internal : :methods_oauth
    if resource_data.has_key?(methods_key)
        resource_data[methods_key].keys.each do |http_method|
            return true if http_method_name.downcase == http_method.downcase
        end
    end
    false
end

.is_empty(value) ⇒ Object

Return TRUE if value is nil, false or empty string.



162
163
164
# File 'lib/core/yml/yml_common.rb', line 162

def self.is_empty(value)
    value.nil? || value == false || value == ''
end

.is_yml_file(file_path) ⇒ Object

Return TRUE if file is .yml – FALSE if not.



9
10
11
# File 'lib/core/yml/yml_common.rb', line 9

def self.is_yml_file(file_path)
    File.extname(file_path).downcase == '.yml'
end

.remove_site_path(path_and_file, site_path) ⇒ Object

Shortens filenames by removing @site_path.

Raises:

  • (RuntimeError)


195
196
197
198
199
200
201
202
# File 'lib/core/yml/yml_common.rb', line 195

def self.remove_site_path(path_and_file, site_path)
    raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
    raise RuntimeError, "Expected String, instead got:#{site_path.class}" unless site_path.is_a?(String)
    sp            = site_path.dup
    sp[0]         = ''
    path_and_file = path_and_file.gsub(/\A\//, '')
    path_and_file.gsub("#{sp}/", '')
end

.split_to_path_file(path_and_file, site_path) ⇒ Object

Splits the path & filename.

Raises:

  • (RuntimeError)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/core/yml/yml_common.rb', line 206

def self.split_to_path_file(path_and_file, site_path)
    raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
    raise RuntimeError, "Expected String, instead got:#{site_path.class}" unless site_path.is_a?(String)
    sp         = site_path.dup
    sp[0]      = ''
    path       = ''
    file       = ''
    file_parts = path_and_file.strip.gsub(/\A\//, '').split('/')
    file_parts.each_with_index do |part, idx|
        if idx < file_parts.length - 1
            path << "#{part}/"
        else
            file << part
        end
    end
    [path.gsub("#{sp}/", '').gsub(/\/\z/, ''), file]
end

.validate_directory_structure(site_path, path, files_expected, files_optional, error_handler) ⇒ Object

Checks that a directory structure (YML directory) matches a certain criteria. Returns FALSE if fails, TRUE if success

Raises:

  • (RuntimeError)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/core/yml/yml_common.rb', line 227

def self.validate_directory_structure(site_path, path, files_expected, files_optional, error_handler)
    raise RuntimeError, "Expected String, instead got:#{path.class}" unless path.is_a?(String)
    raise RuntimeError, "Expected Array, instead got:#{files_expected.class}" unless files_expected.is_a?(Array)
    raise RuntimeError, "Expected Array, instead got:#{files_optional.class}" unless files_optional.is_a?(Array)
    raise RuntimeError, "Expected YmlErrorHandler, instead got:#{error_handler.class}" unless error_handler.is_a?(Blufin::YmlErrorHandler)
    path           = "#{site_path}/#{path}"
    current_errors = error_handler.get_error_count
    files_expected = files_expected.map { |value| "#{path}/#{value}" }
    files_optional = files_optional.map { |value| "#{path}/#{value}" }
    files_present  = []
    Blufin::Files::get_files_in_dir(path).each do |file|
        error_handler.add_error(Blufin::YmlErrorHandler::FILE_NOT_EXPECTED, file, nil, nil, remove_site_path(file, site_path)) unless files_expected.include?(file) || files_optional.include?(file)
        files_present << file
    end
    files_expected.each do |file|
        error_handler.add_error(Blufin::YmlErrorHandler::FILE_NOT_FOUND, file, nil, nil, remove_site_path(file, site_path)) unless files_present.include?(file)
    end
    current_errors == error_handler.get_error_count
end

.validate_file_and_contents(site, file, contents = [], contents_ignore = [], error_handler = nil, add_to_git = true) ⇒ Object

Checks that a file exists (and if not, creates it) and validates that it matches a basic template.

Raises:

  • (RuntimeError)


249
250
251
252
253
254
255
256
257
258
259
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
# File 'lib/core/yml/yml_common.rb', line 249

def self.validate_file_and_contents(site, file, contents = [], contents_ignore = [], error_handler = nil, add_to_git = true)
    raise RuntimeError, "Expected String, instead got:#{file.class}" unless file.is_a?(String)
    raise RuntimeError, "Expected Array, instead got:#{contents.class}" unless contents.is_a?(Array)
    raise RuntimeError, "Expected Array, instead got:#{contents_ignore.class}" unless contents_ignore.is_a?(Array)
    raise RuntimeError, "Expected YmlErrorHandler (or nil), instead got:#{error_handler.class}" unless error_handler.is_a?(Blufin::YmlErrorHandler) || error_handler.nil?
    site      = Blufin::SiteResolver::validate_site(site)
    site_path = Blufin::SiteResolver::get_site_location(site)
    site_name = Blufin::SiteResolver::get_site_name(site)
    if Blufin::Files::file_exists(file)
        lines_expected = []
        contents.each do |line|
            unless contents_ignore.include?(line)
                unless line =~ /\Aimport\s/
                    lines_expected << line unless line.strip == ''
                end
            end
        end
        contents = Blufin::Files::read_file(file)
        contents.each do |line|
            line = line.gsub("\n", '')
            lines_expected.delete(line) if lines_expected.include?(line)
        end
        if lines_expected.length > 0 && !error_handler.nil?
            lines_expected.map! { |line| "\x1B[38;5;154m#{line}" }
            file         = Blufin::YmlCommon::remove_site_path(file, site_path)
            file         = file.gsub(/(#{Blufin::Site::REGEX_APP_DIRS})\/#{site_name}-(#{Blufin::SiteServices::REGEX_JAVA})\/src\/main\/java\//, '')
            error_output = ["File: \x1B[38;5;196m#{file}\x1B[0m", 'Line(s) expected but not found:']
            error_output = error_output + lines_expected + [nil]
            error_handler.add_error(Blufin::YmlErrorHandler::FILE_CONTENT_MISMATCH, nil, nil, nil, error_output)
        end
    else
        if file =~ /\.java$/
            Blufin::Files::write_file_java(file, contents, Blufin::Files::JAVA_AUTO_GENERATED_ONCE)
        else
            Blufin::Files::write_file(file, contents)
        end
        Blufin::Terminal::command("git add #{file}", site_path, false, false) if add_to_git
        Blufin::Terminal::output(colorize_path_and_file(file, site_path), Blufin::Terminal::MSG_GENERATED)
    end

end

.validate_keys(key_set, valid_keys_as_array) ⇒ Object

Returns nil on success, otherwise an array of invalid keys.



15
16
17
18
19
20
21
# File 'lib/core/yml/yml_common.rb', line 15

def self.validate_keys(key_set, valid_keys_as_array)
    invalid_keys = []
    key_set.each do |key|
        invalid_keys << key unless valid_keys_as_array.include?(key)
    end
    return invalid_keys if invalid_keys.any?
end

.varchar_extract_max_length(type) ⇒ Object

Extracts 45 from VARCHAR(45)



141
142
143
# File 'lib/core/yml/yml_common.rb', line 141

def self.varchar_extract_max_length(type)
    Blufin::Strings::extract_using_regex(type, /\(\d{1,3}\)\z/, %w{( )})
end