Class: AccessStack
- Inherits:
-
Object
- Object
- AccessStack
- Defined in:
- lib/access_stack.rb,
lib/access_stack/version.rb
Constant Summary collapse
- TimeoutError =
Class.new StandardError
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#create ⇒ Object
Returns the value of attribute create.
-
#destroy ⇒ Object
Returns the value of attribute destroy.
-
#expires ⇒ Object
Returns the value of attribute expires.
-
#size ⇒ Object
Returns the value of attribute size.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#validate ⇒ Object
Returns the value of attribute validate.
Instance Method Summary collapse
- #create_objects(num = 1) ⇒ Object
- #empty! ⇒ Object
- #empty? ⇒ Boolean
- #fill ⇒ Object
-
#initialize(opts = {}) ⇒ AccessStack
constructor
A new instance of AccessStack.
- #reap! ⇒ Object
- #with(&block) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ AccessStack
Returns a new instance of AccessStack.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/access_stack.rb', line 11 def initialize(opts={}) @timeout = opts[:timeout] || opts["timeout"] || 5 @size = opts[:size] || opts["size"] || 5 @expires = opts[:expires] || opts["expires"] || -1 @expr_hash = {} @stack = [] @count = 0 @mutex = Mutex.new @create = opts[:create] || opts["create"] @destroy = opts[:destroy] || opts["destroy"] @validate = opts[:validate] || opts["validate"] end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
6 7 8 |
# File 'lib/access_stack.rb', line 6 def count @count end |
#create ⇒ Object
Returns the value of attribute create.
7 8 9 |
# File 'lib/access_stack.rb', line 7 def create @create end |
#destroy ⇒ Object
Returns the value of attribute destroy.
7 8 9 |
# File 'lib/access_stack.rb', line 7 def destroy @destroy end |
#expires ⇒ Object
Returns the value of attribute expires.
7 8 9 |
# File 'lib/access_stack.rb', line 7 def expires @expires end |
#size ⇒ Object
Returns the value of attribute size.
7 8 9 |
# File 'lib/access_stack.rb', line 7 def size @size end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/access_stack.rb', line 7 def timeout @timeout end |
#validate ⇒ Object
Returns the value of attribute validate.
7 8 9 |
# File 'lib/access_stack.rb', line 7 def validate @validate end |
Instance Method Details
#create_objects(num = 1) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/access_stack.rb', line 76 def create_objects(num=1) created_count = 0 threadsafe do num.times do if @count < @size @stack.push create_obj @count += 1 created_count += 1 end end end created_count end |
#empty! ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/access_stack.rb', line 62 def empty! return if @count == 0 threadsafe do @stack.each(&@destroy.method(:call)) @expr_hash.clear @stack.clear @count = 0 end end |
#empty? ⇒ Boolean
92 93 94 |
# File 'lib/access_stack.rb', line 92 def empty? @count == 0 end |
#fill ⇒ Object
72 73 74 |
# File 'lib/access_stack.rb', line 72 def fill create_objects @count end |
#reap! ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/access_stack.rb', line 50 def reap! return true if @count == 0 threadsafe do @stack.reject(&method(:obj_valid)).each do |instance| @destroy.call instance @expr_hash.delete instance @stack.delete instance @count -= 1 end end end |
#with(&block) ⇒ Object
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 |
# File 'lib/access_stack.rb', line 24 def with(&block) begin obj = nil threadsafe do obj = @stack.pop end if !(obj_valid obj) obj = nil @count -= 1 end if @count < @size && obj.nil? @count += 1 obj = create_obj end return block.call obj ensure threadsafe do @stack.push obj end end end |