simple_recommender 0.0.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de68a178eb1100f0d5245a71d0d2af980c462068
4
- data.tar.gz: 7a79d4fc80f4926e1a255ac3d9b77bc0f1c9e910
3
+ metadata.gz: a2cb3b1f905c25ef005cea8efb18ac33fd8247f6
4
+ data.tar.gz: 9bd804d22da171f530d108cd5f8f38ebe5c219b1
5
5
  SHA512:
6
- metadata.gz: c31df3ffca819945788ff30a808413caf7ffc858b514748982dbb932c0a9077f73b91d30fc78b736cbd5b9f6bf69506f93cb6809f5d4b5be893ff3bcd3324e35
7
- data.tar.gz: 481725920d711a3e1784fbf5e59fa90f54b205af08c7c0f87a835086394fe1481b82b6fd55999cba344c9b0f389e4c6cf24b2280f39d4062d62772d4adf35be1
6
+ metadata.gz: 373fd505ef35d570b7b36db8bbfdae06b96462a411ee8721fcf763adf528d2983f5e411b93babb987cdffa71627368166d20e3a8e841aaae61c44313acbc426c
7
+ data.tar.gz: 0d537aadd70ad398f1e28d61cc282f74d202750f10f07e9b6f965fe5aaf3e7a8126c7c2ff7f8330836cfa32632c938eabd64909bf9f1cbbfe7a877511fe5057a
@@ -1,32 +1,46 @@
1
1
  module SimpleRecommender
2
2
  module Recommendable
3
3
  extend ActiveSupport::Concern
4
+ DEFAULT_N_RESULTS = 10
5
+ SIMILARITY_KEY = "similarity" # todo: allow renaming to avoid conflicts
6
+ AssociationMetadata = Struct.new(:join_table, :foreign_key, :association_foreign_key)
4
7
 
5
- included do
6
- SIMILARITY_KEY = "similarity" # todo: allow renaming to avoid conflicts
7
- DEFAULT_N_RESULTS = 10
8
-
9
- def similar_by(association_name, n_results: DEFAULT_N_RESULTS)
10
- query = similar_query(
11
- association_name: association_name,
12
- n_results: n_results
13
- )
14
-
15
- self.class.find_by_sql(query)
8
+ module ClassMethods
9
+ def similar_by(association_name)
10
+ define_method :similar_items do |n_results: DEFAULT_N_RESULTS|
11
+ query = similar_query(
12
+ association_name: association_name,
13
+ n_results: n_results
14
+ )
15
+ self.class.find_by_sql(query)
16
+ end
16
17
  end
18
+ end
17
19
 
18
- # For each applicable association, add a dynamically named shortcut method
19
- # e.g. when basing similarity on users, define :similar_by_users
20
- self.reflect_on_all_associations.each do |association|
21
- association_name = association.name
20
+ included do
21
+ private
22
22
 
23
- define_method "similar_by_#{association_name}" do |n_results: DEFAULT_N_RESULTS|
24
- similar_by(association_name, n_results: n_results)
23
+ # Returns database metadata about an association based on its type,
24
+ # used in constructing a similarity query based on that association
25
+ def association_metadata(reflection)
26
+ case reflection
27
+ when ActiveRecord::Reflection::HasAndBelongsToManyReflection
28
+ AssociationMetadata.new(
29
+ reflection.join_table,
30
+ reflection.foreign_key,
31
+ reflection.association_foreign_key
32
+ )
33
+ when ActiveRecord::Reflection::ThroughReflection
34
+ AssociationMetadata.new(
35
+ reflection.through_reflection.table_name,
36
+ reflection.through_reflection.foreign_key,
37
+ reflection.association_foreign_key
38
+ )
39
+ else
40
+ raise ArgumentError, "Association '#{reflection.name}' is not a supported type"
25
41
  end
26
42
  end
27
43
 
28
- private
29
-
30
44
  # Returns a Postgres query that can be executed to return similar items.
31
45
  # Reflects on the association to get relevant table names, and then
32
46
  # uses Postgres's integer array intersection/union operators to
@@ -63,29 +77,6 @@ module SimpleRecommender
63
77
  ORDER BY similarity DESC;
64
78
  SQL
65
79
  end
66
-
67
- # Returns database metadata about an association based on its type,
68
- # used in constructing a similarity query based on that association
69
- def association_metadata(reflection)
70
- case reflection
71
- when ActiveRecord::Reflection::HasAndBelongsToManyReflection
72
- AssociationMetadata.new(
73
- reflection.join_table,
74
- reflection.foreign_key,
75
- reflection.association_foreign_key
76
- )
77
- when ActiveRecord::Reflection::ThroughReflection
78
- AssociationMetadata.new(
79
- reflection.through_reflection.table_name,
80
- reflection.through_reflection.foreign_key,
81
- reflection.association_foreign_key
82
- )
83
- else
84
- raise ArgumentError, "Association '#{reflection.name}' is not a supported type"
85
- end
86
- end
87
-
88
- AssociationMetadata = Struct.new(:join_table, :foreign_key, :association_foreign_key)
89
80
  end
90
81
  end
91
82
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleRecommender
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,6 @@
1
1
  class Book < ActiveRecord::Base
2
+ include SimpleRecommender::Recommendable
3
+
2
4
  has_and_belongs_to_many :tags
3
5
 
4
6
  has_many :likes
@@ -6,5 +8,5 @@ class Book < ActiveRecord::Base
6
8
 
7
9
  belongs_to :author, class_name: "User"
8
10
 
9
- include SimpleRecommender::Recommendable
11
+ similar_by :users
10
12
  end