class ActiveRedis::Base

Attributes

attribute_definitions[RW]
attributes_module[RW]
presence_field[RW]
redis[RW]
attributes[R]
id[RW]

Public Class Methods

attribute_options() click to toggle source
# File lib/active_redis_orm.rb, line 123
def attribute_options
  self.attribute_definitions ||= {}
  attribute_definitions
end
attributes() click to toggle source
# File lib/active_redis_orm.rb, line 119
def attributes
  attribute_options.keys
end
create(*args) click to toggle source
# File lib/active_redis_orm.rb, line 95
def create(*args)
  object = new(*args)
  object.save
  object
end
field(field_name, options = {}) click to toggle source
# File lib/active_redis_orm.rb, line 101
def field(field_name, options = {})
  self.attribute_definitions ||= {}
  self.attribute_definitions[field_name.to_sym] = options
  define_field(field_name, options)
end
find(id) click to toggle source
# File lib/active_redis_orm.rb, line 86
def find(id)
  result = new(id)
  if result.present?
    result
  else
    nil
  end
end
finder_key(field_name, value) click to toggle source
# File lib/active_redis_orm.rb, line 111
def finder_key(field_name, value)
  if attribute_options[field_name] && attribute_options[field_name][:finder_key].present?
    attribute_options[field_name][:finder_key].call(value)
  else
    "#{redis_namespace.pluralize}:#{field_name}_to_#{redis_namespace}_id:#{value}"
  end
end
inherited(klass) click to toggle source
# File lib/active_redis_orm.rb, line 132
def inherited(klass)
  class << klass
    self.class_eval do
      define_method :attribute_options do
        self.attribute_definitions.merge(superclass.attribute_options)
      end
    end
  end

  klass.attributes_module = Module.new
  klass.send(:include, klass.attributes_module)

  if klass.presence_field.blank?
    klass.field :presence_field, default: "1"
  end
end
new(*args) click to toggle source

initialize with hash

# File lib/active_redis_orm.rb, line 38
def initialize(*args)
  @attributes = {}
  if args.first.is_a?(Hash) || args.empty?
    set_attributes(args.first)
    @new_record = true
  elsif args.first.is_a?(String)
    @id = args.first
  end
end
redis_namespace() click to toggle source
# File lib/active_redis_orm.rb, line 107
def redis_namespace
  self.name.underscore
end

Public Instance Methods

==(other) click to toggle source
# File lib/active_redis_orm.rb, line 48
def ==(other)
  self.class == other.class && self.id == other.id && id.present?
end
attribute(name) click to toggle source
# File lib/active_redis_orm.rb, line 70
def attribute(name)
  public_send("#{name}_val")
end
destroy() click to toggle source
# File lib/active_redis_orm.rb, line 62
def destroy
  run_callbacks :destroy do
    self.class.attributes.each do |attribute|
      send("destroy_#{attribute}")
    end
  end
end
dirty?() click to toggle source
# File lib/active_redis_orm.rb, line 57
def dirty?
  check_for_changes
  self.changes.present?
end
present?() click to toggle source
# File lib/active_redis_orm.rb, line 74
def present?
  if self.class.presence_field.present?
    send(self.class.presence_field).present?
  else
    presence_field.present?
  end
end
reload!() click to toggle source
# File lib/active_redis_orm.rb, line 52
def reload!
  @attributes = {}
  self
end