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

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

Instance Method Summary collapse

Instance Method Details

#after_destroyObject

model hook



67
68
69
70
# File 'lib/dcmgr/models/base_new.rb', line 67

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

#after_initializeObject



60
61
62
63
64
# File 'lib/dcmgr/models/base_new.rb', line 60

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”.



74
75
76
# File 'lib/dcmgr/models/base_new.rb', line 74

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’)



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

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_api_documentObject

generate API response document. similar to to_hash() but not to expose integer primary key.



139
140
141
# File 'lib/dcmgr/models/base_new.rb', line 139

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

#to_hashObject



133
134
135
# File 'lib/dcmgr/models/base_new.rb', line 133

def to_hash()
  self.values.dup.merge({:id=>self.id, :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’)



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

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.



56
57
58
# File 'lib/dcmgr/models/base_new.rb', line 56

def uuid_prefix
  self.class.uuid_prefix
end