Skip to content

Instantly share code, notes, and snippets.

View reggieb's full-sized avatar

Rob Nichols reggieb

View GitHub Profile
@reggieb
reggieb / version_struct.rb
Last active August 29, 2015 14:08
Paper Trail: restoring associated objects with main object.
class VersionStruct
class << self
def build_from(version, args = {})
object = deserialize(version.object)
args[:changes] = deserialize(version.object_changes)
args[:version] = version
version_struct = new(object, args)
version_struct.mimic
end
@reggieb
reggieb / number_test.rb
Created November 21, 2014 10:16
Reusing minitest tests via their method names
class NumberTest < ActiveSupport::TestCase
def setup
@number = 4
end
def test_number_is_four
assert_equal 4, @number
end
@reggieb
reggieb / work_request.rb
Last active August 29, 2015 14:13
Rollback persisted finite machine: An example
class WorkRequest < ActiveRecord::Base
# Attributes include :state_history (text) and :state (string)
serialize :state_history, Array
after_find :restore_process
after_initialize :restore_process
def rollback_state_to_previous
@reggieb
reggieb / class_finder.rb
Last active August 29, 2015 14:27
Used in combination with FedoraMigrate to idenitfy target Model Class if not obvious from Object Content Model. https://github.com/projecthydra-labs/fedora-migrate
class ClassFinder
attr_reader :pid
def initialize(pid)
@pid = pid
end
def object
@object ||= self.class.source_objects.select{|x| x.pid =~ /#{pid}/}.first
end
@reggieb
reggieb / apache.god
Created October 9, 2015 14:03
God script to keep apache alive
APACHE_ROOT = 'path/to/apache'
def apache(location)
File.join APACHE_ROOT, location
end
def apachectl(command)
"#{apache 'bin/apachectl'} -k #{command}"
end
@reggieb
reggieb / parent_child.rb
Last active June 29, 2016 10:07
How to define method to be over-ridden in child classes
class Parent
def self.to_be_over_ridden
raise "Class method `#{__method__}` must be defined in #{name}"
end
def to_be_over_ridden
raise "Instance method `#{__method__}` must be defined in #{self.class.name}"
end
end
@reggieb
reggieb / use_uuid.rb
Last active January 3, 2021 20:55
UUID concern for rails models - To allow rails paths to be of the form `foo/63950bdb-6c3b-4744-ad6f-f5df5aed1a76` rather than `foo/1`
require 'active_support/concern'
module UseUuid
extend ActiveSupport::Concern
included do
before_save :generate_uuid
end
def to_param
uuid
# Resistence settings for cartidge loads
settings = [1.1, 33.0, 100.0, 1000.0]
# Combination of resistors switched in via panel under dino
# Equation is: 1/Rtotal = 1/R1 + 1/R2 ... + 1/Rn
(1..4).collect do |i|
settings.combination(i).each do |combination|
label = combination.inspect
fractions = combination.collect{|c| 1/c}
@reggieb
reggieb / contents_of.rb
Created September 20, 2016 12:01
A module to simplify grabbing the contents from files in rails apps
module ContentsOf
def contents_of(file_path, location: Rails.root)
File.read File.expand_path(file_path, location)
end
end
class Address
attr_accessor :address, :flat, :house_number, :house_name, :street, :street2, :street3, :town, :county
def initialize(address)
self.address = address
end
def parts
@parts ||= address.split(/\s?\,\s?/).collect(&:strip)
end