Module: ActiveRedis::Attributes::InstanceMethods

Included in:
Base
Defined in:
lib/active_redis/attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute_pathsArray

Get attribute paths by keys. Complex types needs to be stored at another path.

Returns:

  • (Array)


192
193
194
195
196
197
198
199
# File 'lib/active_redis/attributes.rb', line 192

def attribute_paths
  keys.map do |k,v|
    case v.to_s
    when /Hash/, /Array/ then "#{basename}:#{k}"
    else basename
    end
  end.uniq
end

.basenameString

Redis basename.

Returns:

  • (String)


182
183
184
# File 'lib/active_redis/attributes.rb', line 182

def basename
  "#{self.class.name.downcase.pluralize}:#{self.id}"
end

.create_attribute_methodsvoid

This method returns an undefined value.

Create getter/setter/bool methods by attributes. This method should be called in a constructor.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_redis/attributes.rb', line 84

def create_attribute_methods
  self.class.instance_eval do
    keys.each do |attribute,type|
      define_method(attribute) do
        attributes[attribute]
      end 

      define_method("#{attribute}=") do |arg|
        attributes[attribute] = format_attribute(attribute,arg.to_s)
      end 

      define_method("#{attribute}?") do
        !attributes[attribute].to_s.empty?
      end 
    end 
  end 
end

.format_attribute(key, value) ⇒ Object

Casting objects into original values. The type whould be defined at class method “keys”

Parameters:

  • key (String, Symbol)
  • value (Object)

Returns:

  • (Object)

    Casted value.

See Also:

  • ActiveRedis.keys


134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/active_redis/attributes.rb', line 134

def format_attribute key, value
  case keys[key.to_sym].to_s
  when /Integer/
    value.to_i
  when /Array/
    connection.lrange("#{basename}:#{key}",0,-1)
  when /Hash/
    connection.hgetall("#{basename}:#{key}")
  when /Time/
    Time.parse(value) rescue nil
  else value
  end
end

.get_attributes(reload: false) ⇒ Hash

Get attributes from redis store.

Parameters:

  • reload (true, false) (defaults to: false)

    Force reload from redis.

Returns:

  • (Hash)


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_redis/attributes.rb', line 109

def get_attributes reload: false
  if reload or not @h
    h = (@h and @h[:id]) ? connection.hgetall(basename) : {}
    attribute_hash = {}
    unless h.empty?
      keys.each do |k,v|
        attribute_hash[k.to_sym] = format_attribute(k.to_sym, h[k.to_s])
      end
    end
    @h = attribute_hash
  end
  @h
end

.set_attributes(arg) ⇒ void

This method returns an undefined value.

Save given hash to redis server. Refresh updated_at timestamp. Unless id isn’t set, get the next primary key.

Parameters:

  • arg (Hash)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/active_redis/attributes.rb', line 157

def set_attributes arg
  remove
  arg[:updated_at] = Time.now.to_s
  arg[:id] ||= connection.incr("#{self.class.name}_id")
  arg.each do |k,v|
    case v
    when Array
      v.each do |v|
        connection.lpush "#{basename}:#{k}", v
      end
    when Hash
      connection.mapped_hmset "#{basename}:#{k}", v
    else
      handle_relation relation_key: k, value: v, action: :add
      connection.hset basename, k, v
    end
  end
  get_attributes(reload: true)
end

Instance Method Details

#attributesHash

Get the current attributes.

Returns:

  • (Hash)


50
51
52
# File 'lib/active_redis/attributes.rb', line 50

def attributes
  get_attributes
end

#connectionObject

See Also:

  • ActiveRedis#connection


64
65
66
# File 'lib/active_redis/attributes.rb', line 64

def connection
  self.class.connection
end

#inspectString

Reformat object.

Returns:

  • (String)


20
21
22
23
24
25
# File 'lib/active_redis/attributes.rb', line 20

def inspect
  a = keys.reject{|k,v| k == :id }.map do |attribute,type|
    %Q{#{attribute}=#{send(attribute).inspect}}
  end
  %Q{#<#{self.class.name} id=#{self.id} #{a.join(' ')}>}
end

#keysObject

See Also:

  • ActiveRedis#keys


57
58
59
# File 'lib/active_redis/attributes.rb', line 57

def keys
  self.class.keys
end

#read_attribute_for_validation(key) ⇒ Object

Implement ActiveModel validation support.



71
72
73
# File 'lib/active_redis/attributes.rb', line 71

def read_attribute_for_validation key 
  attributes[key]
end

#removevoid Also known as: delete, destroy

This method returns an undefined value.

Remove the instance from redis store. Remove all relations to another objects.



33
34
35
36
37
38
39
40
# File 'lib/active_redis/attributes.rb', line 33

def remove
  blto = self.class.instance_variable_get(:@belongs_to)
  (blto || []).each do |rel|
    handle_relation relation_key: "#{rel.to_s.singularize}_id",
      action: :del
  end
  connection.del *attribute_paths
end

#savetrue, false

Save the current record to redis store.

Returns:

  • (true, false)


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

def save
  valid? ? (set_attributes(attributes) && true) : false
end