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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7aab993d01164ab48259918ae024ae2e9ad84cf6eb9d78f9a30fc285cccfec47
4
- data.tar.gz: 86f6f40ad889a5fe3b965993e1f94ba8bfcbd33349d3675a432f2060515ceb53
3
+ metadata.gz: 68b015488db5d54d0fde0ff3dc9661fd4f9a643e303a29a8d8bd2faec11ba6a2
4
+ data.tar.gz: 93cec50e9536db1945fc2accf73407c9be0698a48a911ed5d454e4cd81d3ce1e
5
5
  SHA512:
6
- metadata.gz: 75f3870f0f645bc85df251b9b238454f8d998539ed90c7994b9a485799e746a4529a3c825c91573a1299e3a98b625baf47e5951d74945a55742ab756285e1212
7
- data.tar.gz: f9ea4351abd5f43484bc67d27993d518f8658a16b9361c6e7a99878002677ad24dadd45ac79d4ec0ff49bb9dcbde34097233f9f24d2d2990292c84747f91fb83
6
+ metadata.gz: d03879d1c8a3a795566cf3c9214efb844ab90fa60efefccb779f0a59e76c92dcc58ef4ea42c0ef6d05d5be5c1f766a5d4e595734282ff5ef7671acf60f861d4f
7
+ data.tar.gz: b83de9e702435dbc67bc274170d34a6283075e34b2e3b81db6526dd88810bde1e6ce74fe2881cefc7da66f5ddb46920551da2ae158d6cf65b2e1f77adc99403e
data/README.md CHANGED
@@ -1,44 +1,116 @@
1
1
  # Frecency
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/frecency`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ## Overview
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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
- Add this line to your application's Gemfile:
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
- And then execute:
15
+ Then, run the following command to install the gem:
16
16
 
17
- $ bundle install
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
- Or install it yourself as:
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
- $ gem install frecency
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
- TODO: Write usage instructions here
75
+ After configuring the gem, you can use the `frecency` method to fetch records sorted by frecency:
26
76
 
27
- ## Development
77
+ ### Basic Usage
28
78
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+ To retrieve top 10 products sorted by frecency:
30
80
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+ ```ruby
82
+ top_products = Product.frecency(10)
83
+ ```
32
84
 
33
- ## Contributing
85
+ ### Advanced Usage
34
86
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/frecency. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/frecency/blob/master/CODE_OF_CONDUCT.md).
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
- ## License
103
+ ## Contributing
39
104
 
40
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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
- ## Code of Conduct
114
+ ## License
43
115
 
44
- Everyone interacting in the Frecency project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/frecency/blob/master/CODE_OF_CONDUCT.md).
116
+ This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Binary file
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
@@ -1,3 +1,3 @@
1
1
  module Frecency
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/frecency.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  require "frecency/version"
2
- require 'frecency/configuration'
3
- require 'frecency/frecency_methods'
4
- require 'active_support'
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.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-22 00:00:00.000000000 Z
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: