redis_assist 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bcf176574c0e2ac94dcb13dbe7eeeb0f30f3784
4
- data.tar.gz: 4829e9b18420dad2a506703bff439fb4c28b7484
3
+ metadata.gz: aa85c951b1af2427edd4810a0f66a741d105d09e
4
+ data.tar.gz: 7ed0c449fc3dbfe11e3a033ad4ca78eb03857b0f
5
5
  SHA512:
6
- metadata.gz: d0b80cd10a09fdc88d3e2e0e1566134e60cc5c7a0a62b1456ece2dbe6427ad6feb5d458554a963d0ed952fe6fc389ef5313613b74dcb8065bd3429396c198fd4
7
- data.tar.gz: aa51bd8a86fb59e38d92439435a55cffbe42ed032a49094a3657a448bc0390756cbe74f5dc89b8763bf190b101618873395aaeaf816987dd411b077512dbcae7
6
+ metadata.gz: 3525dbfaefc4d333297400e9852cab71a959a499cf6e58f5d1d8ae8efab896758fb36effe1aebbb8d1e88a782386119bc732ce8961ef23dd52129504e863aa0c
7
+ data.tar.gz: d3839bb4921a10d891847cd76789f96e3d40a6df49938c6aa70b3747caf709897f1414db45e13f35d31e2e5ecf3cde1f13f2658fddeb4fb50916238f02839cfb
@@ -2,6 +2,7 @@ module RedisAssist
2
2
  class Base
3
3
 
4
4
  include Callbacks
5
+ include Validations
5
6
 
6
7
  before_create {|instance| instance.created_at = Time.now.to_f if instance.respond_to?(:created_at) }
7
8
  before_update {|instance| instance.updated_at = Time.now.to_f if instance.respond_to?(:updated_at) }
@@ -52,6 +53,8 @@ module RedisAssist
52
53
  redis.exists(key_for(id, :attributes))
53
54
  end
54
55
 
56
+ # TODO: needs a refactor. Should this be an interface for skipping validations?
57
+ # Should we optimize and skip the find? Support an array of ids?
55
58
  def update(id, params={})
56
59
  record = find(id)
57
60
  return false unless record
@@ -210,13 +213,12 @@ module RedisAssist
210
213
  end
211
214
 
212
215
  attr_accessor :attributes
213
- attr_reader :id, :errors
216
+ attr_reader :id
214
217
 
215
218
  def initialize(attrs={})
216
219
  @attributes = {}
217
220
  self.lists = {}
218
221
  self.hashes = {}
219
- self.errors = []
220
222
 
221
223
  if attrs[:id]
222
224
  self.id = attrs[:id]
@@ -242,7 +244,7 @@ module RedisAssist
242
244
  end
243
245
 
244
246
  def save
245
- return false unless valid?
247
+ return false unless valid?
246
248
 
247
249
  invoke_callback(:before_update) unless new_record?
248
250
  invoke_callback(:before_create) if new_record?
@@ -283,12 +285,9 @@ module RedisAssist
283
285
 
284
286
  def valid?
285
287
  invoke_callback(:before_validation)
286
- validate
287
- errors.empty?
288
+ super
288
289
  end
289
290
 
290
- def validate; end
291
-
292
291
  # TODO: should this be a redis-assist feature?
293
292
  def deleted?
294
293
  return false unless respond_to?(:deleted_at)
@@ -319,16 +318,13 @@ module RedisAssist
319
318
  def redis
320
319
  self.class.redis
321
320
  end
322
-
323
- def add_error(field, message)
324
- errors << { field => message }
325
- end
321
+
326
322
 
327
323
 
328
324
  protected
329
325
 
330
326
 
331
- attr_writer :id, :errors
327
+ attr_writer :id
332
328
  attr_accessor :lists, :hashes, :new_record
333
329
 
334
330
 
@@ -0,0 +1,53 @@
1
+ module RedisAssist
2
+ module Validations
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def validate
8
+ errors.clear
9
+ self.class.validations.each {|validation| validation.call(self) }
10
+ end
11
+
12
+ def valid?
13
+ validate
14
+ errors.empty?
15
+ end
16
+
17
+ def errors
18
+ @errors ||= []
19
+ end
20
+
21
+ def add_error(attribute, message)
22
+ self.errors << { attribute => message }
23
+ end
24
+
25
+ module ClassMethods
26
+ def validations
27
+ @validations ||= []
28
+ end
29
+
30
+ def validates_presence_of(*attributes)
31
+ validates_attributes(*attributes) do |instance, attribute, value, options|
32
+ instance.add_error(attribute, "cant't be blank") if value.nil? || value.empty?
33
+ end
34
+ end
35
+
36
+ def validates_format_of(*attributes)
37
+ validates_attributes(*attributes) do |instance, attribute, value, options|
38
+ instance.add_error(attribute, "is invalid") unless value =~ options[:with]
39
+ end
40
+ end
41
+
42
+ def validates_attributes(*attributes, &proc)
43
+ options = attributes.last.is_a?(::Hash) ? attributes.pop : {}
44
+
45
+ validations << Proc.new do |instance|
46
+ attributes.each do |attribute|
47
+ proc.call(instance, attribute, instance.__send__(attribute), options)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module RedisAssist
2
- VERSION = '0.2.0' unless defined?(::RedisAssist::VERSION)
2
+ VERSION = '0.3.0' unless defined?(::RedisAssist::VERSION)
3
3
  end
data/lib/redis_assist.rb CHANGED
@@ -7,6 +7,7 @@ require 'json'
7
7
  require 'redis_assist/config'
8
8
  require 'redis_assist/transform'
9
9
  require 'redis_assist/callbacks'
10
+ require 'redis_assist/validations'
10
11
  require 'redis_assist/base'
11
12
 
12
13
  # == Setup & Configuration
@@ -26,5 +27,4 @@ module RedisAssist
26
27
  end
27
28
 
28
29
  # require all transforms
29
- Dir["#{File.dirname(__FILE__)}/redis_assist/transforms/*.rb"].each {|file| require file }
30
-
30
+ Dir["#{File.dirname(__FILE__)}/redis_assist/transforms/*.rb"].each{|file| require file }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_assist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Love
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-19 00:00:00.000000000 Z
11
+ date: 2013-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -138,6 +138,7 @@ files:
138
138
  - lib/redis_assist/transforms/integer_transform.rb
139
139
  - lib/redis_assist/transforms/json_transform.rb
140
140
  - lib/redis_assist/transforms/time_transform.rb
141
+ - lib/redis_assist/validations.rb
141
142
  - lib/redis_assist/version.rb
142
143
  - lib/redis_assist.rb
143
144
  homepage: http://github.com/endlessinc/redis-assist