Skip to content

Instantly share code, notes, and snippets.

@jopotts
jopotts / default_values.rb
Last active August 13, 2022 16:26
Simple default values on creation of ActiveRecord models
module DefaultValues
def has_default_values(default_values = {})
class_attribute :default_values
self.default_values = default_values
after_initialize :assign_default_values
include InstanceMethods
@jopotts
jopotts / gist:deb35281c6280880272c4b8fff52a3dd
Last active February 23, 2021 09:44
Rails timezone mappings
International Date Line West => Etc/GMT+12
Midway Island => Pacific/Midway
American Samoa => Pacific/Pago_Pago
Hawaii => Pacific/Honolulu
Alaska => America/Juneau
Pacific Time (US & Canada) => America/Los_Angeles
Tijuana => America/Tijuana
Mountain Time (US & Canada) => America/Denver
Arizona => America/Phoenix
Chihuahua => America/Chihuahua
module RandomString
HUMAN = %w{2 3 4 6 7 9 a c d e f g h j k m n p q r t v w x y}
LOWER = [*'a'..'z']
PARAM = [*'a'..'z', *0..9]
FULL = [*'a'..'z', *'A'..'Z', *0..9]
MIXED = [*'a'..'z', *'A'..'Z']
def self.by_rand(length = 8, set = :full)
chars = get_chars(set)
@jopotts
jopotts / lookups.rb
Last active December 26, 2015 10:39
A module for the definition of lookup lists
require "active_support/inflector"
module Lookups
# Allows the definition of lookup values
def define_lookup(const_name, lookup_codes)
mod = Module.new do
extend self
@jopotts
jopotts / set_partitioner.rb
Created June 24, 2014 13:40
Find combinations of sets built from a range that add to a given value
class SetPartitioner
def initialize(min, max, total)
@min = min
@max = max
@total = total
end
def any?
sets.present?

Keybase proof

I hereby claim:

  • I am jopotts on github.
  • I am jopotts (https://keybase.io/jopotts) on keybase.
  • I have a public key whose fingerprint is DEAA 7CA2 5BEE FBB7 B3B5 F9FE 4498 EE2E D41F 7DB6

To claim this, I am signing this object: