Skip to content

Instantly share code, notes, and snippets.

View ihollander's full-sized avatar

Ian Hollander ihollander

View GitHub Profile
@ihollander
ihollander / my_cli.rb
Last active October 23, 2018 19:06
Thor step 1
require 'thor'
require 'faker'
class HeroCLI < Thor
end
HeroCLI.start(ARGV)
@ihollander
ihollander / my_cli.rb
Last active October 23, 2018 02:00
Thor step 2
class HeroCLI < Thor
desc "superhero", "Display a superhero"
def superhero
my_hero = "#{Faker::Superhero.name} #{Faker::Superhero.suffix}"
puts my_hero
end
end
@ihollander
ihollander / my_cli.rb
Last active October 23, 2018 02:00
Thor step 3
class HeroCLI < Thor
desc "superhero WEAKNESS", "Display a superhero and its weakness."
def superhero(weakness)
my_hero = "#{Faker::Superhero.name} #{Faker::Superhero.suffix}"
puts "#{my_hero} has been foiled again by #{weakness}!"
end
end
@ihollander
ihollander / my_cli.rb
Last active October 23, 2018 02:00
Thor step 4
class HeroCLI < Thor
desc "superhero [WEAKNESS]", "Display a superhero and its weakness."
def superhero(weakness='bad puns')
my_hero = "#{Faker::Superhero.name} #{Faker::Superhero.suffix}"
puts "#{my_hero} has been foiled again by #{weakness}!"
end
end
@ihollander
ihollander / my_cli.rb
Last active October 23, 2018 02:18
Thor step 5
class HeroCLI < Thor
...
desc "assemble TEAM", "Displays a TEAM of superheroes."
option :size, :type => :numeric, :required => true, :desc => "How many superheroes to assemble"
def assemble(team)
puts "***#{team} Assemble***"
options[:size].times do
puts "#{Faker::Superhero.name} #{Faker::Superhero.suffix}!"
@ihollander
ihollander / eventemitter.js
Created November 27, 2018 18:04
Observer Pattern pt1
// implementing event emitter pattern
class EventEmitter {
constructor() {
this.events = {} // initialize with empty events object
}
addEventListener(eventLabel, callback) { // create a new key for our event with the eventLabel
if (!this.events[eventLabel])
this.events[eventLabel] = []
@ihollander
ihollander / fluxmodel.js
Created November 27, 2018 18:11
Observer Pattern pt2
class FluxModel extends EventEmitter {
constructor() {
super() // initialize the EventEmitter
this.time = new Date() // set the current time
this.isNighttime = this.getNighttime() // check if it is nighttime
setInterval(this.updateTime.bind(this), 60000) // update our model's state every minute
}
updateTime() {
let previousIsNighttimeState = this.isNighttime // save the PREVIOUS STATE of our application
@ihollander
ihollander / fluxcontroller.js
Last active November 27, 2018 18:17
Observer Pattern pt3
class FluxController extends EventEmitter {
constructor(model, view) {
super()
this.model = model
this.view = view
// add event listeners for changes to our model state
model.addEventListener('daytime', () => this.changeViewToDaytime())
model.addEventListener('nighttime', () => this.changeViewToNighttime())
@ihollander
ihollander / fluxview.js
Created November 27, 2018 18:20
Observer Pattern pt4
class FluxView {
constructor(components) {
this.body = components["body"]
}
}
@ihollander
ihollander / index.js
Last active November 27, 2018 18:22
Observer Pattern pt4
document.addEventListener('DOMContentLoaded', () => {
const body = document.querySelector('body')
const fluxModel = new FluxModel()
const fluxView = new FluxView({ body: body })
const fluxController = new FluxController(fluxModel, fluxView)
})