allowed 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/allowed/throttle.rb +10 -1
- data/spec/lib/allowed/throttle_spec.rb +30 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e450c5a1540f74c95fa6da6c5fc675b93b38272f
|
4
|
+
data.tar.gz: 077515cf12e15f52c1fd65ec5c6c0d13933e2925
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c7cab82212758d6722e60b6ee310ccb7dc50632aeed7e9a8d5992746642e2572e95f8fd39afbd90811246ca6e8693e9562e1a73a00f5d8d524259d98a044311
|
7
|
+
data.tar.gz: 64d5c21ce51dcffbcbf08866735c9489ddc09e4a621c7b4540b60bae2122504e6bccf2bcfa43240f6148673e8bad08230807d520c71b0e9c5ec58ae64ca81760
|
data/lib/allowed/throttle.rb
CHANGED
@@ -18,7 +18,7 @@ module Allowed
|
|
18
18
|
def valid?(record)
|
19
19
|
return true if skip?(record)
|
20
20
|
|
21
|
-
scope_for(record).count <
|
21
|
+
scope_for(record).count < allowed_count(record)
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
@@ -44,5 +44,14 @@ module Allowed
|
|
44
44
|
def timeframe
|
45
45
|
options.fetch(:per, 1.day).ago
|
46
46
|
end
|
47
|
+
|
48
|
+
def allowed_count(record)
|
49
|
+
case limit
|
50
|
+
when Integer
|
51
|
+
limit
|
52
|
+
when Symbol
|
53
|
+
record.__send__(limit)
|
54
|
+
end
|
55
|
+
end
|
47
56
|
end
|
48
57
|
end
|
@@ -102,6 +102,36 @@ describe Allowed::Throttle, "#valid?, above limit" do
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
+
describe Allowed::Throttle, "#valid?, with limit method symbol" do
|
106
|
+
subject { Allowed::Throttle.new(:custom_limit) }
|
107
|
+
|
108
|
+
let(:record) { ExampleRecord.new }
|
109
|
+
|
110
|
+
before do
|
111
|
+
2.times { ExampleRecord.create }
|
112
|
+
end
|
113
|
+
|
114
|
+
it "returns true if higher than the count" do
|
115
|
+
ExampleRecord.class_eval do
|
116
|
+
def custom_limit
|
117
|
+
3
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
expect(subject).to be_valid(record)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "returns false if lower than the count" do
|
125
|
+
ExampleRecord.class_eval do
|
126
|
+
def custom_limit
|
127
|
+
1
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
expect(subject).to_not be_valid(record)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
105
135
|
describe Allowed::Throttle, "#valid?, with custom timeframe" do
|
106
136
|
subject { Allowed::Throttle.new(1, per: 5.minutes) }
|
107
137
|
|