Skip to content

Instantly share code, notes, and snippets.

View adamstrickland's full-sized avatar

Adam Strickland adamstrickland

  • New York, NY
View GitHub Profile
class Audit
include Mongoid::Document
include Mongoid::Timestamps
include RailsAdminImport::Import
class << self
def create_with_type_message_meta(_type, _message, _user)
self.create_without_type_message_meta({
:type => _type,
:content => _message,
@adamstrickland
adamstrickland / widget_context.rb
Last active December 15, 2015 11:49
A shared context for RSpec'ing Apotomo Widgets with Devise mixed in
shared_context "widget" do
let(:widget_klass) { self.described_class }
let(:widget_name) { widget_klass.to_s.underscore.gsub(/_widget/, '') }
let(:widget_instance) { root.find_widget(widget_name) }
let(:current_user) { create(:user) }
before {
self.class.has_widgets do |r|
r << widget(widget_name)
end
@adamstrickland
adamstrickland / calling_out.js
Last active December 17, 2015 08:18
jquery callback
MyObject = {
respondToCallback: function (response, context) {
// context will === "abc123" when called via doingSomething()
},
doingSomething: function(callback) {
var _foo = "abc123";
var _self = this;
var _default_callback = function (resp, status, jqxhr) {
_self.respondToCallback(response, _foo);
};
ng_products = Product.where(:"commodity.name" => /ng/i).each do |p|
p.expiry_rule = :preceeding_weekday
end
class AutomaticExpirationDate < Molecule::Mechanics::Repercussion
extend Molecule::ExchangeConcepts
def act(subj, *args)
subj.pricing.expiry = self.class.expires_day_before(subj.tenor_start) if subj.option?
# Molecule::ExchangeConcepts.expires_day_before is .get_expiry *without* the call to .calculate_expiry
end
end
class Product
field :traits, type: Array, default: []
after_initialize :load_traits!
on_save :validates_methods
def load_traits!
self.traits.each do |trait|
self << class
self.send(:include, trait.camelize.constantize)
module Mongoid
class << self
def Money(value)
Money.create_from_value(value)
end
end
class Money
class << self
def create_from_value(value)
@adamstrickland
adamstrickland / festivity.txt
Created September 29, 2016 15:12
Festivity
_________________________________________
/ Festivity Level 1: Your guests are \
| chatting amiably with each other, |
| admiring your Christmas-tree ornaments, |
| singing carols around the upright |
| piano, sipping at their drinks and |
| nibbling hors d'oeuvres. |
| |
| Festivity Level 2: Your guests are |
| talking loudly -- sometimes to each |
@adamstrickland
adamstrickland / condbm.rb
Last active June 1, 2018 16:09
Benchmark for various ways of doing `if condition1 || condition2` (needs `benchmark-ips` installed)
require "benchmark"
require "benchmark/ips"
thing = :baz
puts "2 conditions, neither is true"
Benchmark.ips do |x|
x.report("conditional") do
if thing == :foo || thing == :bar
true
@adamstrickland
adamstrickland / hash_transform.out
Last active March 7, 2019 20:09
benchmarking some hash transformation techniques
Rehearsal -------------------------------------------------------
{}.tap 0.000382 0.000257 0.000639 ( 0.000871)
[[]].to_h 0.000031 0.000001 0.000032 ( 0.000032)
{}.each_with_object 0.000027 0.000001 0.000028 ( 0.000028)
---------------------------------------------- total: 0.000699sec
user system total real
{}.tap 0.000030 0.000001 0.000031 ( 0.000029)
[[]].to_h 0.000033 0.000011 0.000044 ( 0.000033)
{}.each_with_object 0.000023 0.000012 0.000035 ( 0.000032)