Class: Upkeep::Dependencies::ActiveRecordCollection

Inherits:
Base
  • Object
show all
Defined in:
lib/upkeep/dependencies.rb

Direct Known Subclasses

ActiveRecordQuery

Constant Summary collapse

UNKNOWN =
Object.new

Instance Attribute Summary

Attributes inherited from Base

#key, #metadata, #source

Instance Method Summary collapse

Methods inherited from Base

#cache_key, #identity?, #identity_key, #narrow_frame_safe?, #to_h, #visibility

Constructor Details

#initialize(primary_table:, table_columns:, coverage:, sql:, predicates: [], source: :active_record_collection, precision: :collection_predicate) ⇒ ActiveRecordCollection

Returns a new instance of ActiveRecordCollection.



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
# File 'lib/upkeep/dependencies.rb', line 83

def initialize(
  primary_table:,
  table_columns:,
  coverage:,
  sql:,
  predicates: [],
  source: :active_record_collection,
  precision: :collection_predicate
)
  table_columns = normalize_table_columns(table_columns)
  coverage = coverage.to_sym
  unless coverage == :columns
    raise ArgumentError,
      "unsupported Active Record predicate coverage: #{coverage}; collection dependencies require proven column coverage"
  end

  @precision = precision.to_sym
  super(
    source: source.to_sym,
    key: {
      table: primary_table,
      predicate_digest: Digest::SHA256.hexdigest(sql)[0, 16]
    },
    metadata: {
      primary_table: primary_table,
      table_columns: table_columns,
      coverage: coverage.to_s,
      sql: sql,
      predicates: normalize_predicates(predicates)
    }
  )
end

Instance Method Details

#collection_lookup_columnsObject



136
137
138
139
140
# File 'lib/upkeep/dependencies.rb', line 136

def collection_lookup_columns
  table_columns.flat_map do |table, columns|
    columns.map { |column| [table, column] }
  end.sort
end

#collection_lookup_tablesObject



132
133
134
# File 'lib/upkeep/dependencies.rb', line 132

def collection_lookup_tables
  table_columns.keys.sort
end

#matches_change?(change) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/upkeep/dependencies.rb', line 116

def matches_change?(change)
  return false unless table_columns.key?(change.fetch(:table))

  predicate_match = predicate_match(change)
  return predicate_match unless predicate_match == UNKNOWN

  return true if create_change?(change)
  return true if delete_change?(change)

  table_columns.fetch(change.fetch(:table)).intersect?(change.fetch(:changed_attributes, []))
end

#precisionObject



128
129
130
# File 'lib/upkeep/dependencies.rb', line 128

def precision
  @precision
end