allow date/time and label to be changed on

Redistat::Key and Redistat::Event objects
This commit is contained in:
2010-07-19 17:53:00 +03:00
parent cfbbb7d83a
commit 249a4b5cfd
4 changed files with 89 additions and 28 deletions

View File

@@ -2,26 +2,42 @@ module Redistat
class Event
attr_reader :scope
attr_reader :label
attr_reader :key
attr_reader :options
attr_accessor :data
attr_accessor :options
def initialize(scope, label = nil, data = {}, date = nil, options = {})
@options = options
@scope = scope
@key = Key.new(scope, label, date, options)
@label = @key.label
#TODO ...intialize Redistat::Event
end
def date
@key.date.to_date
end
def time
@key.date.to_time
end
def date=(input)
@key.date = input
end
alias :time :date
alias :time= :date=
def label
@key.label
end
def label_hash
@key.label_hash
end
def label=(input)
@key.label = input
end
def save
end

View File

@@ -2,22 +2,30 @@ module Redistat
class Key
attr_accessor :scope
attr_accessor :label
attr_accessor :date
def initialize(scope, label = nil, date = nil, options = {})
@scope = scope
@label = Label.create(label) if !label.nil?
@date = Date.new(date ||= Time.now) # Redistat::Date, not ::Date
self.label = label if !label.nil?
self.date = date ||= Time.now
@options = options
end
#TODO figure out if direct access to the label object is desired or not
# def label
# if !@label.nil?
# (@options[:hash_label] ||= true) ? @label.hash : @label.name
# end
# end
def date=(input)
@date = (input.instance_of?(Redistat::Date)) ? input : Date.new(input) # Redistat::Date, not ::Date
end
def label
@label.name
end
def label_hash
@label.hash
end
def label=(input)
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input)
end
def to_s(depth = nil)
depth ||= @options[:depth] if !@options[:depth].nil?

View File

@@ -4,12 +4,33 @@ describe Redistat::Event do
before(:each) do
@scope = "PageViews"
@label = "/about/us"
@label = "about_us"
@label_hash = Digest::SHA1.hexdigest(@label)
@now = Time.now
@event = Redistat::Event.new(@scope, @label, {:views => 1}, @now, {:depth => :hour})
@date = Time.now
@event = Redistat::Event.new(@scope, @label, {:views => 1}, @date, {:depth => :hour})
end
it "should initialize properly"
# it "should initialize properly"
it "should allow changing Date" do
@event.time.should == @date
@date = Time.now
@event.date = @date
@event.time.should == @date
end
it "should allow changing Label" do
@event.label.should == @label
@event.label_hash.should == @label_hash
@label = "contact_us"
@label_hash = Digest::SHA1.hexdigest(@label)
@event.label = @label
@event.label.should == @label
@event.label_hash.should == @label_hash
end
end

View File

@@ -6,17 +6,16 @@ describe Redistat::Key do
@scope = "PageViews"
@label = "about_us"
@label_hash = Digest::SHA1.hexdigest(@label)
@now = Time.now
@key = Redistat::Key.new(@scope, @label, @now, {:depth => :day})
@date = Time.now
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day})
end
it "should initialize properly" do
@key.scope.should == @scope
@key.label.should be_instance_of(Redistat::Label)
@key.label.name.should == @label
@key.label.hash.should == @label_hash
@key.label.should == @label
@key.label_hash.should == @label_hash
@key.date.should be_instance_of(Redistat::Date)
@key.date.to_time.to_s.should == @now.to_s
@key.date.to_time.to_s.should == @date.to_s
end
it "should convert to string properly" do
@@ -29,11 +28,28 @@ describe Redistat::Key do
end
it "should abide to hash_label option" do
@key = Redistat::Key.new(@scope, @label, @now, {:depth => :day, :hash_label => true})
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day, :hash_label => true})
@key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:day)}"
@key = Redistat::Key.new(@scope, @label, @now, {:depth => :day, :hash_label => false})
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day, :hash_label => false})
@key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:day)}"
end
it "should allow changing Date" do
@key.date.to_time.should == @date
now = Time.now
@key.date = now
@key.date.to_time.should == now
end
it "should allow changing Label" do
@key.label.should == @label
@key.label_hash == @label_hash
@label = "contact_us"
@label_hash = Digest::SHA1.hexdigest(@label)
@key.label = @label
@key.label.should == @label
@key.label_hash == @label_hash
end
end