This file contains hidden or 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 BaseEnum | |
include Enumerable | |
attr_reader :ctx | |
def initialize( *ctx ) | |
@ctx = ctx | |
end | |
def each( *args, &blk ) | |
return enum_for __callee__, *args unless block_given? |
This file contains hidden or 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 HashlikeFetchCache | |
include HashlikeOverlay | |
def initialize( context ) | |
@overlay = Hash[] | |
@context = context | |
freeze | |
end | |
def []( key ) |
This file contains hidden or 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
# Fixed point combinator. Because reasons. | |
# @param [Block with arity 1] Receives recursion Proc, must return Proc representing function (any arity) | |
# @return [Proc] | |
def Y | |
fail unless block_given? | |
lambda{ |fn| fn.call fn }.call lambda{ |fn| | |
lambda{ |*args| | |
(yield fn.call fn).call *args | |
} | |
} |
This file contains hidden or 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
# Enforces upper bound on the frequency of arbitrary actions. | |
# Thread-safe, but not terribly efficient. | |
class RateLimit | |
# Blocks passed to #enforce are executed with a frequency not exceeding the average of +limit+ calls per +period+ over | |
# +burst+ consecutive periods. Thread safe, but be advised there are no guarantees about the order or timeliness in | |
# which the given blocks are executed. | |
# @param limit [Numeric] Target average number of events per period | |
# @param period [Numeric] Duration of period in seconds (or equivalent, like ActiveSupport::Duration) | |
# @param burst [Numeric] Number of periods over which to average | |
def initialize( limit, period = 1, burst = 3 ) |
This file contains hidden or 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 NormalizeHeredoc | |
module_function | |
# Normalize indents, like for heredoc strings | |
def NormalizeIndent( msg ) | |
msg = String(msg) | |
depth = msg.scan(/^\s*/).flatten.map(&:size).min | |
msg.gsub(/^\s{#{depth}}/, '') | |
end | |
end |
This file contains hidden or 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
# @author Erik Elmore <[email protected]> | |
# This is getting a little out of hand... :dizzy_face: | |
# For printing trace output for demonstration | |
module Status | |
def status( method_name, args = [], extra = nil ) | |
extra = ' => %s' % extra if extra | |
puts '%s#%s(%s)%s' % [self.class, method_name, args.join(', '), extra] | |
end | |
end |
This file contains hidden or 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
# Author: Erik Elmore <[email protected]> | |
# License: Public Domain | |
# Convenience in logging | |
module LogMethods | |
def error( *args, &blk ) | |
log :error, *args, &blk | |
end | |
private :error | |
This file contains hidden or 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
# Author: Erik Elmore <[email protected]> | |
# License: Public Domain | |
require 'uri' | |
require 'aws-sdk' | |
# Facilitates polling and handling queue messages. | |
class SQSMessageDispatch | |
# Configure a new SQSMessageDispatch for the given queue URL. | |
# @param queue_url [String] URL of the SQS message queue | |
# @param sqs_opts [Hash] parameters passed to Aws::SQS::Client.new (except :region) |
This file contains hidden or 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
# Author: Erik Elmore <[email protected]> | |
# License: Public Domain | |
require 'securerandom' | |
require 'aws' | |
# Test the feasability of using temporary queues and topics as a means of receiving | |
# notifications from remote systems via SQS and SNS. TestClient creates either only a | |
# queue or both a queue and a topic before sending a request to the server. When the | |
# client wishes to receive its response directly in a temporary queue, the request | |
# body is the URL to its temporary queue. When the client wishes to be notified via a |
This file contains hidden or 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
# Author: Erik Elmore <[email protected]> | |
# License: Public Domain | |
# Enable "diffing" and two-way transformations between collection objects | |
module Diffable | |
# Calculates the changes required to transform self to the given collection. | |
# @param b [Enumerable] The other collection object | |
# @return [Array] The Diff: A two-element change set representing items to exclude and items to include | |
def diff( b ) | |
a, b = to_a, b.to_a |