Skip to content

Instantly share code, notes, and snippets.

View kieraneglin's full-sized avatar
😎

Kieran kieraneglin

😎
  • British Columbia, Canada
View GitHub Profile
@kieraneglin
kieraneglin / safe-access-object.js
Last active February 28, 2019 13:01
Safely Access Deeply Nested Objects In JavaScript
// Problem: how do you access a deeply nested object that you're not certain exists?
// if(object.user && object.user.post && object.user.post[0] && object.user.post[0].comment) {
// return object.user.post[0].comment
// }
// and that's horrifying. Here's something better
const idx = (props, object) => props.reduce((prefix, val) => (prefix && prefix[val]) ? prefix[val] : null, object)
// Usage:
nestedObj = {...}
@kieraneglin
kieraneglin / regex.js
Last active April 3, 2017 14:04
[JS OSX Duplicate File Regex] #tags: js, regex
new RegExp(/\(([\d^)]+)\)(?!\S)/igm)
@kieraneglin
kieraneglin / stop-puma.sh
Created April 6, 2017 21:47
[Stop Puma-dev] #tags: bash, rails, puma
pkill -USR1 puma-dev
@kieraneglin
kieraneglin / helper_proxy.rb
Created April 8, 2017 04:18
[Include all Rails helpers] If you need access to helpers, even private ones (for a decorator, say), try this. #tags: ruby, rails, helpers, decorators, liquid
module HelperProxy
HELPER_DIRECTORY = Dir[Rails.root.join('app', 'helpers', '**', '*.rb')]
HELPER_DIRECTORY.each do |file|
include file.split(/\/helpers\/(.*?)\.rb/)[1].classify.safe_constantize
end
end
@kieraneglin
kieraneglin / assert_differences.rb
Created May 27, 2017 04:53
assert_differences
def assert_differences(expression_array, by:, message: nil, &block)
expressions = Array(expression_array)
change_by = Array(by).map(&:to_i)
if expressions.size != change_by.size
raise ArgumentError, 'Each expression must have a corresponding value to change by'
end
exps = expressions.map.with_index do |e, i|
callable = e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
@kieraneglin
kieraneglin / README.md
Created June 8, 2017 01:40
js-map-label for Node

Author

Original author is rcknr. I manually transpiled to use JS classes

Usage

See here

Otherwise, put this gist into a file called map-label.js and include with import MapLabel from './map-label'. From there, follow the instructions outlined above.

@kieraneglin
kieraneglin / old_syntax_example.rb
Last active December 18, 2017 06:57
assert_differences old syntax
assert_difference('User.count', -1) do
assert_difference('GoodbyeMailerJob.size', +1) do
assert_difference('Post.count', -5) do
# Logic...
end
end
end
@kieraneglin
kieraneglin / new_syntax_example.rb
Last active December 18, 2017 16:55
assert_differences new syntax example
assert_differences(['User.count', 'Post.count'], by: [-1, -5]) do
# Logic...
end
assert_differences('User.count', by: -1) do
# Logic... (although you should be using assert_difference in this case)
end
assert_differences(-> { User.count }, by: -1) do
# I can do lambdas too!
@kieraneglin
kieraneglin / assert_differences_code.rb
Created December 18, 2017 07:04
assert_differences code example
# Drop into test_helper.rb or similar
def assert_differences(expression_array, by:, message: nil, &block)
expressions = Array(expression_array)
change_by = Array(by).map(&:to_i)
if expressions.size != change_by.size
raise ArgumentError, 'Each expression must have a corresponding value to change by'
end
exps = expressions.map.with_index do |e, i|
@kieraneglin
kieraneglin / punchbox_sample.js
Created December 18, 2017 07:34
punchbox sample
class Post { // The name of this class doesn't matter, nor does the filepath
constructor() {
// Code related to this class' setup. Not required for Punchbox to run
}
controller() {
// Code within here will run on every action of the controller
}
index() {
// This code will run on the Posts#index action specifically
}