Module: Redis::ORM::Attributes

Included in:
Redis::ORM
Defined in:
lib/redis/orm/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/redis/orm/attributes.rb', line 3

def self.included(base)
  base.class_eval do
    extend Redis::ORM::Attributes::ClassMethods
    class_inheritable_hash :model_attributes
    self.model_attributes ||= HashWithIndifferentAccess.new
  end
end

Instance Method Details

#attribute_namesObject



19
20
21
# File 'lib/redis/orm/attributes.rb', line 19

def attribute_names
  @attribute_names ||= attributes.keys
end

#attributesObject



11
12
13
# File 'lib/redis/orm/attributes.rb', line 11

def attributes
  @attributes ||= model_attributes.dup
end

#attributes=(changed_attributes) ⇒ Object



23
24
25
26
27
# File 'lib/redis/orm/attributes.rb', line 23

def attributes=(changed_attributes)
  changed_attributes.each do |key, value|
    send("#{key}=", value)
  end
end

#changed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/redis/orm/attributes.rb', line 39

def changed?
  new_record? || attributes != @previous_attributes
end

#new_record?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/redis/orm/attributes.rb', line 35

def new_record?
  @previous_attributes.nil?
end

#previous_attributesObject



29
30
31
32
33
# File 'lib/redis/orm/attributes.rb', line 29

def previous_attributes
  # note we do NOT assign here, this is because #changed?
  # and #new_record? rely on @previous_attributes to be nil
  @previous_attributes || attributes.dup
end

#set_unchanged!Object



15
16
17
# File 'lib/redis/orm/attributes.rb', line 15

def set_unchanged!
  @previous_attributes = attributes.dup
end