File: README.md

package info (click to toggle)
ruby-tool 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 148 kB
  • ctags: 37
  • sloc: ruby: 283; makefile: 3
file content (93 lines) | stat: -rw-r--r-- 1,976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
*Make sure you view the correct docs: [latest release](http://rubydoc.info/gems/tool/frames), [master](http://rubydoc.info/github/rkh/tool/master/frames).*

Welcome to [Tool](http://www.youtube.com/watch?v=mYKLvYGqaC0), the general purpose Ruby library used by Sinatra 2.0, Mustermann and related projects.

## Tool::Decoration

Mixin for easy method decorations.

``` ruby
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.

``` ruby
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.

``` ruby
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:

``` ruby
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.

``` ruby
require 'tool/warning_filter'

Foo = 10
Foo = 20
```