Skip to content

Instantly share code, notes, and snippets.

View jmeirow's full-sized avatar

Joe Meirow jmeirow

View GitHub Profile
@jmeirow
jmeirow / dump_models_to_files.rb
Created March 15, 2012 10:12
Dump the data of ActiveRecord::Base based models to .yml files in the Rails db directory.
require 'active_record'
#
# When run from the root directory of a Rails project, this code will dump the data
# of all models inheriting from ActiveRecord. The data is dumped to .yml files in the
# db directory of the Rails app.
#
# This code is not very fast, as it starts up Rake for each model. It could use improvement.
#
# ------ Dependency on ar_fixtures -------
@jmeirow
jmeirow / gist:2857556
Created June 2, 2012 09:51
Loading a class from Rails lib directory when using "rails console"
pry(main)> load "#{Rails.root}/lib/yourfile.rb"
Thanks to "NulRef"
This info from http://stackoverflow.com/questions/6361401/can-rails-console-reload-modules-under-lib
def current_club
if session[:user_id].nil?
@club = Club.find(1)
else
Club.find(current_member.club_id)
end
end
@jmeirow
jmeirow / gist:3709299
Created September 12, 2012 19:29
Text parsing and mapping in Ruby
# the big picture... what data we're getting from where and its destination
@data_binding_hash = {
:@pSMSR_GRGR_ID=> [ :GRGR_ID, 'facets_field', DataType.STRING, DataSource.FACETS, '@pSMSR_GRGR_ID=""' , nil ],
:@pSMSR_SBSB_ID=> [ :SBSB_ID, 'facets_field', DataType.STRING, DataSource.FACETS, '@pSMSR_SBSB_ID=""' , nil ],
:@pSMSR_REL=> [ :patient_rel_code, 'rel_code', DataType.STRING, DataSource.LOOKUP, '@pSMSR_REL=""' , nil ],
:@pSMSR_SSN=> [ :MEME_SSN, 'facets_field', DataType.STRING, DataSource.FACETS, '@pSMSR_SSN=""' , nil ],
:@pSMSR_LNAME=> [ :SBSB_LAST_NAME, 'facets_field', DataType.STRING, DataSource.FACETS, '@pSMSR_LNAME=""' , nil ],
:@pSMSR_FNAME=> [ :SBSB_FIRST_NAME, 'facets_field',
@jmeirow
jmeirow / gist:3709453
Created September 12, 2012 19:54
Ruby-style enums
class DataType
def self.STRING
:STRING
end
def self.DATE
:DATE
end
@jmeirow
jmeirow / time_parser.rb
Created November 2, 2012 00:11
TimeParser
class TimeParser
attr_reader :hour, :minute, :am_pm
def initialize time
matched = /(1[012]|^[1-9]):[0-5][0-5] (am|pm|AM|PM)/.match(time.lstrip.squeeze(' '))
if matched
@valid = true
parts = matched[0].split(/[\s:]/)
@hour = parts[0].to_i
@jmeirow
jmeirow / to_range.rb
Created November 26, 2012 12:00
create array of ranges from array of individual values
require 'pp'
require 'date'
def to_range low, high, input, output
if high > input.length
return output.each_slice(2).map {|a| a }.map { |x| x[0]..x[1]}
end
if low == 0
@jmeirow
jmeirow / to_range.rb
Created November 27, 2012 09:35 — forked from darrencauthon/to_range.rb
create array of ranges from array of individual values
class Array
def to_range
highs = self.select { |x| !self.include?(x+1) }.sort_by { |x| x }
lows = self.select { |x| !self.include?(x-1) }.sort_by { |x| x }
lows.zip(highs).map { |a,b| a..b }
end
end
# Running tests:
@jmeirow
jmeirow / to_range.rb
Created November 27, 2012 10:36
Alternate Version of #to_range
class Array
def to_range
self.sort!
highs = self.select { |x| !self.include?(x+1) }
lows = self.select { |x| !self.include?(x-1) }
lows.zip(highs).map { |a,b| a..b }
end
end
@jmeirow
jmeirow / startpg.txt
Created November 29, 2012 01:04
start postgres on OS X
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start