Class: Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/glue/taggable.rb

Overview

The default Tag implementation. A tag attaches semantics to a given object.

Design

Use camelcase names for tags.

– FIXME: use index and char() instead of String. ++

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Tag

Returns a new instance of Tag.


23
24
25
26
# File 'lib/glue/taggable.rb', line 23

def initialize(name = nil)
  @name = name
  @count = 0
end

Class Method Details

.total_frequency(tags = Tag.all) ⇒ Object

Helper method


82
83
84
# File 'lib/glue/taggable.rb', line 82

def self.total_frequency(tags = Tag.all)
  tags.inject(1) { |total, t| total += t.count }
end

Instance Method Details

#tag(obj) ⇒ Object Also known as: link

Tag an object.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/glue/taggable.rb', line 30

def tag(obj)
  #--
  # FIXME: this does not work as expected :( it alters
  # the @loaded flag in the obj.tags collection without 
  # setting the @members.
  # INVESTIGATE: why this happens!
  #
  # return if obj.tagged_with?(@name)
  # class_name = obj.class.name
  # method_name = class_name.index('::') ? (class_name =~ /.*?\:\:(.*)/; $1) : class_name
  # send(method_name.pluralize.underscore.to_sym) << obj
  #++
  
  unless obj.tagged_with?(name)
    obj.tags << self
    @count += 1
    update_property :count
  end        
end

#taggedObject

Return all tagged objects from all categories.


72
73
74
75
76
77
78
# File 'lib/glue/taggable.rb', line 72

def tagged
  t = []
  self.class.relations.each do |rel|
    t += rel[:target_class].find_with_any_tag(name)
  end
  return t
end

#to_sObject

– gmosx: Extra check, useful for utf-8 names on urls. ++


90
91
92
93
94
95
96
# File 'lib/glue/taggable.rb', line 90

def to_s
  if /^\w+$/ =~ @name
    @name
  else 
    @oid.to_s
  end
end

#untag(obj = nil) ⇒ Object Also known as: unlink

Untags an object. If no object is passed, it just decrements the (reference) count. If the count reaches 0 the tag is deleted (garbage collection).


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/glue/taggable.rb', line 55

def untag(obj = nil)
  if obj
    # TODO: implement me.
  end
  
  @count -= 1
  
  if @count > 0
    update_property :count
  else
    self.delete()
  end
end