Skip to content

Instantly share code, notes, and snippets.

View pdbradley's full-sized avatar

Philip Bradley pdbradley

View GitHub Profile
@pdbradley
pdbradley / blur3.rb
Created June 30, 2017 00:09
blur3.rb
require 'pry'
class Image
def initialize(image)
@image = Marshal.load(Marshal.dump(image))
end
def output_image
@image.each do |x|
puts x.join
@pdbradley
pdbradley / king.rb
Created October 16, 2017 19:30
ali king
class King < ChessPiece
# King specific methods ...
def valid_move?(x_target, y_target)
return false if same_location?(x_target, y_target)
return false if !in_board?(x_target, y_target)
return true if move_single_step?(x_target, y_target)
return false
end
@pdbradley
pdbradley / king_spec.rb
Created October 16, 2017 19:31
ali king spec
require 'rails_helper'
RSpec.describe King, type: :class do
describe '.valid_move?' do
it "should check for valid move for a King" do
user = FactoryGirl.create(:user)
piece = FactoryGirl.create(:king, user_id: user.id)
piece.x = 4; piece.y = 4; piece.color = "white"
expect(piece.valid_move?(piece.x+1, piece.y+0)).to eq(true)
@pdbradley
pdbradley / yeahbuddy.rb
Last active December 23, 2017 19:01
getter setter encrypted stuff (weak but good enough)
# assuming a column called encrypted password, and you need to
# get access to the plaintext to do work, put these getters
# and setters in your class to magically get what you need
def password
Base64.decode64(self.encrypted_password)
end
def password=(string)
@pdbradley
pdbradley / hotwings.rb
Created March 4, 2018 01:07
jeremiah presenter
products = Product.by_company(current_user.company.slug).includes(:facilities, :total_stocks).distinct(:sku)
sql = "SELECT DISTINCT ON (product_id) * FROM facility_stocks WHERE product_id IN (#{@products.pluck(:id).join(', ')}) ORDER BY product_id, created_at DESC"
facility_stocks = ActiveRecord::Base.connection.execute(sql)
facilities = Facility.all # or whatever conditions you are putting on that
# then construct a collection of stuff you want to show in the view.
# Basic principle: don't calculate anything in a view unless absolutely necessary
# "separation of concerns" helps you know where to look for your logic
@pdbradley
pdbradley / invoice_adjustment.rb
Created June 6, 2018 03:02
trigger to update invoice total whenever there is an adjustment change
class AddTriggerForInvoiceAmount < ActiveRecord::Migration[5.1]
def change
reversible do |dir|
dir.up do
execute <<-SQL
CREATE OR REPLACE FUNCTION calculate_invoice_current_balance()
RETURNS trigger AS $calc_invoice_balance$
BEGIN
IF (TG_OP = 'DELETE') THEN
@pdbradley
pdbradley / yeahman.js
Created May 9, 2019 18:25
evented chat
#= require framework7.bundle.js
var f7_app = new Framework7({
// App root element
root: '#app',
// App Name
name: 'My App',
// App id
id: 'com.myapp.test',
// Enable swipe panel
@pdbradley
pdbradley / dontgetit.js
Created May 23, 2019 21:37
help us dave
class dontGetIt {
constructor() {
this.foo = 'hello';
}
hello() {
this.foo;
}
}
@pdbradley
pdbradley / bleah
Created July 19, 2019 02:53
throwaway
set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath = &runtimepath
source ~/.vimrc
let g:python2_host_prog = '/usr/local/bin/python'
let g:python3_host_prog = '/usr/local/bin/python3'
@pdbradley
pdbradley / keybase.md
Created February 19, 2020 18:21
keybase

Keybase proof

I hereby claim:

  • I am pdbradley on github.
  • I am pdbradley (https://keybase.io/pdbradley) on keybase.
  • I have a public key ASD5GtzA3z5X7nNf451URVza1LKYC1CUf3Joimi7p7X2Ago

To claim this, I am signing this object: