Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mdchaney's full-sized avatar

Michael Chaney mdchaney

View GitHub Profile
@mdchaney
mdchaney / name_split.rb
Created March 5, 2024 19:21
Split name into first/last name
# words that indicate last name
#
# da
# de
# de la
# del
# di
# dos
# la
# van der
@mdchaney
mdchaney / audio_time.rb
Last active January 10, 2024 00:13
Original audio_time.rb
module AudioTime
# AIFF stores the samples per second as an IEEE 80-bit float, big-endian.
# This code will convert it to a 32-bit integer. This is not generalized
# code for this operation as it ignores the sign bit, part of the mantissa,
# etc.
def AudioTime.ieee80_to_long(raw_number)
discard, mantissa, hidden, exponent, sign = raw_number.reverse.unpack('b80').first.unpack('a32 a31 a1 a15 a1')
# Get the real exponent - the exponent is really exponent + 1
exponent = [exponent].pack('b15').unpack('v').first - 16382
# Now, use the exponent to pull the proper bits from the mantissa.
@mdchaney
mdchaney / _line_item.html.erb
Created September 28, 2023 04:02
Stimulus-based subform
<!--
Note that when showing this line if the object is marked_for_destruction? it
should be hidden.
-->
<tr class="line_item subform fields" id="line_item_<%= f.object.id %>">
<td>
<%= f.text_field :field_1, required: true %>
</td>
<td>
<%= f.text_field :field_2, required: true %>
@mdchaney
mdchaney / card_utilities.js
Created July 28, 2022 02:31
Modernization of Stripe's jquery.payment - almost drop-in replacement with no jQuery
// Based on Stripe's jquery.payment, offers a simple set of utilities
// for handling credit card numbers, expirations, and CVC/CVV2 codes
//
// cc_format_card_number(field)
// cc_format_card_expiry(field)
// cc_format_card_cvc(field)
//
// "field" may be either the actual DOM element reference or a string
// with the ID of the DOM element. This will add event handlers to
// the field to handle input.
@mdchaney
mdchaney / README
Last active February 6, 2024 22:20
Fix string encoding in Ruby
This function takes a string that is in Latin-1 (ISO-8859-1), UTF-8, or plain ASCII and returns the string properly encoded as UTF-8. In addition, any Microsoft smart quotes (stupid quotes) are replaced with plain ASCII equivalents. This is useful for reading CSV files or other text files from a web upload and getting to a "known good" state.
@mdchaney
mdchaney / README
Last active April 10, 2020 16:18
Bootstrap 4 multi-select replacement
This code will replace <select multiple...> boxes with a readonly text input that shows all items selected. When clicked, a modal will appear giving the user the ability to select items with check boxes. In the page load function, just call setup_selection_popups(). The select elements should have a class of "replaceable", and of course the multiple attribute must be present. Everything is contained in one javascript file, and a css file. Bootstrap 4 uses jquery, so it must be present. However, jquery is used only to show the modal.
@mdchaney
mdchaney / recursive_delete.rb
Created October 1, 2018 13:08
Recursively remove data from tables in PGSQL, considering foreign keys
#!/usr/bin/env ruby
@verbose = true
tables_to_clear = ARGV.dup.freeze
# Alternately, clear out everything but these tables
#UNCLEARED_TABLES=%w{ ar_internal_metadata schema_migrations }
#tables_to_clear = (ActiveRecord::Base.connection.tables.sort - UNCLEARED_TABLES).freeze
@mdchaney
mdchaney / _line_item.html.erb
Created May 17, 2017 22:34
Help for dynamic subforms in Rails
<!--
In this file, the fields can be set to "required", but that will
cause issues. Specifically, when someone clicks "Remove" the fields
will be hidden and unless the "required" prop is set to false the form
will not submit. Also note that when showing this line if the object
is marked_for_destruction? it should be hidden. The javascript below
will handle both these issues.
-->
<tr class="line_item subform fields" id="line_item_<%= f.object.id %>">
<td>
@mdchaney
mdchaney / parse_id3v23.rb
Created April 18, 2017 20:46
Pure Ruby parser for ID3 v2.3 tags
#!/usr/bin/env ruby
# By Michael Chaney, mdchaney@michaelchaney.com
# Copyright 2017, Michael Chaney Consulting Corporation, All Rights Reserved
#
# Released under GPL v2 License or MIT License
#
# Simple parser for ID3 v2.3 tags in MP3 files. Provide the filename
# on the command line.
#!/usr/bin/env python
# Demonstration of using Max7219 chip with raspberry pi
# and Python. Program will count from 0-999 on first three digits
# and 999-0 on second three digits. Includes code to setup,
# handle clock & latch, and send 16 bits at a time.
import RPi.GPIO as GPIO
import time