Module: Toy::Dynamo::Tasks

Extended by:
Tasks
Included in:
Tasks
Defined in:
lib/toy/dynamo/tasks.rb

Instance Method Summary collapse

Instance Method Details

#included_modelsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/toy/dynamo/tasks.rb', line 7

def included_models
  dir = ENV['DIR'].to_s != '' ? ENV['DIR'] : Rails.root.join("app/models")
  puts "Loading models from: #{dir}"
  included = []
  Dir.glob(File.join("#{dir}/**/*.rb")).each do |path|
    model_filename = path[/#{Regexp.escape(dir.to_s)}\/([^\.]+).rb/, 1]
    next if model_filename.match(/^concerns\//i) # Skip concerns/ folder

    begin
      klass = model_filename.camelize.constantize
    rescue NameError
      require(path) ? retry : raise
    rescue LoadError => e
      # Try non-namespaced class name instead...
      klass = model_filename.camelize.split("::").last.constantize
    end

    # Skip if the class doesn't have Toy::Dynamo integration
    next unless klass.respond_to?(:dynamo_table)

    included << klass
  end
  included
end