Module: Dcmgr::Models::Taggable::InstanceMethods

Defined in:
lib/dcmgr/models/base_new.rb

Instance Method Summary collapse

Instance Method Details

#after_destroyObject

model hook



70
71
72
73
# File 'lib/dcmgr/models/base_new.rb', line 70

def after_destroy
  super
  TagMapping.filter(:uuid=>self.canonical_uuid).delete
end

#after_initializeObject



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

def after_initialize
  super
  # set random generated uuid value
  self[:uuid] ||= Array.new(8) do UUID_TABLE[rand(UUID_TABLE.size)]; end.join
end

#canonical_uuidObject Also known as: cuuid

Returns canonicalized uuid which has the form of “#uuid_prefix-uuid”.



77
78
79
# File 'lib/dcmgr/models/base_new.rb', line 77

def canonical_uuid
  "#{self.uuid_prefix}-#{self[:uuid]}"
end

#label_tag(arg1, arg2 = nil, arg3 = nil) ⇒ Object

Put the tag on the object.

lable_tag(‘tag-xxxxx’) t = Tag label_tag(t) label_tag(:NetworkPool, ‘newname1’, ‘account_id’)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dcmgr/models/base_new.rb', line 93

def label_tag(arg1, arg2=nil, arg3=nil)
  tag = case arg1
        when String
          Tag[arg1]
        when Symbol
          acctid = arg3 || self.respond_to?(:account_id) ? self. : raise("Unknown Account ID")
          Dcmgr::Tags.const_get(arg1).find_or_create(:account_id=>acctid, :name=>arg2)
        when Tag
          arg1
        else
          raise ArgumentError, "Invalid type: #{arg1.class}"
        end
  raise "Root Tag class can not be used" unless tag.class < Tag
  tag.label(self.canonical_uuid)
end

#to_hashObject



136
137
138
# File 'lib/dcmgr/models/base_new.rb', line 136

def to_hash()
  self.values.dup.merge({:id=>canonical_uuid, :uuid=>canonical_uuid})
end

#unlabel_tag(arg1, arg2 = nil, arg3 = nil) ⇒ Object

Remove the labeled tag from the object

unlable_tag(‘tag-xxxxx’) t = Tag unlabel_tag(t) unlabel_tag(:NetworkPool, ‘newname1’, ‘account_id’)



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dcmgr/models/base_new.rb', line 120

def unlabel_tag(arg1, arg2=nil, arg3=nil)
  tag = case arg1
        when String
          Tag[arg1]
        when Symbol
          acctid = arg3 || self.respond_to?(:account_id) ? self. : raise("Unknown Account ID")
          Dcmgr::Tags.const_get(arg1).find(:account_id=>acctid, :name=>arg2)
        when Tag
          arg1
        else
          raise ArgumentError, "Invalid type: #{arg1.class}"
        end

  tag.unlabel(self.canonical_uuid)
end

#uuid_prefixObject

read-only instance method to retrieve @uuid_prefix class variable.



59
60
61
# File 'lib/dcmgr/models/base_new.rb', line 59

def uuid_prefix
  self.class.uuid_prefix
end