Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Octocat-spinner-32 examples makeover February 21, 2014
Octocat-spinner-32 lib v0.2.2 February 23, 2014
Octocat-spinner-32 spec rework ThreadLocal February 23, 2014
Octocat-spinner-32 .gitignore makeover February 21, 2014
Octocat-spinner-32 .rspec makeover February 21, 2014
Octocat-spinner-32 .travis.yml - Setup rakefile. March 31, 2014
Octocat-spinner-32 Gemfile makeover February 21, 2014
Octocat-spinner-32 LICENSE makeover February 21, 2014
Octocat-spinner-32 README.md add link to youtube video February 23, 2014
Octocat-spinner-32 Rakefile - Setup rakefile. March 31, 2014
Octocat-spinner-32 tool.gemspec - Setup rakefile. March 31, 2014
README.md

Make sure you view the correct docs: latest release, master.

Welcome to Tool, the general purpose Ruby library used by Sinatra 2.0, Mustermann and related projects.

Tool::Decoration

Mixin for easy method decorations.

class Frank
  extend Tool::Decoration
  def self.get(path, &block)
    decorate(block) do |method|
      puts "mapping GET #{path} to #{method}"
    end
  end
end

class MyApp < Frank
  get '/hi' do
    "Hello World"
  end

  get '/'; get '/index.php'
  def index
    "This is the index page."
  end
end

Tool::EqualityMap

Weak reference caching based on key equality. Used for caching.

class ExpensiveComputation
  @map = Tool::EqualityMap.new

  def self.new(*args)
    @map.fetch(*args) { super }
  end
end

Note that fetch is not guaranteed to return the object, even if it has not been garbage collected yet, especially when used concurrently. Therefore, the block passed to fetch has to be idempotent.

Tool::ThreadLocal

Have thread local values without them actually being thread global.

Advantages:

  • Values for all threads are garbage collected when ThreadLocal instance is.
  • Values for specific thread are garbage collected when thread is.
  • No hidden global state.
  • Supports other data types besides hashes.
local = Tool::ThreadLocal.new
local[:key] = "value"

Thread.new do
  local[:key] = "other value"
  puts local[:key] # other value
end.join

puts local[:key] # value

Usage with a pre-filled array:

local = Tool::ThreadLocal.new([:foo])
local << :bar

Thread.new { p local }.join # [:foo]
p local # [:foo, :bar]

Tool::WarningFilter

Enables Ruby's built-in warnings (-w) but filters out those caused by third-party gems. Does not invlove any manual set up.

require 'tool/warning_filter'

Foo = 10
Foo = 20
Something went wrong with that request. Please try again.