initial import

This commit is contained in:
2010-07-19 00:57:23 +03:00
commit afd6265288
17 changed files with 360 additions and 0 deletions

83
lib/redistat.rb Normal file
View File

@@ -0,0 +1,83 @@
require "redis"
require "yaml"
require "time"
require "digest/sha1"
require "redistat/extensions/date_time"
require "redistat/database"
require "redistat/model"
require "redistat/event"
require "redistat/key"
require "redistat/label"
require "redistat/date"
module Redistat
# Provides access to the Redis database. This is shared accross all models and instances.
def redis
threaded[:redis] ||= connection(*options)
end
def redis=(connection)
threaded[:redis] = connection
end
def threaded
Thread.current[:redistat] ||= {}
end
# Connect to a redis database.
#
# @param options [Hash] options to create a message with.
# @option options [#to_s] :host ('127.0.0.1') Host of the redis database.
# @option options [#to_s] :port (6379) Port number.
# @option options [#to_s] :db (0) Database number.
# @option options [#to_s] :timeout (0) Database timeout in seconds.
# @example Connect to a database in port 6380.
# Ohm.connect(:port => 6380)
def connect(*options)
self.redis = nil
@options = options
end
# Return a connection to Redis.
#
# This is a wapper around Redis.new(options)
def connection(*options)
Redis.new(*options)
end
def options
@options = [] unless defined? @options
@options
end
# Clear the database.
def flush
redis.flushdb
end
module_function :connect, :connection, :flush, :redis, :redis=, :options, :threaded
end

7
lib/redistat/database.rb Normal file
View File

@@ -0,0 +1,7 @@
module Redistat
module Database
def db
Redistat.redis
end
end
end

50
lib/redistat/date.rb Normal file
View File

@@ -0,0 +1,50 @@
module Redistat
class Date
attr_accessor :year
attr_accessor :month
attr_accessor :day
attr_accessor :hour
attr_accessor :min
attr_accessor :sec
def initialize(input)
if input.is_a?(::Time)
from_time(input)
elsif input.is_a?(::Date)
from_date(input)
elsif input.is_a?(::String)
from_string(input)
end
end
def from_time(input)
[:year, :month, :day, :hour, :min, :sec].each do |k|
self.send("#{k}=", input.send(k))
end
end
def from_date(input)
[:year, :month, :day].each do |k|
self.send("#{k}=", input.send(k))
end
end
def from_string(input)
from_time(Time.parse(input))
end
def to_s(depth = :sec)
output = ""
[:year, :month, :day, :hour, :min, :sec].each_with_index do |current, i|
break if self.send(current).nil?
output << self.send(current).to_s.rjust((i <= 0) ? 4 : 2, '0')
break if current == depth
end
output
end
alias :to_string :to_s
end
end

18
lib/redistat/event.rb Normal file
View File

@@ -0,0 +1,18 @@
module Redistat
class Event
attr_accessor :label
attr_reader :options
def initialize(scope, options, data = {}, label = nil, time = nil)
key = [scope]
key << Digest::SHA1.hexdigest(label) if !label.nil?
time ||= Time.now
key << time.to_redistat(options ||= nil)
puts key.inspect
end
end
end

View File

@@ -0,0 +1,11 @@
class Date
def to_redistat(depth = nil)
Redistat::Date.new(self).to_s(depth)
end
end
class Time
def to_redistat(depth = nil)
Redistat::Date.new(self).to_s(depth)
end
end

28
lib/redistat/key.rb Normal file
View File

@@ -0,0 +1,28 @@
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 ||= Time.now
@options = options
end
def label
if !@label.nil?
(options[:hash_label] ||= true) ? @label.hash : @label.name
end
end
def to_s
key = "#{@scope}"
key << "/#{label}" if !label.nil?
key << ":#{@date.to_redistat(@options[:depth] ||= nil)}"
end
end
end

28
lib/redistat/label.rb Normal file
View File

@@ -0,0 +1,28 @@
module Redistat
class Label
include Database
attr_reader :name
attr_reader :hash
def initialize(str)
@name = str
@hash = Digest::SHA1.hexdigest(@name)
end
def save
db.set("Redistat:lables:#{@hash}", @name)
@saved = true
self
end
def saved?
@saved ||= false
end
def self.create(name)
self.new(name).save
end
end
end

13
lib/redistat/model.rb Normal file
View File

@@ -0,0 +1,13 @@
module Redistat
class Model
def self.create(*args)
Event.new(self.name, self.options, *args)
end
def self.options
@options ||= {}
end
end
end