steemit / steem-ruby

Steem-ruby is the official Ruby library for the Steem blockchain
MIT License
9 stars 15 forks source link

Simple dependency injection #4

Closed inertia186 closed 6 years ago

inertia186 commented 6 years ago

As an API client developer, I want to be able to inject certain functionality into steem-ruby, so I don't have to resort to monkeypatching.

At the moment, steem-ruby has a modular design to allow other gems to replace things like the RPC Client. But in order to do this, these gems will have to use monkeypatching. One gem that does this is steem-mechanize.

The steem-mechanize gem provides exactly one feature. It replaces the RPC Client a specialized singleton. The way steem-mechanize does this is with this bit of code that runs when the gem is required:

module Steem
  class Api
    def self.default_rpc_client_class
      Steem::Mechanize::RPC::MechanizeClient
    end
  end
end

Instead, support for this idiom is better:

Steem::Api.register default_rpc_client_class: Steem::Mechanize::RPC::MechanizeClient

AC