Skip to content

Instantly share code, notes, and snippets.

@rachidcalazans
rachidcalazans / UserRepoTests.swift
Created September 2, 2017 19:12
Test Class #1 for the Post - better-setups-on-swift-tests
import XCTest
class UserRepoTests: XCTestCase {
var userRepo = UserRepo()
var email = "user@gmail.com"
var result = [User]()
override func setUp() {
super.setUp()
@rachidcalazans
rachidcalazans / UserRepoRefactoredTests.swift
Created September 2, 2017 19:14
Test Class #2 for the Post - better-setups-on-swift-tests
import XCTest
class UserRepoRefactoredTests: XCTestCase {
var userRepo = UserRepo()
var email = "user@gmail.com"
var result = [User]()
lazy var findAdminsByAction: Action = {
return Action() {
self.createAdmin(name: "User Admin 1")
@rachidcalazans
rachidcalazans / UserRepoTest.swift
Created September 2, 2017 19:14
Test Class #3 for the Post - better-setups-on-swift-tests
import Quick
import Nimble
class UserRepoTest: QuickSpec {
override func spec() {
var userRepo = UserRepo()
describe("#findAdminsBy") {
@rachidcalazans
rachidcalazans / Executer.swift
Last active September 2, 2017 21:33
Helper Test file for the Post - better-setups-on-swift-tests
import Foundation
struct Action {
let block: () -> ()
func call() {
block()
}
}
@rachidcalazans
rachidcalazans / sample_01.rb
Created December 31, 2017 17:01
Example I for the Post - avoiding-conditionals
# If Else Statment
def render_msg(state)
if state == :in_progress
return 'In Progress'
elsif state == :done
return 'Done'
elsif state == :on_review
return 'On Review'
end
@rachidcalazans
rachidcalazans / sample_02.rb
Created December 31, 2017 17:02
Example I refactored for the Post - avoiding-conditionals
def render_msg(state)
state_msgs = {
in_progress: 'In Progress',
done: 'Done',
on_review: 'On Review'
}
state_msgs[state]
end
@rachidcalazans
rachidcalazans / sample_03.rb
Last active January 2, 2018 13:06
Example II for the Post - avoiding-conditionals
def extract_params(state, params)
if state == :in_progress
return params.select { |k, v| [:key_a, :key_b].include?(k) }
elsif state == :done
return params.select { |k, v| [:key_c, :key_d].include?(k) }
elsif state == :on_review
return params.select { |k, v| [:key_e, :key_f].include?(k) }
end
end
@rachidcalazans
rachidcalazans / sample_04.rb
Last active January 2, 2018 13:06
Example II Refactored for the Post - avoiding-conditionals
def extract_params(state, params)
state_keys = {
in_progress: %I[key_a key_b],
done: %I[key_c key_d],
on_review: %I[key_e key_f]
}
params.select { |k, v| state_keys[state].include?(k) }
end
@rachidcalazans
rachidcalazans / sample_05.rb
Last active January 2, 2018 13:07
Example III for the Post - avoiding-conditionals
def save(state, params)
if state == :in_progress
return save_in_progress(params)
elsif state == :done
return save_done(params)
elsif state == :on_review
return save_on_review(params)
end
end
@rachidcalazans
rachidcalazans / sample_06.rb
Last active January 2, 2018 13:07
Example III Refactored for the Post - avoiding-conditionals
def save(state, params)
state_action = {
in_progress: :save_in_progress,
done: :save_done,
on_review: :save_on_review
}
action = state_action[state]
self.send(action, params)
end