mana-potion 0.3.0 → 0.4.0
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 +28 -0
- data/lib/mana-potion/pool.rb +11 -2
- data/lib/mana-potion/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 735689e5d94123b058f3099c06d7cadec9db9864
|
4
|
+
data.tar.gz: abc86482d07f616384e54cbd0d8ffd4d3926a759
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32097819c50541441855dac941b36897d6efebf6e11b4704cfd0ae9517682e800f2c7b55aa31b88213c680eae8dbac3cb3c2a2a98d9839696fc2776a5f0c1a14
|
7
|
+
data.tar.gz: de3d91e9c9925fe8e8f4cf77bf8a1ec9645c14241d7abd0ba5b2154792adba7bb99f1e6e58243fbcae6dc6958dc6c7552e7bbb7ca72effde98c2734a6090f47f
|
data/README.md
CHANGED
@@ -72,6 +72,34 @@ class Post < ActiveRecord::Base
|
|
72
72
|
end
|
73
73
|
```
|
74
74
|
|
75
|
+
Trying to create a `Post` without an `User` will raise an error:
|
76
|
+
```ruby
|
77
|
+
Post.create! # Raises a MissingOwnerError
|
78
|
+
```
|
79
|
+
|
80
|
+
To allow nil value for `User`, include the `allow_nil: true` option:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class Post < ActiveRecord::Base
|
84
|
+
include ManaPotion::Pool
|
85
|
+
belongs_to :user
|
86
|
+
|
87
|
+
mana_pool_for :user, allow_nil: true
|
88
|
+
end
|
89
|
+
|
90
|
+
Post.create! # Doesn't raise any errors
|
91
|
+
```
|
92
|
+
|
93
|
+
If you want to check the user's remaining usages, you may use the `ManaPotion::CheckUsage#remaining` method:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
user = User.create!
|
97
|
+
ManaPotion::CheckUsage.new(Post.new, user, 5, 1.day).remaining # => 5
|
98
|
+
|
99
|
+
user.posts.create!
|
100
|
+
ManaPotion::CheckUsage.new(Post.new, user, 5, 1.day).remaining # => 4
|
101
|
+
```
|
102
|
+
|
75
103
|
|
76
104
|
## Development
|
77
105
|
|
data/lib/mana-potion/pool.rb
CHANGED
@@ -2,17 +2,26 @@ require 'mana-potion/check_usage'
|
|
2
2
|
|
3
3
|
module ManaPotion
|
4
4
|
module Pool
|
5
|
+
class MissingOwnerError < StandardError; end
|
6
|
+
|
5
7
|
def self.included(other)
|
6
8
|
other.extend ClassMethods
|
7
9
|
end
|
8
10
|
|
9
11
|
module ClassMethods
|
10
|
-
def mana_pool_for(association, limit: 1, period: 1.day)
|
12
|
+
def mana_pool_for(association, limit: 1, period: 1.day, allow_nil: false)
|
11
13
|
before_validation do
|
14
|
+
owner = send(association)
|
15
|
+
|
16
|
+
if owner.nil?
|
17
|
+
raise MissingOwnerError, "#{self.class.name} it's missing its #{association}. If you want to allow that, include the allow_nil: true option in your Pool configuration." unless allow_nil
|
18
|
+
next
|
19
|
+
end
|
20
|
+
|
12
21
|
limit = instance_exec &limit if limit.respond_to?(:call)
|
13
22
|
period = instance_exec &period if period.respond_to?(:call)
|
14
23
|
|
15
|
-
check_usage = ManaPotion::CheckUsage.new(self,
|
24
|
+
check_usage = ManaPotion::CheckUsage.new(self, owner, limit, period)
|
16
25
|
if check_usage.exceeded?
|
17
26
|
errors.add(association, :limit, limit: limit, count: check_usage.count)
|
18
27
|
end
|
data/lib/mana-potion/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mana-potion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mrodrigues
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08
|
11
|
+
date: 2015-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
requirements: []
|
155
155
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.4.
|
156
|
+
rubygems_version: 2.4.8
|
157
157
|
signing_key:
|
158
158
|
specification_version: 4
|
159
159
|
summary: Do you need to limit some resource's creation rate? It's simple to do it
|