Class: Redistat::Label

Inherits:
Object
  • Object
show all
Includes:
Database, Options
Defined in:
lib/redistat/label.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

included, #options, #parse_options, #raw_options

Methods included from Database

#db, included

Constructor Details

#initialize(str, opts = {}) ⇒ Label

Returns a new instance of Label.



19
20
21
22
# File 'lib/redistat/label.rb', line 19

def initialize(str, opts = {})
  parse_options(opts)
  @raw = str.to_s
end

Class Method Details

.create(name, opts = {}) ⇒ Object



10
11
12
# File 'lib/redistat/label.rb', line 10

def self.create(name, opts = {})
  self.new(name, opts).save
end

.join(*args) ⇒ Object



14
15
16
17
# File 'lib/redistat/label.rb', line 14

def self.join(*args)
  args = args.map {|i| i.to_s}
  self.new(args.reject {|i| i.blank? }.join(Redistat.group_separator))
end

Instance Method Details

#default_optionsObject



6
7
8
# File 'lib/redistat/label.rb', line 6

def default_options
  { :hashed_label => false }
end

#groupsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redistat/label.rb', line 54

def groups
  return @groups unless @groups.nil?
  @groups = []
  parent = ""
  self.to_s.split(Redistat.group_separator).each do |part|
    if !part.blank?
      group = ((parent.blank?) ? "" : "#{parent}#{Redistat.group_separator}") + part
      @groups << Label.new(group)
      parent = group
    end
  end
  @groups.reverse!
end

#hashObject



32
33
34
# File 'lib/redistat/label.rb', line 32

def hash
  @hash ||= Digest::SHA1.hexdigest(self.to_s)
end

#meObject



50
51
52
# File 'lib/redistat/label.rb', line 50

def me
  self.to_s.split(Redistat.group_separator).last
end

#nameObject



28
29
30
# File 'lib/redistat/label.rb', line 28

def name
  @options[:hashed_label] ? hash : self.to_s
end

#parentObject



46
47
48
# File 'lib/redistat/label.rb', line 46

def parent
  @parent ||= groups[1] if groups.size > 1
end

#saveObject



36
37
38
39
# File 'lib/redistat/label.rb', line 36

def save
  @saved = db.hset(KEY_LABELS, hash, self.to_s) if @options[:hashed_label]
  self
end

#saved?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/redistat/label.rb', line 41

def saved?
  return true unless @options[:hashed_label]
  @saved ||= false
end

#to_sObject



24
25
26
# File 'lib/redistat/label.rb', line 24

def to_s
  @raw
end