Sha256: 0899cc8907a118478f3e708f35f227b667f073f2eac20fe3a259af260a050f75

Contents?: true

Size: 1.38 KB

Versions: 7

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.classify
    end

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
meducation_sdk-0.3.4 lib/meducation_sdk/mocker.rb
meducation_sdk-0.3.3 lib/meducation_sdk/mocker.rb
meducation_sdk-0.3.2 lib/meducation_sdk/mocker.rb
meducation_sdk-0.3.1 lib/meducation_sdk/mocker.rb
meducation_sdk-0.3.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.2.0 lib/meducation_sdk/mocker.rb
meducation_sdk-0.1.0 lib/meducation_sdk/mocker.rb