6 Commits

Author SHA1 Message Date
bc7a563f20 Merge branch 'release/v0.0.2' 2010-11-22 13:30:14 +00:00
39da4c96a7 Version bump to 0.0.2 2010-11-22 13:29:56 +00:00
fa7903dc7a added gemspec to .gitignore 2010-11-22 13:29:41 +00:00
8a0e1a47a2 added proper support for hashed_label option, and
disabled it by default
2010-11-22 13:28:19 +00:00
8e3e54a027 updated gemspec description in Rakefile 2010-11-22 12:36:14 +00:00
07ae8b5c78 Merge branch 'release/v0.0.1' into dev 2010-11-22 12:31:45 +00:00
10 changed files with 54 additions and 31 deletions

1
.gitignore vendored
View File

@@ -23,3 +23,4 @@ pkg
.yardoc/* .yardoc/*
spec/db/* spec/db/*
doc doc
redistat.gemspec

View File

@@ -5,8 +5,8 @@ begin
require 'jeweler' require 'jeweler'
Jeweler::Tasks.new do |gem| Jeweler::Tasks.new do |gem|
gem.name = 'redistat' gem.name = 'redistat'
gem.summary = 'TODO: one-line summary of your gem' gem.summary = 'A Redis-backed statistics storage and querying library written in Ruby.'
gem.description = 'TODO: longer description of your gem' gem.description = 'A Redis-backed statistics storage and querying library written in Ruby.'
gem.email = 'contact@jimeh.me' gem.email = 'contact@jimeh.me'
gem.homepage = 'http://github.com/jimeh/redistat' gem.homepage = 'http://github.com/jimeh/redistat'
gem.authors = ['Jim Myhrberg'] gem.authors = ['Jim Myhrberg']

View File

@@ -1 +1 @@
0.0.1 0.0.2

View File

@@ -5,20 +5,20 @@ module Redistat
attr_accessor :date attr_accessor :date
attr_accessor :options attr_accessor :options
def initialize(scope, label = nil, date = nil, options = {}) def initialize(scope, label_name = nil, time_stamp = nil, options = {})
@options = default_options.merge(options || {})
@scope = scope @scope = scope
self.label = label if !label.nil? self.label = label_name if !label_name.nil?
self.date = date ||= Time.now self.date = time_stamp ||= Time.now
@options = default_options.merge(options ||= {})
end end
def default_options def default_options
{ :depth => :day } { :depth => :hour, :hashed_label => false }
end end
def prefix def prefix
key = "#{@scope}" key = "#{@scope}"
key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil? key << "/#{label}" if !label.nil?
key << ":" key << ":"
key key
end end
@@ -40,7 +40,7 @@ module Redistat
end end
def label=(input) def label=(input)
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input) @label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
end end
def to_s(depth = nil) def to_s(depth = nil)

View File

@@ -2,16 +2,23 @@ module Redistat
class Label class Label
include Database include Database
attr_reader :name attr_reader :raw
attr_reader :hash
def initialize(str) def initialize(str, options = {})
@name = str.to_s @options = options
@hash = Digest::SHA1.hexdigest(@name) @raw = str.to_s
end
def name
@options[:hashed_label] ? hash : @raw
end
def hash
@hash ||= Digest::SHA1.hexdigest(@raw)
end end
def save def save
@saved = (db.set("#{KEY_LEBELS}#{@hash}", @name) == "OK") @saved = (db.set("#{KEY_LEBELS}#{hash}", @raw) == "OK") if @options[:hashed_label]
self self
end end
@@ -19,8 +26,8 @@ module Redistat
@saved ||= false @saved ||= false
end end
def self.create(name) def self.create(name, options = {})
self.new(name).save self.new(name, options).save
end end
end end

View File

@@ -20,6 +20,14 @@ module Redistat
end end
alias :find :fetch alias :find :fetch
def hashed_label(boolean = nil)
if !boolean.nil?
options[:hashed_label] = boolean
else
options[:hashed_label] || nil
end
end
def depth(depth = nil) def depth(depth = nil)
if !depth.nil? if !depth.nil?
options[:depth] = depth options[:depth] = depth

View File

@@ -19,24 +19,24 @@ describe Redistat::Key do
end end
it "should convert to string properly" do it "should convert to string properly" do
@key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}" @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
props = [:year, :month, :day, :hour, :min, :sec] props = [:year, :month, :day, :hour, :min, :sec]
props.each do props.each do
@key.to_s(props.last).should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(props.last)}" @key.to_s(props.last).should == "#{@scope}/#{@label}:#{@key.date.to_s(props.last)}"
props.pop props.pop
end end
end end
it "should abide to hash_label option" do it "should abide to hashed_label option" do
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => true}) @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => true})
@key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}" @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}"
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => false}) @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => false})
@key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}" @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
end end
it "should have default depth option" do it "should have default depth option" do
@key = Redistat::Key.new(@scope, @label, @date) @key = Redistat::Key.new(@scope, @label, @date)
@key.depth.should == :day @key.depth.should == :hour
end end
it "should allow changing attributes" do it "should allow changing attributes" do

View File

@@ -15,14 +15,14 @@ describe Redistat::Label do
end end
it "should store a label hash lookup key" do it "should store a label hash lookup key" do
@label.save label = Redistat::Label.new(@name, {:hashed_label => true}).save
@label.saved?.should be_true label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == @name
@name = "contact_us" name = "contact_us"
@label = Redistat::Label.create(@name) label = Redistat::Label.create(name, {:hashed_label => true})
@label.saved?.should be_true label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == name
end end
end end

View File

@@ -11,5 +11,6 @@ class ModelHelper2
depth :day depth :day
store_event true store_event true
hashed_label true
end end

View File

@@ -16,17 +16,23 @@ describe Redistat::Model do
it "should listen to model-defined options" do it "should listen to model-defined options" do
ModelHelper2.depth.should == :day ModelHelper2.depth.should == :day
ModelHelper2.store_event.should == true ModelHelper2.store_event.should == true
ModelHelper2.hashed_label.should == true
ModelHelper.depth.should == nil ModelHelper.depth.should == nil
ModelHelper.store_event.should == nil ModelHelper.store_event.should == nil
ModelHelper.hashed_label.should == nil
ModelHelper.depth(:hour) ModelHelper.depth(:hour)
ModelHelper.depth.should == :hour ModelHelper.depth.should == :hour
ModelHelper.store_event(true) ModelHelper.store_event(true)
ModelHelper.store_event.should == true ModelHelper.store_event.should == true
ModelHelper.hashed_label(true)
ModelHelper.hashed_label.should == true
ModelHelper.options[:depth] = nil ModelHelper.options[:depth] = nil
ModelHelper.options[:store_event] = nil ModelHelper.options[:store_event] = nil
ModelHelper.options[:hashed_label] = nil
ModelHelper.depth.should == nil ModelHelper.depth.should == nil
ModelHelper.store_event.should == nil ModelHelper.store_event.should == nil
ModelHelper.hashed_label.should == nil
end end
it "should store and fetch stats" do it "should store and fetch stats" do