attribute-depends-calculator 0.1 → 0.2
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/.coveralls.yml +1 -0
- data/.gitignore +1 -1
- data/README.md +71 -4
- data/bin/console +2 -2
- data/lib/attribute_depends_calculator/factory.rb +18 -10
- data/lib/attribute_depends_calculator/macro.rb +4 -1
- data/lib/attribute_depends_calculator/parameter.rb +35 -0
- data/lib/attribute_depends_calculator/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f26cc6d17eed91fc793b014e4bdf1fc3a58a8c77
|
4
|
+
data.tar.gz: bb121079814fbfccad2f3b8239504d3d2d8b152f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2a04601664feb8771c9d2db48d9aa674675f78875a4c97e918a701af9d1074fe207a5dae6d1aa9257fad95696b5f227e77d396ea56d3105f949895e60128307
|
7
|
+
data.tar.gz: c104cdd193bf0b0d6519b8ce500eb2b8bb7be8b9b5d9521f54d26ba2aea743e1604fe2d17c7b7fe3e2e1b6467ea1186c2a0f47872eb4059d1beac20001b8a7d5
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
# Attribute Depends Calculator
|
1
|
+
# Attribute Depends Calculator
|
2
|
+
[](https://travis-ci.org/falm/attribute-depends-calculator) [](https://coveralls.io/github/falm/attribute-depends-calculator?branch=master) [](https://codeclimate.com/github/falm/attribute-depends-calculator) [](https://gemnasium.com/github.com/falm/attribute-depends-calculator) [](https://badge.fury.io/rb/attribute-depends-calculator)
|
2
3
|
|
3
|
-
The scenario of the gem is when you have
|
4
|
+
The scenario of the gem is when you have an attribute on model that value depends of a calculation of other model's attribute which attribute's model related. AttributeDependsCalculator will help you solve the case
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -19,7 +20,7 @@ Assume you have model order and order-item
|
|
19
20
|
```ruby
|
20
21
|
class Order < ActiveRecord::Base
|
21
22
|
has_many :order_items
|
22
|
-
|
23
|
+
depend total_price: {order_items: :price}
|
23
24
|
end
|
24
25
|
|
25
26
|
class OrderItem < ActiveRecord::Base
|
@@ -38,7 +39,73 @@ order_item.update(price: 100)
|
|
38
39
|
order.reload.total_price
|
39
40
|
#=> 150.0
|
40
41
|
```
|
41
|
-
As above price of order automatically update when whatever order_items changes
|
42
|
+
As above show the price of order automatically update when whatever order_items changes
|
43
|
+
|
44
|
+
## Advance
|
45
|
+
|
46
|
+
The options **operator** had two cateogries of value, the default value of the gem is expression **sum**
|
47
|
+
|
48
|
+
#### Operation
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class Order < ActiveRecord::Base
|
52
|
+
has_many :order_items
|
53
|
+
depend total_price: {order_items: :price, operator: :+} # or :*
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
#### Expression
|
58
|
+
|
59
|
+
The following expression can be use to calculate the collection of depends attributes
|
60
|
+
|
61
|
+
**sum**
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class Order < ActiveRecord::Base
|
65
|
+
...
|
66
|
+
depend total_price: {order_items: :price, operator: :sum} # default
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
**average**
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class Order < ActiveRecord::Base
|
74
|
+
...
|
75
|
+
depend avg_price: {order_items: :price, operator: :average}
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
**count**
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class Order < ActiveRecord::Base
|
83
|
+
...
|
84
|
+
depend order_items_count: {order_items: :price, operator: :count}
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
**minimum**
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class Order < ActiveRecord::Base
|
92
|
+
...
|
93
|
+
depend min_price: {order_items: :price, operator: :minimum}
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
**maximum**
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class Order < ActiveRecord::Base
|
101
|
+
...
|
102
|
+
depend max_price: {order_items: :price, operator: :maximum}
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
42
109
|
|
43
110
|
## Contributing
|
44
111
|
|
data/bin/console
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
|
-
require
|
5
|
-
|
4
|
+
require 'rails/all'
|
5
|
+
require "attribute-depends-calculator"
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
@@ -1,9 +1,18 @@
|
|
1
1
|
module AttributeDependsCalculator
|
2
2
|
class Factory
|
3
|
-
|
3
|
+
|
4
|
+
attr_accessor :klass, :column, :association
|
5
|
+
|
6
|
+
attr_accessor :parameter
|
7
|
+
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :parameter, :depend_association_name, :depend_column, :expression
|
11
|
+
|
4
12
|
def initialize(klass, column, params)
|
5
13
|
self.klass, self.column = klass, column
|
6
|
-
self.
|
14
|
+
self.parameter = Parameter.new(params)
|
15
|
+
self.association = fetch_association
|
7
16
|
end
|
8
17
|
|
9
18
|
def perform
|
@@ -14,9 +23,9 @@ module AttributeDependsCalculator
|
|
14
23
|
private
|
15
24
|
|
16
25
|
def define_calculator
|
17
|
-
self.klass.class_eval <<-METHOD, __FILE__, __LINE__
|
26
|
+
self.klass.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
18
27
|
def #{calculate_method_name}
|
19
|
-
total = self.#{
|
28
|
+
total = self.#{depend_association_name}.#{expression}
|
20
29
|
update(:#{column} => total)
|
21
30
|
end
|
22
31
|
METHOD
|
@@ -27,12 +36,11 @@ module AttributeDependsCalculator
|
|
27
36
|
end
|
28
37
|
|
29
38
|
def klass_assoc_name
|
30
|
-
klass.table_name.
|
39
|
+
@assoc_name ||= association.reflect_on_all_associations.find {|assoc| assoc.plural_name == klass.table_name}.name
|
31
40
|
end
|
32
41
|
|
33
|
-
def fetch_association
|
34
|
-
|
35
|
-
[klass.reflect_on_association(assoc).class_name.constantize, params.values.first, assoc]
|
42
|
+
def fetch_association
|
43
|
+
ObjectSpace.const_get klass.reflect_on_association(depend_association_name).class_name
|
36
44
|
end
|
37
45
|
|
38
46
|
def append_callbacks
|
@@ -41,14 +49,14 @@ module AttributeDependsCalculator
|
|
41
49
|
end
|
42
50
|
|
43
51
|
def append_callback_hook
|
44
|
-
self.association.class_eval <<-METHOD, __FILE__, __LINE__
|
52
|
+
self.association.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
45
53
|
after_save :depends_update_#{column}
|
46
54
|
around_destroy :depends_update_#{column}_around
|
47
55
|
METHOD
|
48
56
|
end
|
49
57
|
|
50
58
|
def define_callback_methods
|
51
|
-
self.association.class_eval <<-METHOD, __FILE__, __LINE__
|
59
|
+
self.association.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
52
60
|
private
|
53
61
|
|
54
62
|
def depends_update_#{column}
|
@@ -1,10 +1,13 @@
|
|
1
1
|
module AttributeDependsCalculator
|
2
2
|
module Macro
|
3
|
-
|
3
|
+
|
4
|
+
def attribute_depend(options)
|
4
5
|
options.each do |column, option|
|
5
6
|
Factory.new(self, column, option).perform
|
6
7
|
end
|
7
8
|
end
|
9
|
+
|
10
|
+
alias_method :depend, :attribute_depend
|
8
11
|
end
|
9
12
|
end
|
10
13
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AttributeDependsCalculator
|
2
|
+
class Parameter
|
3
|
+
|
4
|
+
attr_accessor :params, :depend_association_name, :depend_column, :operator
|
5
|
+
|
6
|
+
OPERATORS = %i(+ *)
|
7
|
+
METHODS = %i(sum average count minimum maximum)
|
8
|
+
|
9
|
+
def initialize(params)
|
10
|
+
self.operator = params.values_at(:operator).first || :sum
|
11
|
+
self.params = params
|
12
|
+
fetch
|
13
|
+
end
|
14
|
+
|
15
|
+
def fetch
|
16
|
+
self.depend_association_name = params.keys.first
|
17
|
+
self.depend_column = params.values.first
|
18
|
+
end
|
19
|
+
|
20
|
+
def expression
|
21
|
+
operator_filter
|
22
|
+
end
|
23
|
+
|
24
|
+
def operator_filter
|
25
|
+
if OPERATORS.include? operator
|
26
|
+
"pluck(:#{depend_column}).compact.reduce(0, :#{operator})"
|
27
|
+
elsif METHODS.include? operator
|
28
|
+
"#{operator}(:#{depend_column})"
|
29
|
+
else
|
30
|
+
raise 'The operator of depends calculator are incorrect'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute-depends-calculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Hou
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -60,6 +60,7 @@ executables: []
|
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
+
- ".coveralls.yml"
|
63
64
|
- ".gitignore"
|
64
65
|
- ".rspec"
|
65
66
|
- ".travis.yml"
|
@@ -74,6 +75,7 @@ files:
|
|
74
75
|
- lib/attribute_depends_calculator/engine.rb
|
75
76
|
- lib/attribute_depends_calculator/factory.rb
|
76
77
|
- lib/attribute_depends_calculator/macro.rb
|
78
|
+
- lib/attribute_depends_calculator/parameter.rb
|
77
79
|
- lib/attribute_depends_calculator/version.rb
|
78
80
|
homepage: https://github.com/falm/attribute-depends-calculator
|
79
81
|
licenses:
|