Class: RailsLens::ViewMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_lens/view_metadata.rb

Overview

Extracts and manages metadata for database views and materialized views

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ ViewMetadata

Returns a new instance of ViewMetadata.



8
9
10
11
12
13
14
# File 'lib/rails_lens/view_metadata.rb', line 8

def initialize(model_class)
  @model_class = model_class
  @connection = model_class.connection
  @table_name = model_class.table_name
  @adapter_name = connection.adapter_name.downcase
  @adapter = create_adapter
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/rails_lens/view_metadata.rb', line 6

def connection
  @connection
end

#model_classObject (readonly)

Returns the value of attribute model_class.



6
7
8
# File 'lib/rails_lens/view_metadata.rb', line 6

def model_class
  @model_class
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



6
7
8
# File 'lib/rails_lens/view_metadata.rb', line 6

def table_name
  @table_name
end

Instance Method Details

#dependenciesObject



42
43
44
45
46
47
48
# File 'lib/rails_lens/view_metadata.rb', line 42

def dependencies
  return [] unless view_exists?

  @adapter&.view_dependencies || []
rescue ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotDefined
  []
end

#last_refreshedObject



56
57
58
59
60
61
62
# File 'lib/rails_lens/view_metadata.rb', line 56

def last_refreshed
  return nil unless materialized_view?

  @adapter&.view_last_refreshed
rescue ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotDefined
  nil
end

#materialized_view?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rails_lens/view_metadata.rb', line 26

def materialized_view?
  view_type == 'materialized'
end

#refresh_strategyObject



50
51
52
53
54
# File 'lib/rails_lens/view_metadata.rb', line 50

def refresh_strategy
  return nil unless materialized_view?

  @adapter&.view_refresh_strategy
end

#regular_view?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rails_lens/view_metadata.rb', line 30

def regular_view?
  view_type == 'regular'
end

#to_hObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/rails_lens/view_metadata.rb', line 72

def to_h
  {
    view_type: view_type,
    updatable: updatable?,
    dependencies: dependencies,
    refresh_strategy: refresh_strategy,
    last_refreshed: last_refreshed,
    view_definition: view_definition
  }.compact
end

#updatable?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/rails_lens/view_metadata.rb', line 34

def updatable?
  return false unless view_exists?

  @adapter&.view_updatable? || false
rescue ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotDefined
  false
end

#view_definitionObject



64
65
66
67
68
69
70
# File 'lib/rails_lens/view_metadata.rb', line 64

def view_definition
  return nil unless view_exists?

  @adapter&.view_definition
rescue ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotDefined
  nil
end

#view_exists?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rails_lens/view_metadata.rb', line 22

def view_exists?
  ModelDetector.view_exists?(model_class)
end

#view_typeObject



16
17
18
19
20
# File 'lib/rails_lens/view_metadata.rb', line 16

def view_type
  return nil unless view_exists?

  @adapter&.view_type
end