Class: ForeverAlone::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/forever-alone/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, timeout = nil) ⇒ Validator

Returns a new instance of Validator.



6
7
8
9
# File 'lib/forever-alone/validator.rb', line 6

def initialize message, timeout=nil
  @message = message
  @timeout = timeout || ForeverAlone.configuration.timeout
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/forever-alone/validator.rb', line 4

def message
  @message
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



4
5
6
# File 'lib/forever-alone/validator.rb', line 4

def timeout
  @timeout
end

Instance Method Details

#ensureObject



24
25
26
27
# File 'lib/forever-alone/validator.rb', line 24

def ensure
  raise ForeverAlone::MessageIsNotUnique, self.inspect unless unique?
  remember
end

#flush_locksObject



15
16
17
18
# File 'lib/forever-alone/validator.rb', line 15

def flush_locks
  keys = ForeverAlone.redis.keys "#{ ForeverAlone.configuration.namespace }:*"
  ForeverAlone.redis.del keys if keys.any?
end

#keyObject



29
30
31
# File 'lib/forever-alone/validator.rb', line 29

def key
  @key ||= generate_key
end

#rememberObject



20
21
22
# File 'lib/forever-alone/validator.rb', line 20

def remember
  ForeverAlone.redis.setex key, timeout, 'foo'
end

#unique?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/forever-alone/validator.rb', line 11

def unique?
  ForeverAlone.redis.get(key).nil?
end