mana-potion 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/lib/mana-potion.rb +2 -2
- data/lib/mana-potion/check_usage.rb +30 -0
- data/lib/mana-potion/pool.rb +7 -12
- data/lib/mana-potion/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c9af497a3a05274ce139631e4a087e51de2b326
|
4
|
+
data.tar.gz: 1ec2c03a3ce9bb2634d061d8960cbe8b57885f82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c54e21e53dc8664a61520c34f990d601e9faa01d6109619b6b72a793164609d4f8789ac1d860a5d330ee5e2975cd58a020ee85d322c57db6ec3b6ca1cc035250
|
7
|
+
data.tar.gz: f9db133e5d0ce04186e48d4804b1bacff363861b380e7c808ce58394f1ff162ad92880d26ab966592fe95997457504c668f1b52f289d7da846069a0f19165302
|
data/README.md
CHANGED
@@ -28,6 +28,7 @@ You just need to include the `ManaPotion::Pool` module in your model's class and
|
|
28
28
|
```ruby
|
29
29
|
class Post < ActiveRecord::Base
|
30
30
|
include ManaPotion::Pool
|
31
|
+
belongs_to :user
|
31
32
|
|
32
33
|
mana_pool_for :user
|
33
34
|
end
|
@@ -46,11 +47,32 @@ If you want to configure the limit and/or period, just pass them as parameters t
|
|
46
47
|
```ruby
|
47
48
|
class Post < ActiveRecord::Base
|
48
49
|
include ManaPotion::Pool
|
50
|
+
belongs_to :user
|
49
51
|
|
50
52
|
mana_pool_for :user, limit: 10, period: 1.hour
|
51
53
|
end
|
52
54
|
```
|
53
55
|
|
56
|
+
The `limit` and `period` options also accept procs, so you can dynamically define their values:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class Post < ActiveRecord::Base
|
60
|
+
include ManaPotion::Pool
|
61
|
+
belongs_to :user
|
62
|
+
|
63
|
+
mana_pool_for :user, limit: -> { limit }, period: -> { period }
|
64
|
+
|
65
|
+
def limit
|
66
|
+
user.premium? ? 20 : 10
|
67
|
+
end
|
68
|
+
|
69
|
+
def period
|
70
|
+
user.premium? ? 1.hour : 1.day
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
|
54
76
|
## Development
|
55
77
|
|
56
78
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/mana-potion.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
module ManaPotion
|
2
|
+
class CheckUsage
|
3
|
+
attr_reader :model, :owner, :limit, :period
|
4
|
+
|
5
|
+
def initialize(model, owner, limit, period)
|
6
|
+
@model = model
|
7
|
+
@owner = owner
|
8
|
+
@limit = limit
|
9
|
+
@period = period
|
10
|
+
end
|
11
|
+
|
12
|
+
def exceeded?
|
13
|
+
count >= limit
|
14
|
+
end
|
15
|
+
|
16
|
+
def association
|
17
|
+
owner
|
18
|
+
.class
|
19
|
+
.reflect_on_all_associations
|
20
|
+
.detect { |r| r.class_name == model.class.name }
|
21
|
+
end
|
22
|
+
|
23
|
+
def count
|
24
|
+
owner
|
25
|
+
.send(association.name)
|
26
|
+
.where(created_at: period.ago..Time.current)
|
27
|
+
.count
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/mana-potion/pool.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'mana-potion/check_usage'
|
2
|
+
|
1
3
|
module ManaPotion
|
2
4
|
module Pool
|
3
5
|
def self.included(other)
|
@@ -7,19 +9,12 @@ module ManaPotion
|
|
7
9
|
module ClassMethods
|
8
10
|
def mana_pool_for(association, limit: 1, period: 1.day)
|
9
11
|
before_validation do
|
10
|
-
|
11
|
-
|
12
|
-
.class
|
13
|
-
.reflect_on_all_associations
|
14
|
-
.detect { |r| r.class_name == self.class.name }
|
15
|
-
|
16
|
-
count = owner
|
17
|
-
.send(other_side_association.name)
|
18
|
-
.where(created_at: period.ago..Time.current)
|
19
|
-
.count
|
12
|
+
limit = instance_exec &limit if limit.respond_to?(:call)
|
13
|
+
period = instance_exec &period if period.respond_to?(:call)
|
20
14
|
|
21
|
-
|
22
|
-
|
15
|
+
check_usage = ManaPotion::CheckUsage.new(self, send(association), limit, period)
|
16
|
+
if check_usage.exceeded?
|
17
|
+
errors.add(association, :limit, limit: limit, count: check_usage.count)
|
23
18
|
end
|
24
19
|
end
|
25
20
|
end
|
data/lib/mana-potion/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mana-potion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mrodrigues
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- bin/console
|
130
130
|
- bin/setup
|
131
131
|
- lib/mana-potion.rb
|
132
|
+
- lib/mana-potion/check_usage.rb
|
132
133
|
- lib/mana-potion/pool.rb
|
133
134
|
- lib/mana-potion/version.rb
|
134
135
|
- mana-potion.gemspec
|