Module: ChoronSupport::Helper

Defined in:
lib/choron_support/helper.rb

Class Method Summary collapse

Class Method Details

.generate_choron_class(namespaces, model_name, class_symbol, exception: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/choron_support/helper.rb', line 6

def generate_choron_class(namespaces, model_name, class_symbol, exception: true)
  # 命名規則に従いQueryクラスを自動で探して scope を設定する
  namespace = if namespaces.is_a?(Array)
                namespaces.join("::")
              else
                namespaces.to_s
              end

  unless model_name.to_s.empty?
    namespace = "#{namespaces}::#{model_name.pluralize}"
  end

  target_class_name = "#{namespace}::#{class_symbol.to_s.camelize}"
  # ? や ! 終わりはクラスに変換できないため
  if target_class_name.end_with?("?") || target_class_name.end_with?("!")
    target_class_name.chop!
  end

  # 例: Queries::Users::NotLogined
  target_class = nil
  begin
    target_class = target_class_name.constantize
  rescue NameError => e
    if exception
      raise e
    end
  end

  target_class
end