Sha256: 22fb8c279b2667d1f4e75d5c21ee2774a143c75a064200b175bf741b1e529e0d

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 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

1 entries across 1 versions & 1 rubygems

Version Path
redistat-0.4.0 lib/redistat/label.rb