Class: Dcmgr::Models::Tag

Inherits:
BaseNew
  • Object
show all
Defined in:
lib/dcmgr/models/tag.rb

Defined Under Namespace

Classes: TagAlreadyLabeled, TagAlreadyUnlabeled, UnacceptableTagType

Constant Summary collapse

TYPE_NORMAL =
0
TYPE_AUTH =
1

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseNew

Proxy, dataset, install_data, install_data_hooks, lock!, unlock!

Class Method Details

.description(desc = nil) ⇒ Object

Set or read description of the Tag class.



103
104
105
106
107
108
# File 'lib/dcmgr/models/tag.rb', line 103

def description(desc=nil)
  if desc
    @description = desc
  end
  @description
end

.find_tag_class(name) ⇒ Object

one_to_many :tag_attributes, :one_to_one=>true



75
76
77
78
79
# File 'lib/dcmgr/models/tag.rb', line 75

def self.find_tag_class(name)
  self.subclasses.find { |m|
    m.to_s.sub(/^#{self.class}::/, '') == name
  }
end

.type_id(type_id = nil) ⇒ Object

Declare the integer number for the tag.

Also set the value to sti map in class Tag. class Tag1 < Tag

type_id 123456

end

puts Tag1.type_id # == 123456



92
93
94
95
96
97
98
99
# File 'lib/dcmgr/models/tag.rb', line 92

def type_id(type_id=nil)
  if type_id.is_a?(Fixnum)
    @type_id = type_id
    Tag.sti_model_map[type_id] = self
    Tag.sti_key_map[self.to_s] = type_id
  end
  @type_id || raise("#{self}.type_id is unset. Please set the unique number for the tag instance.")
end

Instance Method Details

#accept_mapping?(taggable_obj) ⇒ Boolean

Check the object class type before associating to the Tag. Child class must implement this method.

Parameters:

  • taggable_obj

    any object kind_of?(Model::Taggable)

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


115
116
117
# File 'lib/dcmgr/models/tag.rb', line 115

def accept_mapping?(taggable_obj)
  raise NotImplementedError 
end

#after_initializeObject



119
120
121
122
# File 'lib/dcmgr/models/tag.rb', line 119

def after_initialize
  super
  self[:type_id] = self.class.type_id
end

#label(canonical_uuid) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dcmgr/models/tag.rb', line 54

def label(canonical_uuid)
  tgt = Taggable.find(canonical_uuid)
  
  raise(UnacceptableTagType, self, tgt) if accept_mapping?(tgt)
  raise(TagAlreadyLabeled) if labeled?(canonical_uuid)
  TagMapping.create(:uuid=>canonical_uuid, :tag_id=>self.pk)
  self
end

#labeled?(canonical_uuid) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/dcmgr/models/tag.rb', line 48

def labeled?(canonical_uuid)
  # TODO: check if the uuid exists
  
  !TagMapping.filter(:uuid=>canonical_uuid, :tag_id=>self.pk).empty?
end

#unlabel(canonical_uuid) ⇒ Object



63
64
65
66
67
# File 'lib/dcmgr/models/tag.rb', line 63

def unlabel(canonical_uuid)
  t = TagMapping.find(:uuid=>canonical_uuid, :tag_id=>self.pk) || raise(TagAlreadyUnlabeled)
  t.delete
  self
end