Skip to content

Instantly share code, notes, and snippets.

View d11wtq's full-sized avatar

Chris Corbyn d11wtq

  • Melbourne, Australia
View GitHub Profile
@d11wtq
d11wtq / connection_pool_adapter.rb
Created May 6, 2011 13:45
Sample database.yml for multiple slaves/masters with DataMapper
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 14:41
Sequence Property type for DataMapper
=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
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 July 4, 2011 06:19
Validation always fails with "Address must be an Integer" ?
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 July 4, 2011 16:13
Workaround for dm-rest-adapter serialization to JSON with :field and foreign keys
# 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 July 8, 2011 14:44
Quick and dirty example of using Devil to convert ICO to PNG
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 July 12, 2011 13:06
DataMapper example showing a few different legacy support features
module Commerce
class Refund
include DataMapper::Resource
include Defaults
PENDING = 'pending'
SUCCESSFUL = 'successful'
DECLINED = 'declined'
ERROR = 'error'
@d11wtq
d11wtq / verbose_job.rb
Created August 25, 2011 13:31
Example of wrapping methods using mixins (more complicated than it should be)
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 August 28, 2011 04:14
Customizing ActionMailer delivery methods
# 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 September 18, 2011 06:40
Showing the current Rails 3.1 bug where generating a new session Set-Cookie header crashes Rack if :domain => :all
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