Sha256: 124b38263f756ce9b8cd4109ddf20de7c18ab04c79372205ed8810a733bec33e

Contents?: true

Size: 1.38 KB

Versions: 22

Compression:

Stored size: 1.38 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 MeducationSDK.const_defined?(original_klass)

      resource = "MeducationSDK::#{resource_klass}".constantize
      mock = "MeducationSDK::#{mock_klass}".constantize

      MeducationSDK.const_set(original_klass, resource)
      MeducationSDK.send(:remove_const, resource_klass)
      MeducationSDK.const_set(resource_klass, mock)
      MeducationSDK.send(:remove_const, mock_klass)
    end

    def unmock!
      return unless MeducationSDK.const_defined?(original_klass)

      original = "MeducationSDK::#{original_klass}".constantize
      resource = "MeducationSDK::#{resource_klass}".constantize

      MeducationSDK.const_set(mock_klass, resource)
      MeducationSDK.send(:remove_const, resource_klass)
      MeducationSDK.const_set(resource_klass, original)
      MeducationSDK.send(:remove_const, original_klass)
    end

    private
    def resource_klass
      @resource_name.camelize
    end

    def mock_klass
       "#{@resource_name}_mock".camelize
    end

    def original_klass
      "#{@resource_name}_original".camelize
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
meducation_sdk-1.3.0 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.6 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.5 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.4 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.3 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.2 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.1 lib/meducation_sdk/mocker.rb
meducation_sdk-1.2.0 lib/meducation_sdk/mocker.rb
meducation_sdk-1.1.2 lib/meducation_sdk/mocker.rb
meducation_sdk-1.1.1 lib/meducation_sdk/mocker.rb
meducation_sdk-1.1.0 lib/meducation_sdk/mocker.rb
meducation_sdk-1.0.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.10.1 lib/meducation_sdk/mocker.rb
meducation_sdk-0.10.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.9.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.8.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.7.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.6.1 lib/meducation_sdk/mocker.rb
meducation_sdk-0.6.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.5.0 lib/meducation_sdk/mocker.rb