Skip to content

Instantly share code, notes, and snippets.

View elskwid's full-sized avatar
🤡

Don Morrison elskwid

🤡
View GitHub Profile
@elskwid
elskwid / cascade.rb
Created August 24, 2013 20:10
Saw someone wishing for method cascading in Ruby and wanted to play around with the idea ...
# define
class Object
def cascade(method_name, *args, &block)
self.send(method_name, *args, &block)
self
end
# catch the cascade
def method_missing(method_name, *args, &block)
sig = /^__/
@elskwid
elskwid / enuma.rb
Last active December 18, 2015 16:29
module Enuma
def enumerated(type_name, *members, &block)
@enums = []
type_class = const_set(type_name, Enuma::Type.new(*members))
instance_eval(&block)
finalize(@enums, type_class)
end
@elskwid
elskwid / lambda_ivars.rb
Created June 5, 2013 07:05
Fancy ivar subscript syntax
class A
def initialize
@lambda = ->(x) { puts "lambda: #{x}" }
@proc = Proc.new { |x| puts "proc: #{x}" }
@method = method(:_method)
end
def lambda(x)
@lambda[x]
end
@elskwid
elskwid / struct_and_find.rb
Created May 24, 2013 21:54
Playing with structs and Enumerable.find
class SampleContracts
BASE_PATH = "downloads/sample_contracts/"
Contract = Struct.new(:type, :name, :title) do
# Struct responds to .call so it can be used for Enumerable.find(ifnone)
def self.call
new
end
# By adding this method we get a struct that plays nicely with destructuring
@elskwid
elskwid / modules.rb
Created May 21, 2013 22:32
Playing with modules and bindings
class Configuration
attr_reader :name
def initialize(name)
@name = name
end
end
class ModuleBuilder
@elskwid
elskwid / protomodel.rb
Created May 8, 2013 04:46
Protomodel
# Example of prototyping models with a mix of ActiveRecord and Virtus
#
# Useful with large models where some fields are in flux during spiking/dev
# and you don't want or need to run migrations to try ideas out.
class A < ActiveRecord::Base
# ...
# belongs_to
# has_many
@elskwid
elskwid / event.rb
Created February 23, 2013 20:35
Enumerator fun
require "active_support/all"
class Event
STATES = %w(active archived)
def self.types(&block)
return to_enum(__callee__) unless block
types = {
@elskwid
elskwid / queue.rb
Created February 16, 2013 01:24
Small example of Queue
require 'thread'
q = Queue.new
(0..1000).each{ |i| q << i }
puts q.length
Thread.new do
sleep 1
puts "FIRST #{q.pop}"
end
@elskwid
elskwid / capistrano.rb
Created January 24, 2013 07:02
CAPIFY
# $: << File.expand_path("../../recipes", __FILE__)
# Don't worry about the load path man, Capistrano's got your back!
module Capistrano
if const_defined? :Configuration
Configuration.instance(true).load do
@load_paths << File.expand_path("../recipes", __FILE__)
load "log"
end
end
# encoding: utf-8
class DocumentUploader < CarrierWave::Uploader::Base
include ::CarrierWave::Backgrounder::Delay
include CarrierWave::MimeTypes
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::UNOConv