punchline 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cf29f420c42097206b7df8314facad3f2f777c6
4
- data.tar.gz: 73c9c8d9036f745b3055551ab0de2e40204ae954
3
+ metadata.gz: 9aea1e279156757a3782956b9684c86b9945e43c
4
+ data.tar.gz: a7a3e6921c2bcd932514d7dac7f4177ec77c323b
5
5
  SHA512:
6
- metadata.gz: aea0964fe5cf076e7c7cc645997877f1cb056437c18f2f3a332c5932f80c8106ce628197e8fe6bef2f70d417e7a21e162adeee2e1eccfd2149185d0ad077e4fa
7
- data.tar.gz: 504d8ffd5b9c953d50e3e60629e5a0edab8da91a56629dd583c61965b67ffd8c4240122a0053ff81b029ec66ebd1b923abc470707bcdd5353d835b5535d24240
6
+ metadata.gz: b8eccc58cded5aee6dba4b48ec543d7f60cbe470e28112ce6cd6f4b13fc190d2728b680774e13a1c338d3bf592efa493a8e44703a167f656e9789aad363085a6
7
+ data.tar.gz: d6b6b59dbe054eb1fbd7f36dedb641792c707aa91a10b570e8343dcbaf03ea92e893f7db73412bc4e9428b00704b4aca16f1c65da64662f8cf5a6602933da9d8
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://travis-ci.org/catkins/punchline.svg)](https://travis-ci.org/catkins/punchline) [![Dependency Status](https://gemnasium.com/catkins/punchline.svg)](https://gemnasium.com/catkins/punchline) [![Coverage Status](https://img.shields.io/coveralls/catkins/punchline.svg)](https://coveralls.io/r/catkins/punchline) [![Code Climate](https://codeclimate.com/github/catkins/punchline/badges/gpa.svg)](https://codeclimate.com/github/catkins/punchline)
1
+ [![Build Status](https://travis-ci.org/catkins/punchline.svg)](https://travis-ci.org/catkins/punchline) [![Dependency Status](https://gemnasium.com/catkins/punchline.svg)](https://gemnasium.com/catkins/punchline) [![Coverage Status](https://img.shields.io/coveralls/catkins/punchline.svg)](https://coveralls.io/r/catkins/punchline) [![Code Climate](https://codeclimate.com/github/catkins/punchline/badges/gpa.svg)](https://codeclimate.com/github/catkins/punchline) [![Gem Version](https://badge.fury.io/rb/punchline.svg)](http://badge.fury.io/rb/punchline)
2
2
 
3
3
  # Punchline
4
4
 
@@ -34,18 +34,22 @@ $ bundle
34
34
  require 'punchline'
35
35
 
36
36
  # optionally override Punchline with your own Redis client, otherwise defaults to Redis.new
37
- Punchline.config.redis = Redis.new host: "10.0.1.1", port: 6830
37
+ Punchline.configure do |config|
38
+ config.redis = Redis.new host: "10.0.1.1", port: 6830
39
+ end
38
40
 
39
41
  # create a queue
40
42
  queue = Punchline::MinQueue.new
41
43
  queue.length # => 0
42
44
 
43
45
  # add a key
44
- queue.enqueue priority: Time.now.to_i, value: 'hello!' # => true
46
+ queue.enqueue 'hello!' # => true
47
+
45
48
  queue.length # => 1
46
49
 
47
50
  # shortly after... higher priority score is rejected
48
- queue.enqueue priority: Time.now.to_i, value: 'hello!' # => false
51
+ queue.enqueue 'hello!' # => false
52
+
49
53
  queue.length # => 1
50
54
 
51
55
  # original key is retrieved
@@ -54,6 +58,19 @@ queue.dequeue # => { :priority => 1411405014, :value => "hello!" }
54
58
  # queue is now empty
55
59
  queue.length # => 0
56
60
 
61
+ # optionally set your own priority value
62
+ queue.enqueue 'hello!', priority: 155 # => true
63
+
64
+ # fetch all without dequeuing
65
+ queue.enqueue 'hello!'
66
+ queue.enqueue 'adding values!'
67
+ queue.all # [{:value=>"hello", :priority=>1411445996}, {:value=>"adding values!", :priority=>1411446073}]
68
+
69
+ # clear out queue
70
+ queue.clear!
71
+ queue.all # => []
72
+ queue.length # => 0
73
+
57
74
  ```
58
75
 
59
76
  ## TODO
data/lib/punchline.rb CHANGED
@@ -4,6 +4,10 @@ require 'punchline/min_queue'
4
4
 
5
5
  module Punchline
6
6
  class << self
7
+ def configure(&block)
8
+ yield config if block_given?
9
+ end
10
+
7
11
  def config
8
12
  @config ||= Configuration.new
9
13
  end
@@ -24,9 +24,8 @@ module Punchline
24
24
  end
25
25
  end
26
26
 
27
- def enqueue(options = {})
28
- priority = options.fetch :priority
29
- value = options.fetch :value
27
+ def enqueue(value, options = {})
28
+ priority = options[:priority] || Time.now.to_i
30
29
  @enqueue.call([key], [priority, value]) == 1
31
30
  end
32
31
 
@@ -1,5 +1,5 @@
1
1
  # Encoding: utf-8
2
2
 
3
3
  module Punchline
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
data/punchline.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'bundler', '~> 1.5'
24
24
  spec.add_development_dependency 'rake'
25
25
  spec.add_development_dependency 'rspec', '~> 3.1.0'
26
+ spec.add_development_dependency 'timecop', '~> 0.7.1'
26
27
  spec.add_development_dependency 'pry'
27
28
  spec.add_development_dependency 'coveralls'
28
29
  end
@@ -9,7 +9,10 @@ module Punchline
9
9
  end
10
10
 
11
11
  before(:all) { clear_queue! }
12
- after(:each) { clear_queue! }
12
+ after(:each) do
13
+ subject.reset_scripts!
14
+ clear_queue!
15
+ end
13
16
 
14
17
  let(:some_key) { TEST_KEY }
15
18
  subject(:min_queue) { MinQueue.new some_key }
@@ -42,7 +45,7 @@ module Punchline
42
45
  describe '#length' do
43
46
  it { should respond_to :length }
44
47
 
45
- let(:mock_redis) { double 'redis', zcard: 5, del: true }
48
+ let(:mock_redis) { double 'redis', zcard: 5, del: true, script: true }
46
49
 
47
50
  it 'is initially zero' do
48
51
  expect(subject.length).to eq 0
@@ -74,7 +77,7 @@ module Punchline
74
77
  end
75
78
 
76
79
  it 'returns elements as hashes' do
77
- subject.enqueue priority: 123, value: 'hello'
80
+ subject.enqueue 'hello', priority: 123
78
81
 
79
82
  hash = subject.all.first
80
83
  expect(hash).not_to be_nil
@@ -84,15 +87,11 @@ module Punchline
84
87
  end
85
88
 
86
89
  describe '#enqueue' do
87
- after(:each) do
88
- subject.reset_scripts!
89
- end
90
-
91
90
  it { should respond_to :enqueue }
92
91
 
93
92
  it 'increases the length by 1' do
94
93
  expect {
95
- subject.enqueue priority: 123, value: 'hello'
94
+ subject.enqueue 'hello'
96
95
  }.to change {
97
96
  subject.length
98
97
  }.by 1
@@ -104,50 +103,63 @@ module Punchline
104
103
  end
105
104
 
106
105
  it 'returns false when key is not written' do
107
- result = subject.enqueue priority: 123, value: 'hello'
108
- result = subject.enqueue priority: 456, value: 'hello'
106
+ result = subject.enqueue 'hello', priority: 123
107
+ result = subject.enqueue 'hello', priority: 456
109
108
  expect(result).to eq false
110
109
  end
111
110
 
112
111
  describe 'duplicates' do
113
112
  it 'ignores duplicate values' do
114
- subject.enqueue priority: 123, value: 'hello'
113
+ subject.enqueue 'hello', priority: 123
115
114
 
116
115
  expect {
117
- subject.enqueue priority: 567, value: 'hello'
116
+ subject.enqueue 'hello', priority: 567
118
117
  }.to change {
119
118
  subject.length
120
119
  }.by(0)
121
120
  end
122
121
 
123
122
  it 'retains only the lowest priority score' do
124
- subject.enqueue priority: 123, value: 'hello'
125
- subject.enqueue priority: 567, value: 'hello'
126
- subject.enqueue priority: 789, value: 'hello'
123
+ subject.enqueue 'hello', priority: 123
124
+ subject.enqueue 'hello', priority: 567
125
+ subject.enqueue 'hello', priority: 25
127
126
 
128
127
  pair = subject.dequeue
129
- expect(pair[:priority]).to eq 123
128
+ expect(pair[:priority]).to eq 25
130
129
  end
131
130
  end
132
- end
133
131
 
134
- describe '#dequeue' do
135
- after(:each) do
136
- subject.reset_scripts!
137
- subject.clear!
132
+ describe 'sequential inserts' do
133
+ before(:each) { Timecop.freeze }
134
+
135
+ it 'keeps the oldest value for a given key' do
136
+ original_time = Time.now.to_i
137
+ subject.enqueue 'hello'
138
+
139
+ Timecop.travel Time.now + 1000
140
+ subject.enqueue 'hello'
141
+
142
+ Timecop.travel Time.now + 1000
143
+ subject.enqueue 'hello'
144
+
145
+ result = subject.dequeue
146
+ expect(result[:priority]).to eq original_time
147
+ end
138
148
  end
149
+ end
139
150
 
151
+ describe '#dequeue' do
140
152
  it { should respond_to :dequeue }
141
153
 
142
154
  it 'decreases the length by 1' do
143
- subject.enqueue priority: 123, value: 'hello'
155
+ subject.enqueue 'hello', priority: 123
144
156
 
145
157
  expect{ subject.dequeue }.to change { subject.length }.by -1
146
158
  end
147
159
 
148
160
  it 'returns the pair with the lowest score' do
149
- subject.enqueue priority: 123, value: 'hello'
150
- subject.enqueue priority: 567, value: 'world'
161
+ subject.enqueue 'hello', priority: 123
162
+ subject.enqueue 'world', priority: 567
151
163
  expect(subject.dequeue).to eq({ value: 'hello', priority: 123 })
152
164
  end
153
165
  end
@@ -161,7 +173,7 @@ module Punchline
161
173
  end
162
174
 
163
175
  it 'resets the length' do
164
- subject.enqueue priority: 123, value: 'hello'
176
+ subject.enqueue 'hello', priority: 123
165
177
  expect { subject.clear! }.to change { subject.length }.by -1
166
178
  end
167
179
  end
data/spec/spec_helper.rb CHANGED
@@ -3,3 +3,13 @@ Coveralls.wear!
3
3
 
4
4
  require 'pry'
5
5
  require 'punchline'
6
+ require 'timecop'
7
+
8
+ RSpec.configure do |config|
9
+ config.after(:each) do
10
+
11
+ # always unfreeze timecop after specs
12
+ Timecop.return
13
+
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: punchline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Atkins
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.7.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.7.1
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: pry
71
85
  requirement: !ruby/object:Gem::Requirement