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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 927b2e38bdfad7feb263cfdcaf83309ec84eba6a
4
- data.tar.gz: 6fe541621b542f6f422a23e84cb8d3e2ae64e69c
3
+ metadata.gz: 735689e5d94123b058f3099c06d7cadec9db9864
4
+ data.tar.gz: abc86482d07f616384e54cbd0d8ffd4d3926a759
5
5
  SHA512:
6
- metadata.gz: 0a3c5c70b86493937fb00725c2bf2470036c53930117f9e933227b12b42e75d5798d5bd2550eb157d012eafe307a3d9f717b56e79fb61ab13ad08457d4e3f5eb
7
- data.tar.gz: 0d0d53d421600cc260a4317adb6447ad014cc3b2d64e2c3aa57508af9fbf23a0da9de85b0bab941cd1dbd4b661400f37209a36a3d981c3c9cfd4f9d19b3c68b2
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
 
@@ -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, send(association), limit, period)
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
@@ -1,3 +1,3 @@
1
1
  module ManaPotion
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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-30 00:00:00.000000000 Z
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.7
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