Skip to content

Instantly share code, notes, and snippets.

Avatar

Yury Kaspiarovich yukas

View GitHub Profile
@yukas
yukas / add_attendee_page.rb
Last active May 31, 2017
Obeying the "Single Method of Abstraction" rule
View add_attendee_page.rb
class AddAttendeePage
def add_attendee_with_details
fill_attendee_details
fill_order_form
click_add_attendee
end
private
def fill_attendee_details
@yukas
yukas / add_attendee_page.rb
Last active May 31, 2017
Violation of "Single Level of Abstraction" rule
View add_attendee_page.rb
class AddAttendeePage
def add_attendee_with_details
fill_in('user_email', with: 'test@gmail.com')
fill_in('user_first_name', with: 'test')
fill_in('user_last_name', with: 'test')
fill_order_form
click_add_attendee
end
private
@yukas
yukas / cleaner_controller.rb
Last active Mar 24, 2017
Cleaner controller with service object
View cleaner_controller.rb
# A controller
module Api::V2
module Controllers::ZonesRecords
class Create
include Hanami::Action
def call(params)
@zone = DomainFinder.find!(
params[:zone_id], authentication_context).zone
@yukas
yukas / greeter.rb
Last active Jan 5, 2017
Plain Old Decorators
View greeter.rb
class Greeter
def greet(user)
user_name_decorator = UserNameDecorator.new(user)
@greeting = user_name_decorator.dear_mr_user
@greeting << "\n"
@greeting << "Bla bla bla"
...
@greeting << "\n"
end
View dry_for_good.rb
class ConfigFile
FILE_PATH = 'path/to/config.cfg'
def write_player_state(state)
File.open(FILE_PATH, 'w') do |f|
f.write('player_state: #{state}')
end
end
def read_player_state
View wrong_dry.rb
class UsersController < ApplicationController
before_filter :set_user, only: [:show, :edit, :update]
def show
end
def edit
end
def update
View accidental_duplication_example.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
...
end
def edit 
@user = User.find(params[:id])
...
end
View duplication_example.rb
class Game
def save_player_state
File.open('path/to/config.cfg', 'a') do |f|
f.write('player_state: near_window')
end
end
end
class Player
def read_state
@yukas
yukas / user_creator_spec.rb
Created Jul 28, 2016
Testing service object
View user_creator_spec.rb
describe UserCreator do
describe "#create_user" do
it "creates user" do
user_creator = UserCreator.new("ian@brown.com", "Ian", "Brown")
user_creator.create_user
expect(user_creator.created_user.email).to eq("ian@brown.com")
expect(user_creator.created_user.first_name).to eq("Ian")
expect(user_creator.created_user.last_name).to eq("Brown")
end
@yukas
yukas / user_creator.rb
Created Jul 28, 2016
Example of a good service object
View user_creator.rb
class UserCreator
attr_reader :email, :first_name, :last_name
attr_reader :created_user
def initialize(email, first_name, last_name)
@email = email
@first_name = first_name
@last_name = last_name
raise ArgumentError, "Missing email" if email.nil?