Class: Commendo::MySqlBacked::TagSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_base) ⇒ TagSet

Returns a new instance of TagSet.



7
8
9
10
11
# File 'lib/commendo/mysql-backed/tag_set.rb', line 7

def initialize(key_base)
  config_hash = Commendo.config.to_hash
  @mysql = Mysql2::Client.new(config_hash)
  @key_base = key_base
end

Instance Attribute Details

#key_baseObject

Returns the value of attribute key_base.



5
6
7
# File 'lib/commendo/mysql-backed/tag_set.rb', line 5

def key_base
  @key_base
end

#mysqlObject

Returns the value of attribute mysql.



5
6
7
# File 'lib/commendo/mysql-backed/tag_set.rb', line 5

def mysql
  @mysql
end

Instance Method Details

#add(resource, *tags) ⇒ Object



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

def add(resource, *tags)
  return if tags.empty?
  @mysql.transaction do |client|
    insert_tags(resource, tags)
  end
end

#delete(resource, *tags) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/commendo/mysql-backed/tag_set.rb', line 44

def delete(resource, *tags)
  if tags.empty?
    delete_all_tags_prepared_query.execute(@key_base, resource)
  else
    tags.each { |t| delete_tags_prepared_query.execute(@key_base, resource, t) }
  end
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/commendo/mysql-backed/tag_set.rb', line 13

def empty?
  result = empty_prepared_query.execute(@key_base)
  result.count.zero?
end

#get(resource) ⇒ Object



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

def get(resource)
  result = get_tags_prepared_query.execute(@key_base, resource)
  result.map { |r| r['tag'] }
end

#matches(resource, include, exclude = []) ⇒ Object



37
38
39
40
41
42
# File 'lib/commendo/mysql-backed/tag_set.rb', line 37

def matches(resource, include, exclude = [])
  resource_tags = get(resource)
  can_include = include.nil? || include.empty? || (resource_tags & include).length > 0
  should_exclude = !exclude.nil? && !exclude.empty? && (resource_tags & exclude).length > 0
  return can_include && !should_exclude
end

#set(resource, *tags) ⇒ Object



30
31
32
33
34
35
# File 'lib/commendo/mysql-backed/tag_set.rb', line 30

def set(resource, *tags)
  @mysql.transaction do |client|
    delete(resource)
    insert_tags(resource, tags) unless tags.empty?
  end
end