Class: ActiveMocker::MockCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mocker/mock_creator.rb

Defined Under Namespace

Modules: Associations, Attributes, ClassMethods, DefinedMethods, ModulesConstants, RecreateClassMethodCalls, Scopes Classes: AliasAttributeMethod, Method

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, file_out:, schema_scrapper:, template_creator: nil, class_introspector: nil, enabled_partials: nil, klasses_to_be_mocked:, mock_append_name:, active_record_model:, active_record_base_klass: ActiveRecord::Base) ⇒ MockCreator

Returns a new instance of MockCreator.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_mocker/mock_creator.rb', line 4

def initialize(file:,
               file_out:,
               schema_scrapper:,
               template_creator: nil,
               class_introspector: nil,
               enabled_partials: nil,
               klasses_to_be_mocked:,
               mock_append_name:,
               active_record_model:,
               active_record_base_klass: ActiveRecord::Base)
  @file                     = file
  @file_out                 = file_out
  @schema_scrapper          = schema_scrapper
  @template_creator         = template_creator || template_creator_default(file_out)
  @class_introspector       = class_introspector || class_introspector_default
  @enabled_partials         = enabled_partials || self.class.enabled_partials_default
  @klasses_to_be_mocked     = klasses_to_be_mocked
  @active_record_base_klass = active_record_base_klass
  @mock_append_name         = mock_append_name
  @active_record_model      = active_record_model
  @errors                   = []
  @completed                = false
end

Instance Attribute Details

#active_record_modelObject (readonly)

Returns the value of attribute active_record_model.



141
142
143
# File 'lib/active_mocker/mock_creator.rb', line 141

def active_record_model
  @active_record_model
end

#errorsObject (readonly)

Returns the value of attribute errors.



46
47
48
# File 'lib/active_mocker/mock_creator.rb', line 46

def errors
  @errors
end

#parent_classObject (readonly)

Returns the value of attribute parent_class.



141
142
143
# File 'lib/active_mocker/mock_creator.rb', line 141

def parent_class
  @parent_class
end

Class Method Details

.enabled_partials_defaultObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_mocker/mock_creator.rb', line 83

def enabled_partials_default
  [
    :modules_constants,
    :class_methods,
    :attributes,
    :scopes,
    :recreate_class_method_calls,
    :defined_methods,
    :associations,
  ]
end

Instance Method Details

#class_nameObject



131
132
133
# File 'lib/active_mocker/mock_creator.rb', line 131

def class_name
  @class_name ||= class_introspector.parsed_source.class_name.split("::").last
end

#completed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/active_mocker/mock_creator.rb', line 42

def completed?
  @completed
end

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_mocker/mock_creator.rb', line 28

def create
  verify_class
  if errors.empty?
    begin
      template_creator.render
    rescue => e
      raise e unless error_already_collected?(e)
    end
    file_out.close
    @completed = true
  end
  self
end

#nested_modulesObject



135
136
137
138
139
# File 'lib/active_mocker/mock_creator.rb', line 135

def nested_modules
  @nested_modules ||= begin
    class_introspector.parsed_source.module_nesting.join("::")
  end
end

#partialsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_mocker/mock_creator.rb', line 110

def partials
  OpenStruct.new(enabled_partials.each_with_object({}) do |p, hash|
    begin
      file = File.new(File.join(File.dirname(__FILE__), "mock_template/_#{p}.erb"))
      extend("ActiveMocker::MockCreator::#{p.to_s.camelize}".constantize)
      hash[p] = ERB.new(file.read, nil, "-", "_sub#{p}").result(binding)
    rescue => e
      errors << ErrorObject.new(class_name:     class_name,
                                original_error: e, type: :generation,
                                level:          :error,
                                message:        e.message)
      errors << ErrorObject.new(class_name:     class_name,
                                original_error: e,
                                type:           :erb,
                                level:          :debug,
                                message:        "Erb template: #{p} failed.\n#{file.path}")
      raise e
    end
  end)
end

#primary_keyObject



143
144
145
# File 'lib/active_mocker/mock_creator.rb', line 143

def primary_key
  @primary_key ||= ActiveRecordSchemaScrapper::Attribute.new(name: "id", type: :integer)
end