Class: Commendo::MySqlBacked::ContentSet

Inherits:
Object
  • Object
show all
Defined in:
lib/commendo/mysql-backed/content_set.rb

Constant Summary collapse

DEFAULT_LIMIT =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_base, tag_set = nil) ⇒ ContentSet

Returns a new instance of ContentSet.



10
11
12
13
14
15
16
# File 'lib/commendo/mysql-backed/content_set.rb', line 10

def initialize(key_base, tag_set = nil)
  config_hash = Commendo.config.to_hash
  config_hash[:flags] = Mysql2::Client::MULTI_STATEMENTS
  @mysql = Mysql2::Client.new(config_hash)
  @key_base = key_base
  @tag_set = tag_set
end

Instance Attribute Details

#key_baseObject

Returns the value of attribute key_base.



6
7
8
# File 'lib/commendo/mysql-backed/content_set.rb', line 6

def key_base
  @key_base
end

#mysqlObject

Returns the value of attribute mysql.



6
7
8
# File 'lib/commendo/mysql-backed/content_set.rb', line 6

def mysql
  @mysql
end

#tag_setObject

Returns the value of attribute tag_set.



6
7
8
# File 'lib/commendo/mysql-backed/content_set.rb', line 6

def tag_set
  @tag_set
end

Instance Method Details

#add(resource, *groups) ⇒ Object



23
24
25
26
27
# File 'lib/commendo/mysql-backed/content_set.rb', line 23

def add(resource, *groups)
  groups.map! { |g| g.is_a?(Array) ? g : [g, 1] } #sets default score of 1
  query = add_single_prepared_query
  groups.each { |(g, s)| query.execute(@key_base, resource, g, s, s) }
end

#add_and_calculate(resource, *groups) ⇒ Object



33
34
35
# File 'lib/commendo/mysql-backed/content_set.rb', line 33

def add_and_calculate(resource, *groups)
  add(resource, *groups)
end

#add_by_group(group, *resources) ⇒ Object



18
19
20
21
# File 'lib/commendo/mysql-backed/content_set.rb', line 18

def add_by_group(group, *resources)
  resources.map! { |r| r.is_a?(Array) ? r : [r, 1] } #sets default score of 1
  resources.each { |r| add_single(r[0], group, r[1]) }
end

#add_single(resource, group, score) ⇒ Object



29
30
31
# File 'lib/commendo/mysql-backed/content_set.rb', line 29

def add_single(resource, group, score)
  add(resource, [group, score])
end

#calculate_similarity(threshold = nil) ⇒ Object



45
46
47
48
# File 'lib/commendo/mysql-backed/content_set.rb', line 45

def calculate_similarity(threshold = nil)
  threshold = nil if threshold == 0
  @threshold = threshold
end

#calculate_similarity_for_resource(resource, threshold = 0) ⇒ Object



50
51
# File 'lib/commendo/mysql-backed/content_set.rb', line 50

def calculate_similarity_for_resource(resource, threshold = 0)
end

#delete(resource) ⇒ Object



41
42
43
# File 'lib/commendo/mysql-backed/content_set.rb', line 41

def delete(resource)
  delete_prepared_query.execute(@key_base, resource)
end

#filtered_similar_to(resource, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/commendo/mysql-backed/content_set.rb', line 63

def filtered_similar_to(resource, options = {})
  if @tag_set.nil? || (options[:include].nil? && options[:exclude].nil?) || @tag_set.empty?
    return similar_to(resource, options[:limit] || DEFAULT_LIMIT)
  else
    similar = similar_to(resource)
    limit = options[:limit] || similar.length
    filtered = []
    similar.each do |s|
      return filtered if filtered.length >= limit
      filtered << s if @tag_set.matches(s[:resource], options[:include], options[:exclude])
    end
    return filtered
  end
end

#groups(resource) ⇒ Object



37
38
39
# File 'lib/commendo/mysql-backed/content_set.rb', line 37

def groups(resource)
  groups_prepared_query.execute(@key_base, resource).map { |r| r['groupname'] }
end

#remove_from_groups(resource, *groups) ⇒ Object



78
79
80
# File 'lib/commendo/mysql-backed/content_set.rb', line 78

def remove_from_groups(resource, *groups)
  @mysql.query(remove_from_groups_prepared_query(@key_base, resource, groups))
end

#remove_from_groups_and_calculate(resource, *groups) ⇒ Object



82
83
84
85
# File 'lib/commendo/mysql-backed/content_set.rb', line 82

def remove_from_groups_and_calculate(resource, *groups)
  remove_from_groups(resource, *groups)
  calculate_similarity_for_resource(resource)
end

#similar_to(resource, limit = DEFAULT_LIMIT) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/commendo/mysql-backed/content_set.rb', line 53

def similar_to(resource, limit = DEFAULT_LIMIT)
  resource = [resource] unless resource.is_a? Array
  results = @mysql.query(similar_to_query(@key_base, resource, limit)) if @threshold.nil?
  results = @mysql.query(similar_to_with_threshold_query(@key_base, resource, @threshold, limit)) unless @threshold.nil?
  similar = results.map { |r| {resource: r['similar'], similarity: r['similarity'].round(3)} }
  return similar if resource.length == 1
  grouped = similar.group_by { |r| r[:resource] }
  grouped.map { |resource, similar| {resource: resource, similarity: similar.inject(0.0) { |sum, s| sum += s[:similarity] }} }.sort_by { |h| [h[:similarity], h[:resource]] }.reverse
end