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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7af153f66450cb6df34f81701dc3cd967bdc51c4
4
- data.tar.gz: 2b0899d05b5a3dfa6d3166631eab8299548aaca3
3
+ metadata.gz: e450c5a1540f74c95fa6da6c5fc675b93b38272f
4
+ data.tar.gz: 077515cf12e15f52c1fd65ec5c6c0d13933e2925
5
5
  SHA512:
6
- metadata.gz: 3b33a0f054d66a70805b00e562a4784048089cdba366e5b9e5c78af2c82aa40550cb477ae99a49f8c82f98a655b6082de375b569a9b3aa3433c5ec4476d381a3
7
- data.tar.gz: d8dd767fa5548e1fccad3f5f9c629ec63923c8ce399e673318dcda48002a3280c45c04665f71f78222713fc019fa7079e1767b75dbdf050957a07b9e508c5930
6
+ metadata.gz: 1c7cab82212758d6722e60b6ee310ccb7dc50632aeed7e9a8d5992746642e2572e95f8fd39afbd90811246ca6e8693e9562e1a73a00f5d8d524259d98a044311
7
+ data.tar.gz: 64d5c21ce51dcffbcbf08866735c9489ddc09e4a621c7b4540b60bae2122504e6bccf2bcfa43240f6148673e8bad08230807d520c71b0e9c5ec58ae64ca81760
@@ -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 < limit
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allowed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tristan Dunn