Skip to content

Instantly share code, notes, and snippets.

View elskwid's full-sized avatar
🤡

Don Morrison elskwid

🤡
View GitHub Profile
@elskwid
elskwid / results.txt
Created August 14, 2014 22:52
Virtus speed comparison
> be ruby coerce-speed.rb
Rehearsal -------------------------------------------------------
Poro 0.020000 0.000000 0.020000 ( 0.025502)
MongoidDoc 0.460000 0.000000 0.460000 ( 0.464085)
VirtusModel 0.210000 0.010000 0.220000 ( 0.219621)
VirtusModelNoCoerce 0.060000 0.000000 0.060000 ( 0.062512)
---------------------------------------------- total: 0.760000sec
user system total real
Poro 0.010000 0.000000 0.010000 ( 0.004206)
@elskwid
elskwid / presenter.rb
Last active January 12, 2018 05:16
Simple Rails Presenter and View Presenter (so I don't forget)
# frozen_string_literal: true
module Foo
# Simple presenter module
#
# Uses Charlatan to create an `Foo::Presenter` module for use in the app.
# This isn't *strictly* a presenter - it's somewhere between a presenter,
# proxy, and/or decorator.
#
# Including `Foo::Presenter.new(:presented_thing)` in a class will
@elskwid
elskwid / domain_service.rb
Created February 11, 2016 00:27
Base service object
require "concord"
require "procto"
# Small wrapper module for domain services.
#
# Creates a service object with a ::call method that delegates to the instance
# method name given in the `call` option. Defaults to :call. (Procto)
#
# Provides initializer arguments, sets the ivars, and optional public reader
# methods for the attribute names passed in for `attrs`. (Concord)
# Example:
#
# class EditForm < FormModel
# model :a
#
# attribute :title, String
#
# validates :title, presence: true
# end
#
@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