bib 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/bib/pool.rb +26 -0
  2. metadata +47 -0
data/lib/bib/pool.rb ADDED
@@ -0,0 +1,26 @@
1
+ class Pool
2
+ def initialize(klass, init_func, valid_func, destroy_func=nil)
3
+ @klass = klass
4
+ raise "Error: '#{init_func.inspect}' not a proc" unless init_func.respond_to?(:call)
5
+ @init_func = init_func
6
+ raise "Error" unless valid_func.is_a? Proc
7
+ @valid_func = valid_func
8
+ raise "Error" unless destroy_func.nil? || destroy_func.is_a?(Proc)
9
+ @destroy_func = destroy_func
10
+ @pool = []
11
+ end
12
+
13
+ def get(*args)
14
+ (@pool.size>0 ? @pool.shift : @klass.new).tap{|o|
15
+ @init_func.call(o, *args, &(block_given? ? Proc.new : nil)) }
16
+ end
17
+
18
+ def release(obj)
19
+ if @valid_func.call(obj) then
20
+ @pool << obj
21
+ else
22
+ @destroy_func.call(obj) unless @destroy_func.nil?
23
+ end
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexandre Ignjatovic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A set of development helpers, whose purpose is to avoid the developper
15
+ to drool around
16
+ email: alexandre.ignjatovic@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/bib/pool.rb
22
+ homepage: https://github.com/bankair/bib
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Development helpers
47
+ test_files: []