redistat 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -23,3 +23,4 @@ pkg
23
23
  .yardoc/*
24
24
  spec/db/*
25
25
  doc
26
+ redistat.gemspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,20 +5,20 @@ module Redistat
5
5
  attr_accessor :date
6
6
  attr_accessor :options
7
7
 
8
- def initialize(scope, label = nil, date = nil, options = {})
8
+ def initialize(scope, label_name = nil, time_stamp = nil, options = {})
9
+ @options = default_options.merge(options || {})
9
10
  @scope = scope
10
- self.label = label if !label.nil?
11
- self.date = date ||= Time.now
12
- @options = default_options.merge(options ||= {})
11
+ self.label = label_name if !label_name.nil?
12
+ self.date = time_stamp ||= Time.now
13
13
  end
14
14
 
15
15
  def default_options
16
- { :depth => :day }
16
+ { :depth => :hour, :hashed_label => false }
17
17
  end
18
18
 
19
19
  def prefix
20
20
  key = "#{@scope}"
21
- key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil?
21
+ key << "/#{label}" if !label.nil?
22
22
  key << ":"
23
23
  key
24
24
  end
@@ -40,7 +40,7 @@ module Redistat
40
40
  end
41
41
 
42
42
  def label=(input)
43
- @label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input)
43
+ @label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
44
44
  end
45
45
 
46
46
  def to_s(depth = nil)
@@ -2,16 +2,23 @@ module Redistat
2
2
  class Label
3
3
  include Database
4
4
 
5
- attr_reader :name
6
- attr_reader :hash
5
+ attr_reader :raw
7
6
 
8
- def initialize(str)
9
- @name = str.to_s
10
- @hash = Digest::SHA1.hexdigest(@name)
7
+ def initialize(str, options = {})
8
+ @options = options
9
+ @raw = str.to_s
10
+ end
11
+
12
+ def name
13
+ @options[:hashed_label] ? hash : @raw
14
+ end
15
+
16
+ def hash
17
+ @hash ||= Digest::SHA1.hexdigest(@raw)
11
18
  end
12
19
 
13
20
  def save
14
- @saved = (db.set("#{KEY_LEBELS}#{@hash}", @name) == "OK")
21
+ @saved = (db.set("#{KEY_LEBELS}#{hash}", @raw) == "OK") if @options[:hashed_label]
15
22
  self
16
23
  end
17
24
 
@@ -19,8 +26,8 @@ module Redistat
19
26
  @saved ||= false
20
27
  end
21
28
 
22
- def self.create(name)
23
- self.new(name).save
29
+ def self.create(name, options = {})
30
+ self.new(name, options).save
24
31
  end
25
32
 
26
33
  end
@@ -20,6 +20,14 @@ module Redistat
20
20
  end
21
21
  alias :find :fetch
22
22
 
23
+ def hashed_label(boolean = nil)
24
+ if !boolean.nil?
25
+ options[:hashed_label] = boolean
26
+ else
27
+ options[:hashed_label] || nil
28
+ end
29
+ end
30
+
23
31
  def depth(depth = nil)
24
32
  if !depth.nil?
25
33
  options[:depth] = depth
@@ -19,24 +19,24 @@ describe Redistat::Key do
19
19
  end
20
20
 
21
21
  it "should convert to string properly" do
22
- @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}"
22
+ @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
23
23
  props = [:year, :month, :day, :hour, :min, :sec]
24
24
  props.each do
25
- @key.to_s(props.last).should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(props.last)}"
25
+ @key.to_s(props.last).should == "#{@scope}/#{@label}:#{@key.date.to_s(props.last)}"
26
26
  props.pop
27
27
  end
28
28
  end
29
29
 
30
- it "should abide to hash_label option" do
31
- @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => true})
30
+ it "should abide to hashed_label option" do
31
+ @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => true})
32
32
  @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}"
33
- @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => false})
33
+ @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => false})
34
34
  @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
35
35
  end
36
36
 
37
37
  it "should have default depth option" do
38
38
  @key = Redistat::Key.new(@scope, @label, @date)
39
- @key.depth.should == :day
39
+ @key.depth.should == :hour
40
40
  end
41
41
 
42
42
  it "should allow changing attributes" do
@@ -15,14 +15,14 @@ describe Redistat::Label do
15
15
  end
16
16
 
17
17
  it "should store a label hash lookup key" do
18
- @label.save
19
- @label.saved?.should be_true
20
- db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
18
+ label = Redistat::Label.new(@name, {:hashed_label => true}).save
19
+ label.saved?.should be_true
20
+ db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == @name
21
21
 
22
- @name = "contact_us"
23
- @label = Redistat::Label.create(@name)
24
- @label.saved?.should be_true
25
- db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
22
+ name = "contact_us"
23
+ label = Redistat::Label.create(name, {:hashed_label => true})
24
+ label.saved?.should be_true
25
+ db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == name
26
26
  end
27
27
 
28
28
  end
@@ -11,5 +11,6 @@ class ModelHelper2
11
11
 
12
12
  depth :day
13
13
  store_event true
14
+ hashed_label true
14
15
 
15
16
  end
@@ -16,17 +16,23 @@ describe Redistat::Model do
16
16
  it "should listen to model-defined options" do
17
17
  ModelHelper2.depth.should == :day
18
18
  ModelHelper2.store_event.should == true
19
+ ModelHelper2.hashed_label.should == true
19
20
 
20
21
  ModelHelper.depth.should == nil
21
22
  ModelHelper.store_event.should == nil
23
+ ModelHelper.hashed_label.should == nil
22
24
  ModelHelper.depth(:hour)
23
25
  ModelHelper.depth.should == :hour
24
26
  ModelHelper.store_event(true)
25
27
  ModelHelper.store_event.should == true
28
+ ModelHelper.hashed_label(true)
29
+ ModelHelper.hashed_label.should == true
26
30
  ModelHelper.options[:depth] = nil
27
31
  ModelHelper.options[:store_event] = nil
32
+ ModelHelper.options[:hashed_label] = nil
28
33
  ModelHelper.depth.should == nil
29
34
  ModelHelper.store_event.should == nil
35
+ ModelHelper.hashed_label.should == nil
30
36
  end
31
37
 
32
38
  it "should store and fetch stats" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redistat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Myhrberg