Skip to content

Instantly share code, notes, and snippets.

View teaforthecat's full-sized avatar

Chris Thompson teaforthecat

View GitHub Profile
@teaforthecat
teaforthecat / PollStation.js.coffee
Created August 9, 2012 20:36
javascript timing mechanism to allow multiple machines to sync to the same time, within 50 milliseconds
class PollStation
start: => @runner = setInterval @oneSecond, 50
stop: => clearInterval(@runner)
runner: undefined
doOn: (num, func, args) =>
$(document).on 'poll:oneSecond', (e, second, epochSecond) ->
if (epochSecond % num) == 0
$(document.body).trigger("poll:#{num}Second")
func()
oneSecond: =>
@teaforthecat
teaforthecat / mongoid-models-summary.rb
Created September 23, 2012 03:04
Showing validation/error reporting problem with associated Mongoid models
class Artist
include Mongoid::Document
has_many :history_events, autosave: true, validate: true
end
class HistoryEvent
include Mongoid::Document
belongs_to :artist
validates_presence_of :headline
@teaforthecat
teaforthecat / tile processor
Created January 17, 2013 16:39
dragonfly custom processor to create pyramidal tiff images to be used by iipsrv
class TileProcessor
def tile temp_object,tile_size=256
geom = "#{tile_size}x#{tile_size}"
temf = Tempfile.open('o.ptif')
`convert #{temp_object.path} -define tiff:tile-geometry=#{geom} -compress jpeg 'ptif:#{temf.path}'`
temf
end
end
tiled_images.processor.register(TileProcessor)
@teaforthecat
teaforthecat / initializers_dragonfly.rb
Last active December 11, 2015 06:09
Store the processed image on the datastore before being served by dragonfly (pre-caching). The url needs to be accesses programatically, like in a background job.
# store the job before it has a chance to be served by dragonfly
images.configure do |c|
c.define_url do |app, job, opts|
thumb = ThumbTracker.where(job: job.serialize).first
unless thumb
uid = job.store
thumb = ThumbTracker.create!(
:uid => uid,
:job => job.serialize
)
@teaforthecat
teaforthecat / catalog_entry.rb
Last active December 13, 2015 17:08
Custom html parser. This was used to convert html files which were exported from an older Adobe InDesign file into markdown. The html was inconsistent and varied but this handled 90 percent of the files. The remaining cases were edited by hand in a form with a markdown enabled text area.
# renders markdown with footnotes matching Pandoc's syntax,
# but renders html with custom formatting in the class methods link_to_text_replace, link_to_fn_replace
class CatalogEntry
LINK_TO_FN = "[^%d] "
LINK_TO_TEXT = "[^%d]: %s"
CITATION = "%{last}, %{first}%{other}. \"%{title}.\" In _Bits & Pieces Put Together to Present a Semblance of a Whole: Walker Art Center Collections_, edited by Joan Rothfuss and Elizabeth Carpenter. Minneapolis, MN: Walker Art Center, 2005."
FILE_TEMPLATE = "B&Pcat-%{last},%{first}_2005"
FILE_NAME_REGEX = /B&Pcat-(.+),(.+)_2005.*/
# wraps third..+ paragraphs in div.more_content
# first two paragraphs are left in tack
def read_more_content html
first, second, *rest = Nokogiri::HTML::fragment(html).children
out = first.to_s + second.to_s
if rest.present?
out << "\n\n<div class=\"more_content\">"
out << rest.map(&:to_s).reduce(:+)
out << "</div>"
end
@teaforthecat
teaforthecat / double_nested_fields.html.haml
Created February 13, 2013 16:35
This is an example of a field set using the nested_form gem along with the twitter bootstrap style framework. It enables very efficient editing of data and relationships since three objects can be edited at once. Instead of being directed to another form and having to go back, ajax is used to create a dynamic form.
%li.reference_group
.container-fluid
.row-fluid
.span1.move
%i.icon-move
.span4
= f.input_field :label_markdown, placeholder: '<markdown enabled>'
= f.input :order, as: :hidden
.span7
= f.link_to_remove class: 'btn btn-danger' do
@teaforthecat
teaforthecat / ruby.el
Created March 21, 2013 01:33 — forked from fujin/ruby.el
(setq ruby-deep-indent-paren nil)
(defadvice ruby-indent-line (after unindent-closing-paren activate)
(let ((column (current-column))
indent offset)
(save-excursion
(back-to-indentation)
(let ((state (syntax-ppss)))
(setq offset (- column (current-column)))
(when (and (eq (char-after) ?\))
(not (zerop (car state))))
@teaforthecat
teaforthecat / common.yaml
Created December 1, 2013 00:00
puppet-hiera hash data access fails why?
nfs_volumes:
home: "%{gdnet_env}-netapp1:/vol/home/qtree0"
yum_cache: "%{gdnet_env}-netapp1:/vol/yum_cache/qtree0"
techops: "%{gdnet_env}-netapp1:/vol/techops/qtree0"
websites: "%{gdnet_env}-netapp1:/vol/websites_%{gdnet_env}/qtree0"
apache_logs: "%{gdnet_env}-netapp1:/vol/apache_logs_%{gdnet_env}/qtree0"
evolution_logs: "%{gdnet_env}-netapp1:/vol/evolution_logs_%{gdnet_env}/qtree0"
@teaforthecat
teaforthecat / project.clj
Created May 7, 2014 02:53
offending lein project file with dependencies configured to trigger a bug in clojurescript compilation. "NoSuchMethodError: com.google.common.io.ByteStreams.limit"
(defproject intrepid-life-coffee "0.1.0-SNAPSHOT"
:description "website to advertise the coffee shop"
:url "www.intrepidlifecoffee.com"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[compojure "1.1.6"]
[ring "1.2.1"]
[hiccup "1.0.5"]
;; [org.slf4j/log4j-over-slf4j "1.6.6"] ;;avoids dep loop