Class: Findable::Base
Class Method Summary
collapse
belongs_to, has_many, has_one
Methods included from Connection
#redis
Methods included from Recordable
#attributes, #delete, #hash, #id, #id=, #initialize, #new_record?, #persisted?, #save, #to_json
Methods included from Serializer
#deserialize, #serialize
Class Method Details
.all ⇒ Object
22
23
24
|
# File 'lib/findable/base.rb', line 22
def all
all_data.map {|data| new(data) }
end
|
.column_names ⇒ Object
18
19
20
|
# File 'lib/findable/base.rb', line 18
def column_names
raise NotImplementedError
end
|
.count ⇒ Object
73
74
75
|
# File 'lib/findable/base.rb', line 73
def count
redis.hlen(data_key)
end
|
.create(attrs = {}) ⇒ Object
81
82
83
84
85
|
# File 'lib/findable/base.rb', line 81
def create(attrs = {})
record = new(attrs)
record.save
record
end
|
.create!(attrs = {}) ⇒ Object
87
88
89
90
91
|
# File 'lib/findable/base.rb', line 87
def create!(attrs = {})
record = new(attrs)
record.save!
record
end
|
.delete(id) ⇒ Object
116
117
118
|
# File 'lib/findable/base.rb', line 116
def delete(id)
redis.hdel(data_key, id)
end
|
.delete_all ⇒ Object
Also known as:
destroy_all
93
94
95
|
# File 'lib/findable/base.rb', line 93
def delete_all
redis.del(data_key)
end
|
.exists?(record) ⇒ Boolean
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/findable/base.rb', line 60
def exists?(record)
if record.is_a?(self)
_id = record.id
return false unless _id
else
_id = record.to_i
end
redis.hexists data_key, _id
end
|
.find(*ids) ⇒ Object
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/findable/base.rb', line 26
def find(*ids)
ids = ids.first if ids.size == 1
values = find_by_id(ids)
case
when values.empty? then nil
when ids.is_a?(Array) then values.map {|val| new(val)}
else new(values.first)
end
end
|
.find_by(params) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/findable/base.rb', line 37
def find_by(params)
params.symbolize_keys!
if id = params.delete(:id)
values = find_by_id(id)
return nil if values.empty?
return new(values.first) if params.empty?
values.each {|val|
record = new(val)
return record if params.all? {|k,v| record.send(k) == v }
}
else
all_data.each {|val|
record = new(val)
return record if params.all? {|k,v| record.send(k) == v }
}
end
end
|
.ids ⇒ Object
77
78
79
|
# File 'lib/findable/base.rb', line 77
def ids
redis.hkeys(data_key).map(&:to_i)
end
|
.import(records) ⇒ Object
102
103
104
105
106
107
108
109
|
# File 'lib/findable/base.rb', line 102
def import(records)
data = records.each_with_object([]) {|record, obj|
record.id ||= auto_incremented_id
obj << record.id
obj << record.to_json
}
redis.hmset(data_key, *data)
end
|
.insert(record) ⇒ Object
111
112
113
114
|
# File 'lib/findable/base.rb', line 111
def insert(record)
record.id ||= auto_incremented_id
redis.hset(data_key, record.id, record.to_json)
end
|
.primary_key ⇒ Object
14
15
16
|
# File 'lib/findable/base.rb', line 14
def primary_key
"id"
end
|
.transaction(&block) ⇒ Object
98
99
100
|
# File 'lib/findable/base.rb', line 98
def transaction(&block)
redis.multi &block
end
|
.where(params) ⇒ Object
56
57
58
|
# File 'lib/findable/base.rb', line 56
def where(params)
all.select {|record| params.all? {|k,v| record.send(k) == v } }
end
|