Skip to content

Instantly share code, notes, and snippets.

View adam-e-trepanier's full-sized avatar

Adam Trepanier adam-e-trepanier

View GitHub Profile
@adam-e-trepanier
adam-e-trepanier / spec_helper.rb
Created July 14, 2011 19:00
Spork reload factories
Spork.each_run do
# This code will be run each time you run your specs.
AppName::Application.reload_routes!
Factory.factories.clear
Dir.glob("#{::Rails.root}/spec/factories/*.rb").each { |file| load "#{file}" }
end
@adam-e-trepanier
adam-e-trepanier / singleton_class.rb
Created July 15, 2011 03:18
Singleton (eigen) class
module Kernel
# Returns the object's singleton class.
def singleton_class
class << self
self
end
end unless respond_to?(:singleton_class) # exists in 1.9.2
# class_eval on an object acts like singleton_class.class_eval.
def class_eval(*args, &block)
# controller code for /foo/1
before_filter do |controller|
controller.some_verification(params[:id])
end
# controller code for /foo/1/bar/1
before_filter do |controller|
controller.some_verification(params[:foo_id])
@adam-e-trepanier
adam-e-trepanier / spec_helper.rb
Created December 15, 2011 20:30
json helper in spec_helper
def set_json_headers
request.env['HTTP_ACCEPT'] = 'application/json, text/javascript, *'
request.env['CONTENT_TYPE'] = 'application/json'
end
@adam-e-trepanier
adam-e-trepanier / feature-scenario
Created September 27, 2013 03:44
rspec feature
require 'spec_helper'
feature "Credential idempotency" do
# He shouldn't be describing it anyway, but you
# could write the nice little stuff here
scenario "Editing authorization information of a credential increments the version" do
prev_version = obj.credential_version
obj.modify_encrypted_data # A real Domain model or moved to a page object
-module(math_functions).
-export([test/0, even/1, odd/1, filter/2, split/1, split2/1]).
test() ->
true = even(10),
false = even(11),
true = odd(11),
false = odd(10),
tests_passed.
(def data
{:name "Adam Trepanier"
:date-of-birth "1981-04-30"
:gender "male"
:patient-key "test-adam"
:status "active"
:phone "555-555-5555"
:shipping-address {:street "123 Foo Bar Way"
:city "Somwhere"
:state "CA"
@adam-e-trepanier
adam-e-trepanier / gist:47866b5d0a8045d87561
Created October 20, 2014 21:48
Destructuring with :key directive
;; Destructor a map with another map -> patient has a shipping address
;; We use the :keys directive to to assign the shipping address info inside of data
;; Notice we also use the :keys directive to also get the name and date of birth
(let [{:keys [name date-of-birth orders]
{:keys [street city state postal-code]} :shipping-address
:as patient} data]
(println name \- date-of-birth)
(println street ", " city ", " state " " postal-code)
(println orders)
(println "------- Full Patient Deatils -----")
@adam-e-trepanier
adam-e-trepanier / gist:cac11fa98ac4d1422735
Last active August 29, 2015 14:07
Restructuring with prefix
;; In Clojure 1.6 we can now prefix the name of a key if its the same
;; A litte more context with the prefix, but still very clean
(let [{:keys [name date-of-birth orders
shipping-address/street shipping-address/city
shipping-address/state shipping-address/postal-code]
:as patient} data]
(println name \- date-of-birth)
(println street ", " city ", " state " " postal-code)
(println orders)
(println "------- Full Patient Deatils -----")
@adam-e-trepanier
adam-e-trepanier / gist:2383227e043d93c68923
Created October 20, 2014 21:55
Restructuring with auto resolve
;; In Clojure 1.6 we can now also auto resolve keyword forms in the :keys directive
;; I think you lose some context with auto-resolve, but its the shortest code
(let [{:keys [name date-of-birth orders
::street ::city ::state ::postal-code]
:as patient} data]
(println name \- date-of-birth)
(println street ", " city ", " state " " postal-code)
(println orders)
(println "------- Full Patient Deatils -----")
(println patient))