Skip to content

Instantly share code, notes, and snippets.

Chris Corbyn d11wtq

  • Melbourne, Australia
Block or report user

Report or block d11wtq

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@d11wtq
d11wtq / connection_pool_adapter.rb
Created May 6, 2011
Sample database.yml for multiple slaves/masters with DataMapper
View connection_pool_adapter.rb
module DataMapper
module Adapters
class ConnectionPoolAdapter < AbstractAdapter
def initialize(name, options)
super
assert_kind_of 'options', @options[:pool], Array
raise ArgumentError, "The are no adapters in the adapter pool" if @options[:pool].empty?
@d11wtq
d11wtq / sequence.rb
Created May 6, 2011
Sequence Property type for DataMapper
View sequence.rb
=begin
@author Chris Corbyn
@license None, use at your own risk
=end
module DataMapper
class Property
# Adds support for a sequences table to DataMapper, via a Sequence property type.
#
# class Person
View time.rb
class Time
class << self
attr_accessor :mock_time
def now_with_mock_time
@mock_time || now_without_mock_time
end
alias_method_chain :now, :mock_time
@d11wtq
d11wtq / example.rb
Created Jul 4, 2011
Validation always fails with "Address must be an Integer" ?
View example.rb
class Example
include DataMapper::Resource
property :id, Serial
property :ip_address, IPAddressInteger
end
example = Example.new
example.ip_address = "10.48.1.1"
@d11wtq
d11wtq / custom_json_format.rb
Created Jul 4, 2011
Workaround for dm-rest-adapter serialization to JSON with :field and foreign keys
View custom_json_format.rb
# This is used temporarily, until serialization to :raw is done in dm-serializer
module DataMapper
module Rest
class CustomJsonFormat < DataMapperRest::Format::Json
def string_representation(resource)
model = resource.model
hash = {}
hash = model.properties.reduce(hash) do |h, property|
h.merge(property.field.to_sym => property.dump(property.get(resource)))
@d11wtq
d11wtq / devil_ico_example.rb
Created Jul 8, 2011
Quick and dirty example of using Devil to convert ICO to PNG
View devil_ico_example.rb
ico_file = "some-file.ico"
png_file = "output.png"
Devil.load(ico_file) do |img|
img.resize(16, 16).save(png_file)
end
@d11wtq
d11wtq / datamapper_example.rb
Created Jul 12, 2011
DataMapper example showing a few different legacy support features
View datamapper_example.rb
module Commerce
class Refund
include DataMapper::Resource
include Defaults
PENDING = 'pending'
SUCCESSFUL = 'successful'
DECLINED = 'declined'
ERROR = 'error'
@d11wtq
d11wtq / verbose_job.rb
Created Aug 25, 2011
Example of wrapping methods using mixins (more complicated than it should be)
View verbose_job.rb
module VerboseJob
module ClassMethods
def wrap_perform!
class << self
def perform_with_verbose(*args)
JobLogger.verbose { perform_without_verbose(*args) }
end
alias_method_chain :perform, :verbose \
unless instance_method(:perform) == instance_method(:perform_with_verbose)
@d11wtq
d11wtq / deliver_email_job.rb
Created Aug 28, 2011
Customizing ActionMailer delivery methods
View deliver_email_job.rb
# Resque job to do the true outbound sending
class DeliverEmailJob
include ProjectName::Job::Logging
@queue = :mail_queue
def self.perform(args)
message = QueuedEmail.get!(args["message_id"])
logger.info("Delivering (%s) to %s" % [message.subject, message.formatted_recipient])
@d11wtq
d11wtq / rails_session_bug_spec.rb
Created Sep 18, 2011
Showing the current Rails 3.1 bug where generating a new session Set-Cookie header crashes Rack if :domain => :all
View rails_session_bug_spec.rb
require 'spec_helper'
class AbstractStoreSubclass < ActionDispatch::Session::AbstractStore
def get_session(env, sid)
[sid || generate_sid, nil]
end
def set_session(env, sid, data, options)
end
You can’t perform that action at this time.