-
-
Save rociiu/305184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ParameterizedConcern | |
def extended(mod = nil, &block) | |
unless mod | |
@extended_block = block | |
return | |
end | |
mod.module_eval(&@extended_block) if @extended_block | |
end | |
def self.extended(mod) | |
class << mod | |
def with(options) define_method(:with_options) { options }; dup end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Query | |
class Original | |
def initialize(connection, query_string, *args) | |
# Implement initialize | |
end | |
def select | |
# Implement select | |
end | |
end | |
module Timeouter | |
extend ParameterizedConcern | |
def select | |
Timeout.timeout(with_options[:duration]) do | |
super | |
end | |
end | |
end | |
module Memoizer | |
extend ParameterizedConcern | |
extended do | |
@query_memo ||= {} | |
end | |
def new(connection, query_string, *args) | |
@query_memo[[connection, query_string, args]] ||= super | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyQuery < Query::Original | |
include Query::Timeouter.with(:duration => 5) | |
extend Query::Memoizer | |
end | |
class NewQuery < Query::Original | |
include Query::Timeouter.with(:duration => 10) | |
end | |
MyQuery.new("connection", "get me 1").select | |
MyQuery.new("connection", "get me 1").select | |
NewQuery.new("connection", "get me 1").select | |
NewQuery.new("connection", "get me 1").select | |
MyQuery.new("connection", "get me 1").select | |
MyQuery.new("connection", "get me 1").select | |
NewQuery.new("connection", "get me 1").select | |
NewQuery.new("connection", "get me 1").select |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Query | |
class Original | |
def initialize(connection, query_string, *args) | |
puts "Made *new* query with #{connection.inspect}, #{query_string.inspect}, #{args.inspect}" | |
end | |
def select | |
puts "Selecting" | |
end | |
end | |
module Timeouter | |
extend ParameterizedConcern | |
def select | |
puts "Timing out with #{with_options[:duration]}" | |
Timeout.timeout(with_options[:duration]) do | |
super | |
end | |
end | |
end | |
module Memoizer | |
extend ParameterizedConcern | |
extended do | |
@query_memo ||= {} | |
end | |
def new(connection, query_string, *args) | |
if @query_memo.key?([connection, query_string, args]) | |
puts "Got *memoized* query for #{connection.inspect}, #{query_string.inspect}, #{args.inspect}" | |
end | |
@query_memo[[connection, query_string, args]] ||= super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment