Class: ActiveRedis::Base

Inherits:
Object show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming, Attributes
Includes:
ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serialization, ActiveModel::Validations, AllList, AttributeMethods, DirtyAttributes, Save
Defined in:
lib/active_redis_orm.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

attr_accessible, attr_accessible?, boolean_field, counter_field, date_field, define_field, finder_field, float_field, hash_field, integer_field, list_field, set_default, set_field, sorted_set_field, string_field, time_field

Methods included from AllList

#add_to_all_lists, included, #remove_from_all_lists

Methods included from AttributeMethods

#after_set, #allow_mass_assignment?, #expire_field, #set_attributes, #update_attributes

Methods included from Save

#new_record?, #save

Methods included from DirtyAttributes

#check_for_changes

Constructor Details

#initialize(*args) ⇒ Base

initialize with hash



38
39
40
41
42
43
44
45
46
# 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

Class Attribute Details

.attribute_definitionsObject

Returns the value of attribute attribute_definitions.



84
85
86
# File 'lib/active_redis_orm.rb', line 84

def attribute_definitions
  @attribute_definitions
end

.attributes_moduleObject

Returns the value of attribute attributes_module.



84
85
86
# File 'lib/active_redis_orm.rb', line 84

def attributes_module
  @attributes_module
end

.presence_fieldObject

Returns the value of attribute presence_field.



84
85
86
# File 'lib/active_redis_orm.rb', line 84

def presence_field
  @presence_field
end

.redisObject

Returns the value of attribute redis.



84
85
86
# File 'lib/active_redis_orm.rb', line 84

def redis
  @redis
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



32
33
34
# File 'lib/active_redis_orm.rb', line 32

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



31
32
33
# File 'lib/active_redis_orm.rb', line 31

def id
  @id
end

Class Method Details

.attribute_optionsObject



123
124
125
126
# File 'lib/active_redis_orm.rb', line 123

def attribute_options
  self.attribute_definitions ||= {}
  attribute_definitions
end

.attributesObject



119
120
121
# File 'lib/active_redis_orm.rb', line 119

def attributes
  attribute_options.keys
end

.create(*args) ⇒ Object



95
96
97
98
99
# File 'lib/active_redis_orm.rb', line 95

def create(*args)
  object = new(*args)
  object.save
  object
end

.field(field_name, options = {}) ⇒ Object



101
102
103
104
105
# 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) ⇒ Object



86
87
88
89
90
91
92
93
# 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) ⇒ Object



111
112
113
114
115
116
117
# 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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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

.redis_namespaceObject



107
108
109
# File 'lib/active_redis_orm.rb', line 107

def redis_namespace
  self.name.underscore
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
# File 'lib/active_redis_orm.rb', line 48

def ==(other)
  self.class == other.class && self.id == other.id && id.present?
end

#attribute(name) ⇒ Object



70
71
72
# File 'lib/active_redis_orm.rb', line 70

def attribute(name)
  public_send("#{name}_val")
end

#destroyObject



62
63
64
65
66
67
68
# 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?Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/active_redis_orm.rb', line 57

def dirty?
  check_for_changes
  self.changes.present?
end

#present?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# 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!Object



52
53
54
55
# File 'lib/active_redis_orm.rb', line 52

def reload!
  @attributes = {}
  self
end