Class: RedisStorage::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/redis_storage.rb

Constant Summary collapse

@@_attributes =
Hash.new { |hash, key| hash[key] = [] }
@@_indizies =
Hash.new { |hash, key| hash[key] = [] }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



152
153
154
155
156
157
158
159
160
# File 'lib/redis_storage.rb', line 152

def initialize(params={})
  @_attributes = {}
  @_indizies = {}
  @id = nil
  @errors = ActiveModel::Errors.new(self)
  params.each do |key, value|
    send("#{key}=", value)
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



13
14
15
# File 'lib/redis_storage.rb', line 13

def errors
  @errors
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/redis_storage.rb', line 12

def id
  @id
end

Class Method Details

.allObject



66
67
68
69
70
# File 'lib/redis_storage.rb', line 66

def self.all
  keys = $db.smembers(persisted_key)

  load_many keys
end

.attribute(*attr) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redis_storage.rb', line 18

def self.attribute *attr
  attr.each do |name|
    define_method(name) do
      @_attributes[name]
    end
    define_method(:"#{name}=") do |v|
      @_attributes[name] = v
    end
    attributes << name unless attributes.include? name
  end
end

.build(params) ⇒ Object



35
36
37
# File 'lib/redis_storage.rb', line 35

def self.build params
  new params
end

.countObject



72
73
74
# File 'lib/redis_storage.rb', line 72

def self.count
  $db.scard(persisted_key)
end

.create(params) ⇒ Object



39
40
41
42
43
# File 'lib/redis_storage.rb', line 39

def self.create params
  obj = build params
  obj.save
  obj
end

.db_keyObject



142
143
144
# File 'lib/redis_storage.rb', line 142

def self.db_key
  model_name
end

.find(params = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/redis_storage.rb', line 49

def self.find(params=nil)
  if params.nil?
    all
  else
    find_by :id, params   #TODO perhaps make this at some point more generic
  end
end

.find_by(key, value) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/redis_storage.rb', line 56

def self.find_by(key, value)
  if key == :id
    load "#{db_key}:#{value}"
  elsif indizies.include? key
    load_many $db.smembers("#{db_key}:#{key}:#{value.hash}")
  else
    nil
  end
end

.firstObject



75
76
77
# File 'lib/redis_storage.rb', line 75

def self.first
  load $db.smembers(persisted_key).sort_by{|s| s.split(':')[1].to_i}.first
end

.index(*attr) ⇒ Object



29
30
31
32
33
# File 'lib/redis_storage.rb', line 29

def self.index *attr
  attr.each do |name|
    indizies << name unless indizies.include? name
  end
end

.lastObject



78
79
80
# File 'lib/redis_storage.rb', line 78

def self.last
  load $db.smembers(persisted_key).sort_by{|s| s.split(':')[1].to_i}.last
end

.persisted_keyObject



148
149
150
# File 'lib/redis_storage.rb', line 148

def self.persisted_key
  "#{db_key}:persisted"
end

.randomObject



45
46
47
48
# File 'lib/redis_storage.rb', line 45

def self.random
  i = $db.srandmember(persisted_key)
  load(i) unless i.nil?
end

Instance Method Details

#db_keyObject



145
146
147
# File 'lib/redis_storage.rb', line 145

def db_key
  "#{self.class.db_key}:#{self.id}"
end

#delete!Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/redis_storage.rb', line 119

def delete!
  if persisted?
    $db.multi do
      $db.del db_key
      $db.srem self.class.persisted_key, db_key
      indizies.each do |key|
        $db.srem "#{self.class.db_key}:#{key}:#{send(key).hash}", db_key
      end
    end
    true
  else
    false
  end
end

#destroyObject



116
117
118
# File 'lib/redis_storage.rb', line 116

def destroy
  delete!     #for the default rails controller
end

#persisted?Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
# File 'lib/redis_storage.rb', line 134

def persisted?
  if id.nil?
    false
  else
    $db.sismember(self.class.persisted_key, db_key)
  end
end

#saveObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/redis_storage.rb', line 98

def save
  if @id.nil?
    @id = $db.incr(self.class.next_id_key)
  end
  if valid?
    $db.multi do
      $db.set db_key, to_json
      $db.sadd self.class.persisted_key, db_key
      indizies.each do |key|
        $db.sadd "#{self.class.db_key}:#{key}:#{send(key).hash}", db_key
      end
    end
    @id
  else
    nil
  end
end

#serializable_hashObject



81
82
83
84
85
86
# File 'lib/redis_storage.rb', line 81

def serializable_hash
  attributes.inject({:id => @id}) do |a,key|
    a[key] = send(key)
    a
  end
end

#to_jsonObject



87
88
89
# File 'lib/redis_storage.rb', line 87

def to_json
  serializable_hash.to_json
end

#update_attributes(params) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/redis_storage.rb', line 91

def update_attributes(params)
  delete!
  params.each do |key, value|
    send("#{key}=", value) unless key.to_sym == :id
  end
  save
end