Skip to content

Instantly share code, notes, and snippets.

View elskwid's full-sized avatar
🤡

Don Morrison elskwid

🤡
View GitHub Profile
@elskwid
elskwid / block_yield.rb
Last active January 2, 2016 19:09
Difference with capturing the block, yield with an argument, and yield
class WithBlockVar
attr_accessor :z
def initialize(x, &blk)
@x = x
instance_eval(&blk) if block_given?
end
end
# Using block variable
a = WithBlockVar.new("A") do
@elskwid
elskwid / service.rb
Created December 3, 2013 19:46
Service Object WIP
# An attempt to codify how we're using Service Objects now
#
# Most follow this pattern:
#
# class SomeService
#
# def self.call(a, b, &block)
# new(a, b).call(&block)
# end
#
@elskwid
elskwid / virtus_module.rb
Created November 25, 2013 16:34
Using a module instead of a class with Virtus
module Entity
include Virtus.module
attribute :id, Integer
def persisted?
!id.nil?
end
end
@elskwid
elskwid / some_types.rb
Created October 23, 2013 06:24
Another enum-ish example
class SomeTypes
@all = [
["TypeA", "Type A"],
["Type B", "Type B"],
]
Type = Struct.new(:type, :name, :slug, :sym) do
alias_method :to_ary, :to_a
# allows Type to be used for ifnone option in .find
@elskwid
elskwid / washoe_eats.rb
Created September 12, 2013 03:19
Old rough sketch to scrape data from eats.washoecounty.us - still has the SraperWiki code in it. (and did I mention it's old?)
require "mechanize"
# we need some methods to clean this up so make it a class
class InspectionScraper
BASE_URL = "http://eats.washoecounty.us/"
FACILITY_COLS = ["link", "name", "score", "facility_type", "address", "inspection_date"]
RESULTS_TABLE_ID = "table#ctl00_ContentPlaceHolder1_grdHealth"
@elskwid
elskwid / virtus_value_objects.rb
Created September 10, 2013 06:29
Potential fix for Virtus::ValueObjects inheritance issue
require "virtus"
class Teacher
include Virtus::ValueObject
attribute :id, String
def self.inherited(descendant)
super
# Example:
#
# class EditForm < FormModel
# model :a
#
# attribute :title, String
#
# validates :title, presence: true
# end
#
@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