Sha256: 72ecefa0d339acf003610710108e3adf15343342f837882c45caf5ab783a0027

Contents?: true

Size: 1.93 KB

Versions: 41

Compression:

Stored size: 1.93 KB

Contents

module MeducationSDK
  class Mocker
    def self.mock!
      RESOURCES.each do |resource|
        new(resource).mock!
      end
    end

    def self.unmock!
      RESOURCES.each do |resource|
        new(resource).unmock!
      end
    end

    def initialize(resource_name)
      @resource_name = resource_name
    end

    def mock!
      return if mock_module.const_defined?(original_class_name)

      resource = "#{mock_module_name}::#{resource_class_name}".constantize
      mock = "#{mock_module_name}::#{mock_class_name}".constantize

      mock_module.const_set(original_class_name, resource)
      mock_module.send(:remove_const, resource_class_name)
      mock_module.const_set(resource_class_name, mock)
      mock_module.send(:remove_const, mock_class_name)
    end

    def unmock!
      return unless mock_module.const_defined?(original_class_name)

      original = "#{mock_module_name}::#{original_class_name}".constantize
      resource = "#{mock_module_name}::#{resource_class_name}".constantize

      mock_module.const_set(mock_class_name, resource)
      mock_module.send(:remove_const, resource_class_name)
      mock_module.const_set(resource_class_name, original)
      mock_module.send(:remove_const, original_class_name)
    end

    private
    def resource_class_name
      @resource_class_name ||= @resource_name.camelize.split("::").last
    end

    def mock_class_name
       @mock_class_name ||= "#{@resource_name}_mock".camelize.split("::").last
    end

    def original_class_name
      @original_class_name ||= "#{@resource_name}_original".camelize.split("::").last
    end

    def mock_module
      @mock_module ||= mock_module_name.constantize
    end

    def mock_module_name
      @mock_module_name ||= begin
        parts = @resource_name.camelize.split("::")
        if parts.size == 1
          "MeducationSDK"
        else
          parts.pop
          "MeducationSDK::#{parts.join("::")}"
        end
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
meducation_sdk-2.2.2 lib/meducation_sdk/mocker.rb
meducation_sdk-2.2.1 lib/meducation_sdk/mocker.rb
meducation_sdk-2.2.0 lib/meducation_sdk/mocker.rb
meducation_sdk-2.1.4 lib/meducation_sdk/mocker.rb
meducation_sdk-2.1.3 lib/meducation_sdk/mocker.rb
meducation_sdk-2.1.2 lib/meducation_sdk/mocker.rb
meducation_sdk-2.1.1 lib/meducation_sdk/mocker.rb
meducation_sdk-2.1.0 lib/meducation_sdk/mocker.rb
meducation_sdk-2.0.5 lib/meducation_sdk/mocker.rb
meducation_sdk-2.0.4 lib/meducation_sdk/mocker.rb
meducation_sdk-2.0.3 lib/meducation_sdk/mocker.rb
meducation_sdk-2.0.2 lib/meducation_sdk/mocker.rb
meducation_sdk-2.0.1 lib/meducation_sdk/mocker.rb
meducation_sdk-2.0.0 lib/meducation_sdk/mocker.rb
meducation_sdk-1.6.16 lib/meducation_sdk/mocker.rb
meducation_sdk-1.6.15 lib/meducation_sdk/mocker.rb
meducation_sdk-1.6.14 lib/meducation_sdk/mocker.rb
meducation_sdk-1.6.13 lib/meducation_sdk/mocker.rb
meducation_sdk-1.6.12 lib/meducation_sdk/mocker.rb
meducation_sdk-1.6.11 lib/meducation_sdk/mocker.rb