Sha256: fbf5b42d4d66562936e0ac40782f3d09346f32471566fa255e6eacf98ed1461f

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
module ActiveMocker
  class ParentClass
    def initialize(parsed_source:, klasses_to_be_mocked:, mock_append_name:, active_record_base_klass: ::ActiveRecord::Base)
      @parsed_source            = parsed_source
      @klasses_to_be_mocked     = klasses_to_be_mocked
      @active_record_base_klass = active_record_base_klass
      @mock_append_name         = mock_append_name
    end

    attr_reader :error

    def call
      if parent_class?
        deal_with_parent
      else
        create_error("#{class_name} is missing a parent class.")
      end
      self
    end

    def parent_mock_name
      if @parent_mock_name
        "#{@parent_mock_name}#{mock_append_name}"
      else
        "ActiveMocker::Base"
      end
    end

    private

    attr_reader :parsed_source,
                :klasses_to_be_mocked,
                :active_record_base_klass,
                :mock_append_name

    def deal_with_parent
      if parent_class <= active_record_base_klass
        @parent_mock_name = parent_class_name if included_mocked_class?
      else
        create_error("#{class_name} does not inherit from ActiveRecord::Base")
      end
    end

    def create_error(message)
      @error = OpenStruct.new(class_name: class_name,
                              message:    message)
    end

    def parent_class?
      parsed_source.parent_class?
    end

    def parent_class_name
      parsed_source.parent_class_name
    end

    def class_name
      parsed_source.class_name
    end

    def parent_class
      parent_class_name.constantize
    end

    def included_mocked_class?
      sanitize_consts(klasses_to_be_mocked).include?(sanitize_consts(parent_class_name).first)
    end

    def sanitize_consts(consts)
      [*consts].map do |const|
        const.split("::").reject(&:empty?).join("::")
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_mocker-2.6.2 lib/active_mocker/parent_class.rb
active_mocker-2.6.1.beta2 lib/active_mocker/parent_class.rb
active_mocker-2.6.1.beta lib/active_mocker/parent_class.rb
active_mocker-2.6.0 lib/active_mocker/parent_class.rb
active_mocker-2.5.4 lib/active_mocker/parent_class.rb
active_mocker-2.5.3 lib/active_mocker/parent_class.rb
active_mocker-2.5.2 lib/active_mocker/parent_class.rb
active_mocker-2.5.1 lib/active_mocker/parent_class.rb
active_mocker-2.5.1.pre lib/active_mocker/parent_class.rb