Skip to content

Instantly share code, notes, and snippets.

View stephancom's full-sized avatar

stephan.com stephancom

View GitHub Profile
@stephancom
stephancom / valid_values.rb
Created March 27, 2018 20:02
Get valid values for a field with :inclusion validation via inspection
def valid_values(klass, kolumn)
klass.validators_on(kolumn).select { |v| v.is_a? ActiveModel::Validations::InclusionValidator }.first.options[:in].map(&:to_s)
end
@stephancom
stephancom / valid_values.rb
Created March 27, 2018 20:02
Get valid values for a field with :inclusion validation via inspection
def valid_values(klass, kolumn)
klass.validators_on(kolumn).select { |v| v.is_a? ActiveModel::Validations::InclusionValidator }.first.options[:in].map(&:to_s)
end
@stephancom
stephancom / obscure_email.rb
Created April 13, 2018 18:16
Obscures an email address with asterisks
# hide email, eg john.doe@gmail.com -> j******o@g*******m
def obscure_email(email)
midstars = -> (s) { s.length > 2 ? s[0] + '*' * (s.length - 2) + s[-1] : s[0]+'*' }
email.split('@').map { |p| midstars.call(p) }.join('@')
end
@stephancom
stephancom / asciify-fonts.js
Created August 10, 2018 00:27
show all asciify fonts in node.js
asciify = require('asciify');
asciify.getFonts(function (err, fonts) { fonts.forEach(function(f) { asciify(f, { font: f }, function(e, r) { console.log(r); }) }) });
@stephancom
stephancom / phone_format.rb
Last active January 16, 2019 06:31
Phone Format test
#! /usr/bin/ruby
# _ _
# ___| |_ ___ _ __ | |__ __ _ _ __ ___ ___ _ __ ___
# / __| __/ _ \ '_ \| '_ \ / _` | '_ \ / __/ _ \| '_ ` _ \
# \__ \ || __/ |_) | | | | (_| | | | || (_| (_) | | | | | |
# |___/\__\___| .__/|_| |_|\__,_|_| |_(_)___\___/|_| |_| |_|
# |_|
# coding challenge for Simplero
module TableHelpers
module ArrayMethods
def find_row(expected_row)
find_index do |row|
expected_row.all? do |expected_column|
first_column = row.find_index { |column|
content = normalize_content(column.content)
expected_content = normalize_content(expected_column)
matching_parts = expected_content.split(/\s*\*\s*/, -1).collect { |part| Regexp.escape(part) }
matching_expression = /\A#{matching_parts.join(".*")}\z/
@stephancom
stephancom / stdio.rb
Created February 11, 2019 21:10
Handy stdio helpers for Rspec - suppress output and fake input
# _ _ _ _ _
# ___| |_ __| (_) ___ | |__ ___| |_ __ ___ _ __ ___
# / __| __/ _` | |/ _ \ | '_ \ / _ \ | '_ \ / _ \ '__/ __|
# \__ \ || (_| | | (_) | | | | | __/ | |_) | __/ | \__ \
# |___/\__\__,_|_|\___/ |_| |_|\___|_| .__/ \___|_| |___/
# |_| by stephan.com
# especially handy for testing interactive rake tasks
# best in an rspec around block, and can even be chained eg:
#
@stephancom
stephancom / thank_you.txt
Created March 11, 2019 23:25
ascii thank you suitable for facebook response even on mobile
!!!
┏┓┏┓╋╋╋╋╋╋┏┓
┃┗┫┗┳━┓┏━┳┫┣┓
┃┏┫┃┃╋┗┫┃┃┃━┫
┗━┻┻┻━━┻┻━┻┻┛
╋╋╋╋╋╋┏┳┓
┏┳┳━┳┳┫┃┃
┃┃┃╋┃┃┃┃┃
┣┓┣━┻━╋╋┫
┗━┛╋╋╋┗┻┛
@stephancom
stephancom / populate.rb
Created March 22, 2019 23:31
"populate" file header
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-. #
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / #
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' #
# .-------. ,-----. .-------. ___ _ .---. ____ ,---------. .-''-. #
# \ _(`)_ \ .' .-, '. \ _(`)_ \.' | | | | ,_| .' __ `.\ \ .'_ _ \ #
# | (_ o._)| / ,-.| \ _ \ | (_ o._)|| .' | |,-./ ) / ' \ \`--. ,---'/ ( ` ) ' #
# | (_,_) /; \ '_ / | :| (_,_) /.' '_ | |\ '_ '`) |___| / | | \ . (_ o _) | #
# | '-.-' | _`,/ \ _/ || '-.-' ' ( \.-.| > (_) ) _.-` | :_ _: | (_,_)___| #
# | | : ( '\_/ \ ;| | ' (`. _` /|( . .-' .' _ | (_I_) ' \ .---. #
# | | \ `"/ \ ) / | | | (_ (_) _) `-'`-'|___ | _( )_ | (_(=)_) \ `-' / #
class BlockQueue
def initialize(name)
@name = name
@blocks = []
end
def run_later(&block)
Progress.log "queueing job for #{@name}"
@blocks << block
end