Skip to content

Instantly share code, notes, and snippets.

View saturnflyer's full-sized avatar
😀

Jim Gay saturnflyer

😀
View GitHub Profile

Keybase proof

I hereby claim:

  • I am saturnflyer on github.
  • I am saturnflyer (https://keybase.io/saturnflyer) on keybase.
  • I have a public key ASDwc6NFkREQr6HNVxVL2w4gI0IykEnAZvZiIDU1yGL3ywo

To claim this, I am signing this object:

@saturnflyer
saturnflyer / the_world_of_foos.rb
Last active July 16, 2019 16:49 — forked from zdennis/the_world_of_foos.rb
Given the below hierarchy how many ways can you think of to wrap the `Parent#foo` implementation so that `Grandchild1` and `Grandchild2` can have their own unique logic that executes after all of the mixin `foo`(s) but before the `Parent#foo` executes?
module N
def foo
puts "N: foo"
super
end
end
module O
def foo
puts "O: foo"
@saturnflyer
saturnflyer / json-generator.rb
Last active May 24, 2017 20:49
Using casting to apply different behaviors to an object for generating JSON
require 'json'
require 'casting'
module Names
def names
"names here"
end
end
module Secrets
@saturnflyer
saturnflyer / prepended.rb
Last active May 11, 2017 12:54 — forked from cmar/prepended.rb
example of prepended class methods
class Foo
def self.say
p "hello from Foo"
end
end
module Bar
def say
super
p "hello from Bar"
module Foo1
def self.bar
"Yolo"
end
end
"Foo1.bar => #{Foo1.bar}" # => "Foo1.bar => Yolo"
module Foo2
def bar # !> previous definition of bar was here
"Yolo"
@saturnflyer
saturnflyer / namespace.rb
Last active January 13, 2016 21:58
ability to create classes and modules in a namespace
module Namespace
class Manager
def initialize(namespace)
@managed = namespace
end
def create_module(name, &block)
mod = Module.new(&block)
@managed.const_set(name, mod)
mod
# This is designed to be used in cell classes but will work with any class
# which has a 'model' method.
#
# Example:
#
# class UserCell < Cell::ViewModel
# extend ModelFeature
#
# model_feature :name
# end
class Something
def self.protector(name, &block)
begin
call_mod = self.const_get(:ExternalSystemCalls, false)
rescue NameError
call_mod = Module.new
const_set(:ExternalSystemCalls, call_mod)
include call_mod
end
begin
<div rel="warning-record">
<p>Warning text... <span rel="which-field">1</span> of 3</p>
</div>
require 'resolv'
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if Resolv::DNS.new.getresources(value.split("@").last, Resolv::DNS::Resource::IN::MX).empty?
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
rescue Resolv::ResolvError, Resolv::ResolvTimeout
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end