Todo app
I've included a link to a simple Todo application built in backbone with a detailed commit history. I would recommend going through the repo from the first commit to the last. It should be clear what
require 'stock_quote' | |
require 'pp' | |
class StocksController < ApplicationController | |
before_action :load_user | |
skip_before_filter :verify_authenticity_token | |
def index | |
update_stocks | |
end |
# ###################### | |
# Refactored | |
# ###################### | |
# app/controllers/stocks_controller.rb | |
require 'stock_quote' | |
require 'pp' | |
# ###################### | |
# Refactored | |
# ###################### | |
# app/services/stock_service.rb | |
class StockService | |
def initialize(user) | |
@user = user | |
@stocks = [] | |
@stock_symbols = [] |
# 1 -------------------BAD | |
class Gear | |
attr_reader :chainring, :cog | |
def initialize(chainring, cog) | |
@chainring = chainring | |
@cog = cog | |
end | |
def ratio |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Needs to Manage Dependencies | |
class Gear | |
attr_reader :chainring, :cog, :rim, :tire | |
def initialize(chainring, cog, rim, tire) | |
@chainring = chainring | |
@cog = cog | |
@rim = rim |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Law of Demeter | |
# set of coding rules that results in loosely coupled objects. It prohibits | |
# routing a message to a third object via a second object of a different type. | |
# Paraphrased: "only talk to your immediate neighbors", "use only one dot" | |
class Trip | |
def depart | |
# "train wrecks" |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Overlooking Ducks | |
# 1 -------------------BAD | |
class Trip | |
attr_reader :bicycles, :customers, :vehicle | |
def prepare(mechanic) | |
# no depedency on mecahnic |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Starting Class | |
# 1 -------------------Okay | |
class Bicycle | |
attr_reader :size, :tape_color | |
def initialize(args) | |
@size = args[:size] |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Writing the Concrete Code | |
# 1 -------------------Bad | |
class Schedule | |
def scheduled?(schedulable, start_date, end_date) | |
puts "This #{schedulable.class} " + | |
"is not scheduled\n" + | |
" between #{start_date} and #{end_date}" |