Class: Packages::Dependency

Inherits:
ApplicationRecord show all
Defined in:
app/models/packages/dependency.rb

Constant Summary collapse

NAME_VERSION_PATTERN_TUPLE_MATCHING =
'(name, version_pattern) = (?, ?)'.freeze
MAX_STRING_LENGTH =
255.freeze
MAX_CHUNKED_QUERIES_COUNT =
10.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.for_package_names_and_version_patterns(names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200) ⇒ Object


32
33
34
35
36
37
38
# File 'app/models/packages/dependency.rb', line 32

def self.for_package_names_and_version_patterns(names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200)
  ids = ids_for_package_names_and_version_patterns(names_and_version_patterns, chunk_size, max_rows_limit)

  return none if ids.empty?

  id_in(ids)
end

.ids_for_package_names_and_version_patterns(names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200) ⇒ Object

Raises:

  • (ArgumentError)

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/packages/dependency.rb', line 13

def self.ids_for_package_names_and_version_patterns(names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200)
  names_and_version_patterns.reject! { |key, value| key.size > MAX_STRING_LENGTH || value.size > MAX_STRING_LENGTH }
  raise ArgumentError, 'Too many names_and_version_patterns' if names_and_version_patterns.size > MAX_CHUNKED_QUERIES_COUNT * chunk_size

  matched_ids = []
  names_and_version_patterns.each_slice(chunk_size) do |tuples|
    where_statement = Array.new(tuples.size, NAME_VERSION_PATTERN_TUPLE_MATCHING)
                           .join(' OR ')
    ids = where(where_statement, *tuples.flatten)
            .limit(max_rows_limit + 1)
            .pluck(:id)
    matched_ids.concat(ids)

    raise ArgumentError, 'Too many Dependencies selected' if matched_ids.size > max_rows_limit
  end

  matched_ids
end

.pluck_ids_and_namesObject


40
41
42
# File 'app/models/packages/dependency.rb', line 40

def self.pluck_ids_and_names
  pluck(:id, :name)
end

Instance Method Details

#orphaned?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'app/models/packages/dependency.rb', line 44

def orphaned?
  self.dependency_links.empty?
end