Class: Telm::Core::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/telm/core/schema.rb

Overview

The introspected registry: every browsable model, keyed by table name. Built once and memoized on Telm.schema; Telm.reset_schema! clears it (wired to to_prepare for dev reloading).

Constant Summary collapse

INTERNAL_MODELS =
%w[ActiveRecord::InternalMetadata ActiveRecord::SchemaMigration].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources) ⇒ Schema

Returns a new instance of Schema.



106
107
108
109
110
# File 'lib/telm/core/schema.rb', line 106

def initialize(resources)
  @resources = resources.sort_by(&:label)
  @by_table = {}
  @resources.each { |resource| @by_table[resource.table_name] ||= resource }
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



104
105
106
# File 'lib/telm/core/schema.rb', line 104

def resources
  @resources
end

Class Method Details

.build(config: Telm.config) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/telm/core/schema.rb', line 14

def build(config: Telm.config)
  eager_load!
  excluded = config.excluded_models.map(&:to_s)

  resources = ActiveRecord::Base.descendants.filter_map do |model|
    resource_for(model, excluded)
  end

  new(resources)
end

Instance Method Details

#find(table_name) ⇒ Object



112
113
114
# File 'lib/telm/core/schema.rb', line 112

def find(table_name)
  @by_table[table_name.to_s]
end

#include?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/telm/core/schema.rb', line 116

def include?(table_name)
  @by_table.key?(table_name.to_s)
end