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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80942d403a27cbfd662010a58bcfa152da5799ce
4
- data.tar.gz: 47cd96ebb4f0f9ef77334e4f3fc7d8454539ae65
3
+ metadata.gz: 9c9af497a3a05274ce139631e4a087e51de2b326
4
+ data.tar.gz: 1ec2c03a3ce9bb2634d061d8960cbe8b57885f82
5
5
  SHA512:
6
- metadata.gz: 0872d4cdd206a859d238b99e1ccca4e908a4eea47654aa75f5d46e2e6589b34265f1a98ef25e609ffbbfd8a5289f08f7be04120132ce8abfdbecb9082ec51960
7
- data.tar.gz: 73004fe76622ecbbd22785e7bd6acfcb0d2dec66fdf185726d70a2e5f438686e3ce285475956adfd3ff8ee573e685b00f6827ed1d102cb33b7540da4157e807b
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.
@@ -1,5 +1,5 @@
1
- require "mana-potion/version"
1
+ require 'mana-potion/version'
2
+ require 'mana-potion/pool'
2
3
 
3
4
  module ManaPotion
4
- # Your code goes here...
5
5
  end
@@ -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
@@ -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
- owner = send(association)
11
- other_side_association = owner
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
- if count >= limit
22
- errors.add(association, :limit, limit: limit, count: count)
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
@@ -1,3 +1,3 @@
1
1
  module ManaPotion
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.0
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