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, Scopes Classes: 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_base_klass: ActiveRecord::Base) ⇒ MockCreator

Returns a new instance of MockCreator.



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

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_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
  @errors                   = []
  @completed                = false
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Class Method Details

.enabled_partials_defaultObject



79
80
81
# File 'lib/active_mocker/mock_creator.rb', line 79

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

Instance Method Details

#class_nameObject



112
113
114
# File 'lib/active_mocker/mock_creator.rb', line 112

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

#completed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_mocker/mock_creator.rb', line 39

def completed?
  @completed
end

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_mocker/mock_creator.rb', line 25

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



116
117
118
119
120
# File 'lib/active_mocker/mock_creator.rb', line 116

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

#parent_classObject



122
123
124
# File 'lib/active_mocker/mock_creator.rb', line 122

def parent_class
  @parent_class
end

#partialsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_mocker/mock_creator.rb', line 98

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"))
      self.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



126
127
128
# File 'lib/active_mocker/mock_creator.rb', line 126

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