frecency 0.1.1 → 0.1.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/README.md +89 -17
- data/frecency-0.1.0.gem +0 -0
- data/frecency-0.1.1.gem +0 -0
- data/lib/frecency/configuration.rb +11 -0
- data/lib/frecency/frecency_methods.rb +40 -0
- data/lib/frecency/version.rb +1 -1
- data/lib/frecency.rb +6 -4
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 68b015488db5d54d0fde0ff3dc9661fd4f9a643e303a29a8d8bd2faec11ba6a2
|
|
4
|
+
data.tar.gz: 93cec50e9536db1945fc2accf73407c9be0698a48a911ed5d454e4cd81d3ce1e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d03879d1c8a3a795566cf3c9214efb844ab90fa60efefccb779f0a59e76c92dcc58ef4ea42c0ef6d05d5be5c1f766a5d4e595734282ff5ef7671acf60f861d4f
|
|
7
|
+
data.tar.gz: b83de9e702435dbc67bc274170d34a6283075e34b2e3b81db6526dd88810bde1e6ce74fe2881cefc7da66f5ddb46920551da2ae158d6cf65b2e1f77adc99403e
|
data/README.md
CHANGED
|
@@ -1,44 +1,116 @@
|
|
|
1
1
|
# Frecency
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Overview
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Frecency is a Ruby on Rails gem that provides a way to sort models based on a combination of frequency and recency, also known as "frecency." This allows for more relevant sorting of records by considering both how often and how recently an item has been accessed or updated.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
To install the `frecency` gem, add it to your Gemfile in your Rails application:
|
|
10
10
|
|
|
11
11
|
```ruby
|
|
12
12
|
gem 'frecency'
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
Then, run the following command to install the gem:
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
```sh
|
|
18
|
+
bundle install
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configuration
|
|
22
|
+
|
|
23
|
+
### Initializer Configuration
|
|
24
|
+
|
|
25
|
+
You need to configure your frecency gem in an initializer. Create a file named `frecency.rb` in your `config/initializers` directory:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
# config/initializers/frecency.rb
|
|
29
|
+
Frecency.configure do |config|
|
|
30
|
+
config.default_frequency_column = :views_count # Default column for frequency
|
|
31
|
+
config.default_recency_column = :updated_at # Default column for recency
|
|
32
|
+
config.default_custom_frequency_scope = nil # Default custom frequency scope (optional)
|
|
33
|
+
end
|
|
34
|
+
```
|
|
18
35
|
|
|
19
|
-
|
|
36
|
+
### Model Configuration
|
|
37
|
+
|
|
38
|
+
Next, configure the models that you want to utilize frecency sorting. You can do this by adding the necessary configuration to each model. Here’s how you can configure a model:
|
|
39
|
+
|
|
40
|
+
#### Basic Configuration
|
|
41
|
+
|
|
42
|
+
For basic configuration using default settings:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
class Product < ApplicationRecord
|
|
46
|
+
configure_frecency(
|
|
47
|
+
frequency: :views_count, # Column calculating frequency
|
|
48
|
+
recency: :updated_at # Column calculating recency
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
```
|
|
20
52
|
|
|
21
|
-
|
|
53
|
+
#### Custom Frequency Scope
|
|
54
|
+
|
|
55
|
+
If you need a custom frequency scope, you can specify it:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
class Order < ApplicationRecord
|
|
59
|
+
belongs_to :product
|
|
60
|
+
|
|
61
|
+
configure_frecency(
|
|
62
|
+
frequency: :dynamic, # Use dynamic frequency calculation
|
|
63
|
+
recency: :created_at, # Column calculating recency
|
|
64
|
+
custom_frequency_scope: -> {
|
|
65
|
+
select("#{table_name}.*, COUNT(orders.id) as frequency")
|
|
66
|
+
.joins(:orders)
|
|
67
|
+
.group("#{table_name}.id")
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
```
|
|
22
72
|
|
|
23
73
|
## Usage
|
|
24
74
|
|
|
25
|
-
|
|
75
|
+
After configuring the gem, you can use the `frecency` method to fetch records sorted by frecency:
|
|
26
76
|
|
|
27
|
-
|
|
77
|
+
### Basic Usage
|
|
28
78
|
|
|
29
|
-
|
|
79
|
+
To retrieve top 10 products sorted by frecency:
|
|
30
80
|
|
|
31
|
-
|
|
81
|
+
```ruby
|
|
82
|
+
top_products = Product.frecency(10)
|
|
83
|
+
```
|
|
32
84
|
|
|
33
|
-
|
|
85
|
+
### Advanced Usage
|
|
34
86
|
|
|
35
|
-
|
|
87
|
+
You can define more complex configuration and filtering:
|
|
36
88
|
|
|
89
|
+
```ruby
|
|
90
|
+
class CustomModel < ApplicationRecord
|
|
91
|
+
configure_frecency(
|
|
92
|
+
frequency: :custom_count, # Custom column for frequency
|
|
93
|
+
recency: :last_accessed_at, # Custom column for recency
|
|
94
|
+
custom_frequency_scope: -> {
|
|
95
|
+
select("#{table_name}.*, custom_logic() as frequency")
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
top_records = CustomModel.frecency(20)
|
|
101
|
+
```
|
|
37
102
|
|
|
38
|
-
##
|
|
103
|
+
## Contributing
|
|
39
104
|
|
|
40
|
-
|
|
105
|
+
1. Fork the repository on GitHub.
|
|
106
|
+
2. Clone your forked repository.
|
|
107
|
+
3. Create a new branch for your feature or bug fix.
|
|
108
|
+
4. Write tests for your feature or bug fix.
|
|
109
|
+
5. Make your changes.
|
|
110
|
+
6. Run RSpec tests (or your preferred test suite).
|
|
111
|
+
7. Commit your changes and push to your fork.
|
|
112
|
+
8. Create a pull request on GitHub.
|
|
41
113
|
|
|
42
|
-
##
|
|
114
|
+
## License
|
|
43
115
|
|
|
44
|
-
|
|
116
|
+
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/frecency-0.1.0.gem
ADDED
|
Binary file
|
data/frecency-0.1.1.gem
ADDED
|
Binary file
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Frecency
|
|
2
|
+
class Configuration
|
|
3
|
+
attr_accessor :default_frequency_column, :default_recency_column, :default_custom_frequency_scope
|
|
4
|
+
|
|
5
|
+
def initialize
|
|
6
|
+
@default_frequency_column = :views
|
|
7
|
+
@default_recency_column = :updated_at
|
|
8
|
+
@default_custom_frequency_scope = nil
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module Frecency
|
|
2
|
+
module FrecencyMethods
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
class_attribute :frecency_frequency_column, instance_accessor: false, default: Frecency.configuration.default_frequency_column
|
|
7
|
+
class_attribute :frecency_recency_column, instance_accessor: false, default: Frecency.configuration.default_recency_column
|
|
8
|
+
class_attribute :frecency_custom_frequency_scope, instance_accessor: false, default: Frecency.configuration.default_custom_frequency_scope
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class_methods do
|
|
12
|
+
def configure_frecency(frequency: nil, recency: nil, custom_frequency_scope: nil)
|
|
13
|
+
self.frecency_frequency_column = frequency || Frecency.configuration.default_frequency_column
|
|
14
|
+
self.frecency_recency_column = recency || Frecency.configuration.default_recency_column
|
|
15
|
+
self.frecency_custom_frequency_scope = custom_frequency_scope || Frecency.configuration.default_custom_frequency_scope
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def frecency(limit = 10)
|
|
19
|
+
frequency_column = frecency_frequency_column
|
|
20
|
+
recency_column = frecency_recency_column
|
|
21
|
+
custom_frequency_scope = frecency_custom_frequency_scope
|
|
22
|
+
|
|
23
|
+
if custom_frequency_scope
|
|
24
|
+
scope = custom_frequency_scope.call
|
|
25
|
+
frequency_column = "frequency"
|
|
26
|
+
else
|
|
27
|
+
scope = where.not("#{recency_column}" => nil).where.not("#{frequency_column}" => nil)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Example frecency query, modify based on your database (PostgreSQL example)
|
|
31
|
+
scope
|
|
32
|
+
.select("#{table_name}.*,
|
|
33
|
+
((log(#{frequency_column} + 1) * 100) +
|
|
34
|
+
(extract(epoch from age(current_timestamp, #{recency_column})) / 86400)) AS frecency_score")
|
|
35
|
+
.order('frecency_score DESC')
|
|
36
|
+
.limit(limit)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/frecency/version.rb
CHANGED
data/lib/frecency.rb
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
require "frecency/version"
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
2
|
+
require "frecency/configuration"
|
|
3
|
+
require "frecency/frecency_methods"
|
|
4
|
+
require "active_support"
|
|
5
5
|
|
|
6
6
|
module Frecency
|
|
7
7
|
class Error < StandardError; end
|
|
8
8
|
|
|
9
|
+
# Initialize configuration
|
|
10
|
+
@configuration = Frecency::Configuration.new
|
|
11
|
+
|
|
9
12
|
def self.configure
|
|
10
|
-
@configuration ||= Frecency::Configuration.new
|
|
11
13
|
yield(@configuration)
|
|
12
14
|
end
|
|
13
15
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: frecency
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Debobroto
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-06-
|
|
11
|
+
date: 2024-06-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: This gem allows Rails models to be sorted based on a combination of how
|
|
14
14
|
frequently and how recently they have been accessed or used.
|
|
@@ -26,8 +26,12 @@ files:
|
|
|
26
26
|
- Rakefile
|
|
27
27
|
- bin/console
|
|
28
28
|
- bin/setup
|
|
29
|
+
- frecency-0.1.0.gem
|
|
30
|
+
- frecency-0.1.1.gem
|
|
29
31
|
- frecency.gemspec
|
|
30
32
|
- lib/frecency.rb
|
|
33
|
+
- lib/frecency/configuration.rb
|
|
34
|
+
- lib/frecency/frecency_methods.rb
|
|
31
35
|
- lib/frecency/version.rb
|
|
32
36
|
homepage: https://github.com/debobroto/frecency
|
|
33
37
|
licenses:
|