Sha256: 2a551052770c44fb4b0a0ee54342297ca710b28bbd2f84bbd183f2e391cb4ce1

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module Redistat
  class Label
    include Database
    include Options
    
    def default_options
      { :hashed_label => false }
    end
    
    def self.create(name, opts = {})
      self.new(name, opts).save
    end
    
    def self.join(*args)
      args = args.map {|i| i.to_s}
      self.new(args.reject {|i| i.blank? }.join(GROUP_SEPARATOR))
    end
    
    def initialize(str, opts = {})
      parse_options(opts)
      @raw = str.to_s
    end
    
    def to_s
      @raw
    end
    
    def name
      @options[:hashed_label] ? hash : self.to_s
    end
    
    def hash
      @hash ||= Digest::SHA1.hexdigest(self.to_s)
    end
    
    def save
      @saved = db.hset(KEY_LABELS, hash, self.to_s) if @options[:hashed_label]
      self
    end
    
    def saved?
      return true unless @options[:hashed_label]
      @saved ||= false
    end
    
    def parent
      @parent ||= groups[1] if groups.size > 1
    end
    
    def me
      self.to_s.split(GROUP_SEPARATOR).last
    end
    
    def groups
      return @groups unless @groups.nil?
      @groups = []
      parent = ""
      self.to_s.split(GROUP_SEPARATOR).each do |part|
        if !part.blank?
          group = ((parent.blank?) ? "" : "#{parent}#{GROUP_SEPARATOR}") + part
          @groups << Label.new(group)
          parent = group
        end
      end
      @groups.reverse!
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redistat-0.3.0 lib/redistat/label.rb
redistat-0.2.6 lib/redistat/label.rb
redistat-0.2.5 lib/redistat/label.rb
redistat-0.2.4 lib/redistat/label.rb