Class: Tapioca::Dsl::Compilers::StoreModel

Inherits:
Tapioca::Dsl::Compiler
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/store_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tapioca/dsl/compilers/store_model.rb', line 9

def self.gather_constants
  return [] unless defined?(::StoreModel)

  ::ActiveRecord::Base.descendants.select do |klass|
    next false unless klass.respond_to?(:attribute_types)

    # Skip classes that can't load their schema or attributes
    begin
      attribute_types = klass.attribute_types
    rescue ActiveRecord::TableNotSpecified, StandardError
      next false
    end

    # Check if any attribute types are StoreModel types
    attribute_types.values.any? { |type| store_model_type?(type) }
  end
end

.store_model_class_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/tapioca/dsl/compilers/store_model.rb', line 45

def self.store_model_class_type?(type)
  return false unless type.respond_to?(:model_klass)

  model_klass = type.model_klass
  return false if model_klass.nil?

  model_klass.include?(::StoreModel::Model)
end

.store_model_one_of_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/tapioca/dsl/compilers/store_model.rb', line 55

def self.store_model_one_of_type?(type)
  # Check for StoreModel.one_of types (polymorphic types)
  return true if type.is_a?(::StoreModel::Types::OnePolymorphic)
  return true if type.is_a?(::StoreModel::Types::ManyPolymorphic)

  false
end

.store_model_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/tapioca/dsl/compilers/store_model.rb', line 35

def self.store_model_type?(type)
  return true if type.is_a?(::StoreModel::Types::One)
  return true if type.is_a?(::StoreModel::Types::Many)
  return true if store_model_class_type?(type)
  return true if store_model_one_of_type?(type)

  false
end

Instance Method Details

#decorateObject



28
29
30
31
32
# File 'lib/tapioca/dsl/compilers/store_model.rb', line 28

def decorate
  root.create_path(constant) do |klass|
    create_store_model_methods(klass)
  end
end