Class: ActiveMocker::Generate

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

Defined Under Namespace

Classes: OtherErrors

Instance Method Summary collapse

Constructor Details

#initializeGenerate

Returns a new instance of Generate.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
# File 'lib/active_mocker/generate.rb', line 4

def initialize
  raise ArgumentError, "mock_dir is missing a valued value!" if config.mock_dir.nil? || config.mock_dir.empty?
  create_mock_dir
  raise ArgumentError, "model_dir is missing a valued value!" if config.model_dir.nil? || config.model_dir.empty? || !Dir.exists?(config.model_dir)
  @display_errors = DisplayErrors.new(models_paths.count)
end

Instance Method Details

#callObject

Returns self.

Returns:

  • self



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/active_mocker/generate.rb', line 12

def call
  clean_up
  progress_init
  models_paths.each do |file|
    model_name      = model_name(file)
    model           = get_model_const(model_name)
    mock_file_name  = "#{model_name.underscore}_#{config.mock_append_name.underscore}.rb"
    mock_file_path  = File.join(Config.mock_dir, mock_file_name)
    assure_dir_path_exists(mock_file_path)
    schema_scrapper = ActiveRecordSchemaScrapper.new(model: model)
    File.open(mock_file_path, 'w') do |file_out|
      begin
        result                       = create_mock(file, file_out, schema_scrapper)
        status                       = collect_errors(mock_file_path, result.errors, schema_scrapper, model_name)
        display_errors.success_count += 1 if result.completed? && status.successful?
      rescue => e
        rescue_clean_up(e, file_out, model_name)
      end
    end
    progress.increment
  end
  display_errors.display_errors
  self
end

#get_model_const(model_name) ⇒ Object



37
38
39
40
41
# File 'lib/active_mocker/generate.rb', line 37

def get_model_const(model_name)
  model_name.constantize
rescue StandardError, LoadError => e
  display_errors.wrap_an_exception(e, model_name)
end