road_to_el_duration 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -1
- data/lib/road_to_el_duration/calculations.rb +4 -2
- data/lib/road_to_el_duration/version.rb +1 -1
- data/lib/road_to_el_duration.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d0a0614c1d994dc0147a886302cb7b1ba5c07af58f36fdfc695618233cade40
|
4
|
+
data.tar.gz: bd8abb6d4dcc62e9071a719f00517938e2d5e1c1df3f2d0646392d09d122b506
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9100463c5e7a10d7105cc2609d9fd1bdd01a7c612394c6c87fffd96a53298f8bb4d8f62dc6835114ef28305e6e734ebf5c3b485ff47548ac85b02f904ecc7614
|
7
|
+
data.tar.gz: afc0bcbe9b437df54775386c55c2cf61d3722f35931f0c6bd72ea6765652033706db896e635563ad9b79b90edbc54cbdaed2a9c388db1f2e44c1f3d3201663e6
|
data/README.md
CHANGED
@@ -14,6 +14,15 @@ ActiveSupport::Duration class and the Ruby Integer class. This is an opt-in
|
|
14
14
|
feature so you don't have to use it if you don't want.
|
15
15
|
|
16
16
|
## Usage
|
17
|
+
|
18
|
+
The gem provides two modules that you can include in your models. One named
|
19
|
+
`RoadToElDuration::Calculations` which provides the `calculates_duration_from`
|
20
|
+
and `updates_duration_of` methods and the other named `RoadToElDuration::Coder`
|
21
|
+
which provides the `coder` class that is used to serialize and deserialize the
|
22
|
+
duration columns.
|
23
|
+
|
24
|
+
Let's look at a simple example.
|
25
|
+
|
17
26
|
```ruby
|
18
27
|
class Episode < ApplicationRecord
|
19
28
|
include RoadToElDuration::Calculations
|
@@ -35,6 +44,44 @@ class Series < ApplicationRecord
|
|
35
44
|
end
|
36
45
|
```
|
37
46
|
|
47
|
+
Here we have two models, `Episode` and `Series`. The `Series` model has its
|
48
|
+
`duration` column serialized with the `RoadToElDuration::DurationCoder` coder and
|
49
|
+
the `Episode` model has its `duration` column serialized with the
|
50
|
+
`RoadToElDuration::DurationCoder` coder as well. The `Series` model also
|
51
|
+
has its `duration` attribute populated from the sum of episode `duration` that
|
52
|
+
belongs to a series.
|
53
|
+
|
54
|
+
This setup assumes that both the `Episode` and `Series` models have an integer
|
55
|
+
column on their tables named `duration` that is used to store the duration in
|
56
|
+
seconds.
|
57
|
+
|
58
|
+
If your columns are named differently you can specify the column names in both
|
59
|
+
the `calculates_duration_from` and `updates_duration_of` methods by passing
|
60
|
+
the column name as a symbol or a string as the `column` keyword argument.
|
61
|
+
|
62
|
+
For example:
|
63
|
+
```ruby
|
64
|
+
class Episode < ApplicationRecord
|
65
|
+
include RoadToElDuration::Calculations
|
66
|
+
|
67
|
+
belongs_to :series
|
68
|
+
|
69
|
+
serialize :duration, coder: RoadToElDuration::DurationCoder
|
70
|
+
updates_duration_of :series, column: :total_length
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
class Series < ApplicationRecord
|
75
|
+
include RoadToElDuration::Calculations
|
76
|
+
|
77
|
+
has_many :episodes
|
78
|
+
|
79
|
+
serialize :duration, coder: RoadToElDuration::DurationCoder
|
80
|
+
calculates_duration_from :episodes, column: :length
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
|
38
85
|
## Installation
|
39
86
|
Add this line to your application's Gemfile:
|
40
87
|
|
@@ -47,7 +94,7 @@ And then execute:
|
|
47
94
|
$ bundle
|
48
95
|
```
|
49
96
|
## Contributing
|
50
|
-
|
97
|
+
If you would like to contribute something, go for it. 👍
|
51
98
|
|
52
99
|
## License
|
53
100
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -26,14 +26,16 @@ module RoadToElDuration
|
|
26
26
|
# to calculate the duration.
|
27
27
|
#
|
28
28
|
def calculates_duration_from(child_association_name, column: :duration)
|
29
|
+
raise ArgumentError, "Please pass a valid child_association argument to the #{__method__} instead of nil" if child_association_name.nil?
|
29
30
|
self.child_association_name= child_association_name
|
30
31
|
self.child_duration_column = column
|
31
32
|
end
|
32
33
|
|
33
34
|
# This class method is used to set the parent association that will be updated.
|
34
35
|
#
|
35
|
-
def updates_duration_of(
|
36
|
-
|
36
|
+
def updates_duration_of(parent_association_name, column: :duration)
|
37
|
+
raise ArgumentError, "Please pass a valid parent_association argument to the #{__method__} instead of nil" if parent_association_name.nil?
|
38
|
+
self.parent_association_name = parent_association_name
|
37
39
|
self.parent_duration_column = column
|
38
40
|
end
|
39
41
|
end
|
data/lib/road_to_el_duration.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: road_to_el_duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Collin Jilbert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|