Skip to content

Instantly share code, notes, and snippets.

View adz's full-sized avatar

Adam Davies adz

  • Veridapt
  • Adelaide, Australia
  • 03:34 (UTC +09:30)
  • X @adzdavies28
View GitHub Profile
#!/usr/bin/env ruby
command = '/usr/bin/passenger-memory-stats'
memory_limit = 200 # megabytes
def running?(pid)
begin
return Process.getpgid(pid) != -1
rescue Errno::ESRCH
return false
@adz
adz / library.rb
Created June 3, 2011 03:21 — forked from captainpete/library.rb
Tom's coding challenge. MiniTest suite
module Library
def self.leaf_paths_of(paths)
paths.select{|path|
!paths.any?{|other|
other.start_with?(path + '/')
}
end
end
guard 'annotate' do
watch( 'db/schema.rb' )
# Uncomment the following line if you also want to run annotate anytime
# a model file changes
#watch( 'app/models/**/*.rb' )
end
@adz
adz / gist:5105384
Last active December 14, 2015 15:08
require 'net/http' # CODE FROM: https://github.com/mwmitchell/rsolr/blob/master/lib/rsolr/connection.rb
require 'net/https'
# The default/Net::Http adapter for RSolr.
class RSolr::Connection
# using the request_context hash,
# send a request,
# then return the standard rsolr response hash {:status, :body, :headers}
def execute client, request_context
GIT
remote: git://github.com/AlessandroBerardi/activerecord_null_object.git
revision: b042c43b8cafb753300a13f2aaeeeaf6c79558f2
branch: rails_3_2
specs:
activerecord_null_object (0.3.0)
activerecord (~> 3.2.11)
activesupport (~> 3.2.11)
GIT
source 'https://rubygems.org'
gem 'ruleby', github: 'Codalytics/ruleby'
gem 'q_rules', path: 'q_rules'
#####################################
# Core Infrastructure
#####################################
gem 'rails', '~> 3.2.12'
gem 'json', '>= 1.7.7' # Required to fix Denial of Service and Unsafe Object Creation Vulnerability in JSON [CVE-2013-0269]
@adz
adz / example.rb
Last active January 1, 2016 23:19 — forked from joakimk/example.rb
class Filter
include FormObject
# Proc because Date.yesterday changes every day :)
attribute :from, Date, default: Proc.new { Date.yesterday }
attribute :to, Date, default: Proc.new { 1.month.from_now - 1.day }
end
# in controller
@filter = Filter.new(params[:filter])
@adz
adz / null_object_example.rb
Created October 6, 2014 14:06
Simple null object
class Client < ActiveRecord::Base
belongs_to :pricing_level
end
class PricingLevel
def self.find_default
#...
end
end
@adz
adz / hash.rb
Created October 6, 2014 14:26
Simple null object
class CoursesController < ApplicationController
expose(:course, attributes: :course_params)
expose(:current_course) { current_user.courses.find_by_id(params[:id] || params[:course_id]) }
expose(:first_of_each_month_of_course) { (current_course.start_date.change(day: 1)..current_course.end_date).select{ |d| d.day == 1 } }
# ...
end
# Orginal code gives:
# line 4 of [PROJECT_ROOT]/app/controllers/courses_controller.rb: block in <class:CoursesController>
@adz
adz / hash.rb
Created October 6, 2014 14:28
Simple null object
the next common error is likely from side effects: "How did the value of this variable change?!"
immutability...