Module: Pragma::Macro::Classes

Defined in:
lib/pragma/macro/classes.rb

Class Method Summary collapse

Class Method Details

.for(input, options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pragma/macro/classes.rb', line 12

def for(input, options)
  {
    'model.class' => expected_model_class(input, options),
    'policy.default.class' => expected_policy_class(input, options),
    'policy.default.scope.class' => expected_policy_scope_class(input, options),
    'decorator.instance.class' => expected_instance_decorator_class(input, options),
    'decorator.collection.class' => expected_collection_decorator_class(input, options),
    'contract.default.class' => expected_contract_class(input, options)
  }.each_pair do |key, value|
    next if options[key]

    # FIXME: This entire block is required to trigger Rails autoloading. Ugh.
    begin
      Object.const_get(value)
    rescue NameError => e
      # We check the error message to avoid silently ignoring other NameErrors
      # thrown while initializing the constant.
      raise e unless e.message.start_with?('uninitialized constant')

      # Required instead of a simple equality check because loading
      # API::V1::Post::Contract::Index might throw "uninitialized constant
      # API::V1::Post::Contract" if the resource has no contracts at all.
      error_constant = e.message.scan(/uninitialized constant ([^\s]+)/).first.first
      raise e unless value.sub(/\A::/, '').start_with?(error_constant)
    end

    options[key] = (Object.const_get(value) if Object.const_defined?(value))
  end
end