Skip to content

Instantly share code, notes, and snippets.

View saturnflyer's full-sized avatar
😀

Jim Gay saturnflyer

😀
View GitHub Profile
mod = Module.new
mod.module_eval %{
def fill_out_#{name}(value)
checkbox = find(:#{name})
if (#{name}_checked_values.include?(value) && !checkbox.selected?) ||
(#{name}_unchecked_values.include?(value) && checkbox.selected?)
checkbox.click
end
end
@saturnflyer
saturnflyer / gist:10140954
Created April 8, 2014 15:17
overriding presenter with
def with_conditions(method_name, computed_value)
if method_name.to_s == 'address' && !computed_value.nil?
address.renderable?
else
super
end
end
@saturnflyer
saturnflyer / exception.rb
Created November 26, 2013 18:37
Exception cause patch for access like a null object
class Exception
def cause_message
cause && cause.message
end
def cause_backtrace
cause && cause.backtrace
end
end
Introduction .....................................................................................................6
What This Is ..................................................................................................................8
What This Is Not ...........................................................................................................8
Lost In Translation...........................................................................................9
Our First Bugs..............................................................................................................10
Object Orientation ........................................................................................12
Where We Are..............................................................................................................12
Where We’ve Been .......................................................................................................20
Where We’re Going ....................
require 'delegate'
# Public: This class initializes a presenter object.
# The presenter wraps a given object and a view object and will forward
# method calls to each. By default, any unknown method will be passed
# to the first object.
# Any methods that need to be sent to the view, may be done explicitly.
#
# Examples:
#
# # Inside a controller you create a helper method to access this object
@saturnflyer
saturnflyer / topics.md
Last active December 22, 2015 19:59
newsletter topics
  1. Ruby Delegate.rb Secrets
  2. Simple presenters to clean up views
  3. Ruby Forwardable in Depth
  4. how it works
  5. SingleForwardable
  6. identifying responsibility leaks with if
  7. enforcing encapsulation
  8. seeing the westward flow and heading east
  9. ActiveSupport::Delegation, the Rails approach to forwarding
  10. The Casting gem and why it's different from SimpleDelegator
@saturnflyer
saturnflyer / gol.rb
Last active December 21, 2015 00:09
Conway's Game of Life
seed = [[0,0],[0,1],[0,2]]
puts "First generation:"
puts seed.to_s
# Point surrounding a given point
perimeter = ->(x, y){
[
[x - 1, y + 1], [x, y + 1], [x + 1, y + 1],
[x - 1, y ], [x + 1, y ],
[x - 1, y - 1], [x, y - 1], [x + 1, y - 1]
@saturnflyer
saturnflyer / multiwrapper.rb
Last active December 20, 2015 13:48
Maybe get values from an object. Wrap one object, but check if an alternate one has better data first. In this case, we wrap a user object, but for the methods we specify we can check the profile object for data before falling back to the user.
require 'delegate'
class SpecialFormatter < SimpleDelegator
def initialize(user, profile)
super(user)
@profile = profile
end
attr_reader :profile
def self.maybe(*names)
@saturnflyer
saturnflyer / gist:6011386
Last active December 19, 2015 20:09
mail helper
def custom_mail_to(email, link_text, i18n_subject_key)
mail_to(email, link_text, :subject => I18n.t(i18n_subject_key, :email => email))
end
@saturnflyer
saturnflyer / dynamic_initialize.rb
Created July 10, 2013 14:11
Dynamically defining the initialize method and setting the arity.
class Something
def self.init(*init_args)
# this works but...
define_method(:initialize){|*args|
# arity is -1
}
class_eval %Q<
def initialize(#{*init_args})