Class: Uorm::Redis::Persistance

Inherits:
Struct
  • Object
show all
Defined in:
lib/uorm/redis/persistance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client

Returns:

  • (Object)

    the current value of client



3
4
5
# File 'lib/uorm/redis/persistance.rb', line 3

def client
  @client
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



3
4
5
# File 'lib/uorm/redis/persistance.rb', line 3

def model
  @model
end

Instance Method Details

#all(q = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/uorm/redis/persistance.rb', line 5

def all q = {}
  arr = client.keys.map { |id| self.find id }
  return arr if q.empty? or arr.empty?

  if have_query?(q, arr)
    arr.select! { |attrs| q.all? { |(key, value)| [key] == value } }
  end
end

#create(object) ⇒ Object



18
19
20
21
# File 'lib/uorm/redis/persistance.rb', line 18

def create object
  object.id = ::SecureRandom.uuid
  write object
end

#delete(object) ⇒ Object



27
28
29
# File 'lib/uorm/redis/persistance.rb', line 27

def delete object
  client.del object.id
end

#find(id) ⇒ Object



14
15
16
# File 'lib/uorm/redis/persistance.rb', line 14

def find id
  client.mapped_hmget id, *model.fields.map(&:name)
end

#update(object) ⇒ Object



23
24
25
# File 'lib/uorm/redis/persistance.rb', line 23

def update object
  write object
end