Module: Pragma::Operation::Macro::Classes

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

Class Method Summary collapse

Class Method Details

.for(input, options) ⇒ Object



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
41
42
43
# File 'lib/pragma/operation/macro/classes.rb', line 13

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.
      if 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.split.last
        raise e unless value.sub(/\A::/, '').start_with?(error_constant)
      end
    end

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