Skip to content

Instantly share code, notes, and snippets.

View mfpiccolo's full-sized avatar

Mike Piccolo mfpiccolo

View GitHub Profile
require "net/ftp"
require "miro"
class HexBuilder
attr_accessor :url_or_path, :local
def self.call(url_or_path, opts = {})
new(url_or_path, opts).call
end
@mfpiccolo
mfpiccolo / to_struct_example.rb
Last active August 29, 2015 14:01
Gives an example of the use case of to struct and benchmarks against OpenStruct.
require "json"
require 'benchmark'
require 'bigdecimal/math'
class Hash
def to_struct
Struct.new(*(k = self.keys)).new(*self.values_at(*k))
end
end
require "json"
require 'benchmark'
require 'bigdecimal/math'
class Hash
def to_struct
k = self.keys
klass = k.map(&:to_s).sort_by {|word| word.downcase}.join.capitalize
begin
Kernel.const_get("Struct::" + klass).new(*self.values_at(*k))
# What I want to do is take a string like "some_attribute <= 5" and be able to parse that into SQL
# The string will only ever have one operation so I need to check for the inclusion of any valid opperator
# and build a SQL string based on that.
# initial conditional method
def build_sql_string
@final_sql_array = sql_array.map(&:strip).map do |string|
if string.include?(":")
q = string.split(":")
"(similarity((data->>'#{q[0].strip}')::text, '#{q[1].strip}') > .5 OR " +
class App.Logger
@add_error: (error_object) ->
@error ||= []
@error.push(error_object)
jQueryInit = $.fn.init
$.fn.init = (selector, context) ->
element = new jQueryInit(selector, context)
if selector and element.length is 0
# must include stacktrace library
@mfpiccolo
mfpiccolo / serializer_monkey.rb
Last active August 29, 2015 14:13
A active record monkey patch to make serialization easy with active model serializers
# Monkey patch for ActiveRecord::Base to return serialized JSON in the format
# of an ActiveModel::Serializer.
module SerializedJson
extend ActiveSupport::Concern
def to_serial(serializer=nil, opts={})
if self.respond_to?(:map)
serializer ||= (self.base_class.name + "Serializer").constantize
serialized_collection = self.map do |resource|
@mfpiccolo
mfpiccolo / money-rails-four-decimal.rb
Created March 31, 2015 22:30
Money Rails with 4 decimal places
MoneyRails.configure do |config|
config.register_currency = {
:priority => 1,
:iso_code => "USD1",
:name => "USD with subunit of 4 digits",
:symbol => "$",
:symbol_first => true,
:subunit => "Subcent",
:subunit_to_unit => 10000,
:thousands_separator => ",",
import Ember from 'ember';
var Pollable = Ember.Mixin.create({
afterModel: function (model, transition) {
var self = this;
this._super(model, transition);
var interval_info = this.get('interval_info');
if (!interval_info) {
interval_info = Ember.Object.create({
guard "minitest", all_on_start: false, start_on_start: false do
# with Minitest::Spec
watch(%r|^test/(.*)_test\.rb$|)
watch(%r|^app/(.*)/(.*)\.rb$|) { |m| "test/#{m[1]}/#{m[2]}_test.rb" }
watch(%r|^lib/(.*)([^/]+)\.rb$|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
watch(%r|^test/test_helper\.rb$|) { "test" }
watch(%r|^test/support/|) { "test" }
end
#minitest/spec
# Drop SOLO in the name of the test you want to run and then run
# solo and tab complete the path/to/the/test/file.rb
solo() {
ruby -Itest $1 --name /SOLO/
}
# describe SomeClass do
# it "does some stuff SOLO" do