Skip to content

Instantly share code, notes, and snippets.

View dgorodnichy's full-sized avatar
🏠
Working from home

Dmitry Gorodnichy dgorodnichy

🏠
Working from home
  • Krasnodar
View GitHub Profile
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', '= 6.0.0'
class Posts
cattr_accessor :source
def initialize
@client = source.new
end
def posts(args)
@client.user_posts(args)
end
@dgorodnichy
dgorodnichy / get_posts_from_fake_medium.rb
Last active April 6, 2020 09:30
Dependency injection example
posts = Posts.new
posts.source = PostsSources::FakeMedium
posts.user_posts
@dgorodnichy
dgorodnichy / medium.rb
Last active April 9, 2020 15:48
medium.rb
class Medium
cattr_accessor :source
def initialize(name = nil)
@client = source.new(name)
end
def posts
@client.user_posts
end
@dgorodnichy
dgorodnichy / posts_source_remote.rb
Last active April 13, 2020 17:16
posts_adapter_medium.rb
module PostsSource
class Remote
def initialize(username)
@client = MediumAPI.new(username)
end
def user_posts
@client.posts
end
end
@dgorodnichy
dgorodnichy / posts_source_fake.rb
Last active April 9, 2020 15:46
posts_adapter_fake.rb
module PostsSource
class Fake
def initialize(username)
@client = OpenStruct.new(
posts: [
{
title: 'Signal v Noise exits Medium[Fake source]',
...
}
]
@dgorodnichy
dgorodnichy / initializer.rb
Last active April 13, 2020 17:16
initializer.rb
# frozen_string_literal: true
require 'posts_source/remote'
require 'medium/testing'
Medium::Testing.fake! if Rails.env.test?
require 'posts_source/fake'
class Medium::Testing
def self.fake!
Medium.source = PostsSource::Fake
end
end
posts = Medium.new('dhh')
posts.source = PostsSource::Remote
posts.user_posts
posts = Medium.new
posts.source = PostsSource::Fake
posts.user_posts