Module: Redis::Persistence

Defined in:
lib/redis/persistence.rb,
lib/redis/persistence/version.rb

Overview

Redis::Persistence is a lightweight object persistence framework, fully compatible with ActiveModel and based on Redis.

Features:

  • 100% Rails compatibility

  • 100% ActiveModel compatibility: callbacks, validations, serialization, …

  • No crazy has_many-type of semantics

  • Auto-incrementing IDs

  • Defining default values for properties

  • Casting properties as built-in or custom classes

  • Convenient “dot access” to properties (article.views.today)

  • Support for “collections” of embedded objects (eg. article <> comments)

  • Automatic conversion of UTC-formatted strings to Time objects

Basic example:

class Article
  include Redis::Persistence

  property :title
  property :body
  property :author, :default  => '(Unknown)'
  property :created
end

Article.create title: 'Hello World!', body: 'So, in the beginning...', created: Time.now.utc
article = Article.find(1)
# => <Article: {"id"=>1, "title"=>"Hello World!", ...}>
article.title
# => Hello World!
article.created.class
# => Time

See the examples/article.rb for full example.

Defined Under Namespace

Modules: ActiveModelIntegration, ClassMethods, InstanceMethods Classes: FamilyNotLoaded, RedisNotAvailable

Constant Summary collapse

DEFAULT_FAMILY =
'default'
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.configObject



54
55
56
# File 'lib/redis/persistence.rb', line 54

def self.config
  @__config ||= Hashr.new
end

.configure {|config| ... } ⇒ Object

Yields:



58
59
60
# File 'lib/redis/persistence.rb', line 58

def self.configure
  yield config
end

.included(base) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/redis/persistence.rb', line 62

def self.included(base)
  base.class_eval do
    include ActiveModelIntegration
    self.include_root_in_json = false

    extend  ClassMethods
    include InstanceMethods

    def self.__redis
      Redis::Persistence.config.redis
    end

    def __redis
      self.class.__redis
    end
  end
end