Skip to content

Instantly share code, notes, and snippets.

View rinaldifonseca's full-sized avatar

Rinaldi Fonseca rinaldifonseca

View GitHub Profile
# Framework
# ===========================================
module ContextAccessor
def context
Thread.current[:context]
end
end
module Context
include ContextAccessor
@rinaldifonseca
rinaldifonseca / mt.rb
Created June 7, 2012 20:17 — forked from alanq/mt.rb
DCI in ruby without injection - MoneyTransfer example
# The code can be seen here: gist.github.com/2711153 (it does use a small bit
# from the Rails framework, just for the persisted LedgerEntry and to create
# attribute readers). Some notes about the code:
# - Each Role is a class, having only (public) class methods. To help
# avoid them being called outside of their Context, a Role Method only has
# access to its associated object while the role's context is the
# current/active global context.
# - Following Trygve's approach: "Roles are referenced by name. If I mean
# the Role I write the role name, not 'self'."
# - For referencing the object playing a role, I've overridden the Role's
# Expand print panel by default
defaults write NSGlobalDomain PMPrintingExpandedStateForPrint -bool true
# Disable the “Are you sure you want to open this application?” dialog
defaults write com.apple.LaunchServices LSQuarantine -bool false
# Enable subpixel font rendering on non-Apple LCDs
defaults write NSGlobalDomain AppleFontSmoothing -int 2
# Disable press-and-hold for keys in favor of key repeat

#include and #extend have same effect on instances as inheritance

Let's say we have this

module A; end
module B; end
class C; end
class D < C; end

Then we modify instance

class DashboardPresenter < ClassicPresenter::Base
def navigation
if context.controller_name == 'vendors'
context.render 'shared/dashboard/vendor_navigation'
end
end
end
@rinaldifonseca
rinaldifonseca / mt.rb
Created June 26, 2012 03:27 — forked from alanq/mt.rb
DCI in ruby without injection - MoneyTransfer example
-----------------------------------------
Boilerplate code (in rails_app_root/lib)
-----------------------------------------
module ContextAccessor
def context
Thread.current[:context]
end
end
def screenshot
require 'capybara/util/save_and_open_page'
now = Time.now
p = "/#{now.strftime('%Y-%m-%d-%H-%M-%S')}-#{rand}"
Capybara.save_page body, "#{p}.html"
path = Rails.root.join("#{Capybara.save_and_open_page_path}" "#{p}.png").to_s
page.driver.render path
Launchy.open path
end
@rinaldifonseca
rinaldifonseca / sketch.rb
Created July 9, 2012 04:00 — forked from mikegehard/sketch.rb
Responders implemented using a block instead of another class
class Organization
def to_param
"42"
end
def saved?
rand > 0.5
end
end
@rinaldifonseca
rinaldifonseca / Plea.markdown
Created July 11, 2012 03:41 — forked from justinko/Plea.markdown
Am I doing it wrong?

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this:

@rinaldifonseca
rinaldifonseca / gist:3101121
Created July 12, 2012 21:27 — forked from viniciusteles/gist:556029
Sete Atitudes para Hackear a Indústria de Software
Sete Atitudes para Hackear a Indústria de Software
By Klaus Wuestefeld
1) Torne-se excelente.
Seja realmente bom em alguma coisa. Não fique só choramingando ou
querendo progredir às custas dos outros. Não pense q pq vc sentou 4
anos numa faculdade ouvindo um professor falar sobre software q vc
sabe alguma coisa. Jogador de futebol não aprende a jogar bola tendo