Class: ActionHandle::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_handle/base.rb

Constant Summary collapse

BUILTIN_ADAPTERS =
{
  redis: Adapters::RedisPool,
  cache: Adapters::CacheStore
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, instance_value = nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(key, instance_value = nil)
  @key = key
  @instance_value = instance_value
end

Instance Attribute Details

#instance_valueObject (readonly)

Returns the value of attribute instance_value.



43
44
45
# File 'lib/action_handle/base.rb', line 43

def instance_value
  @instance_value
end

#keyObject (readonly)

Returns the value of attribute key.



43
44
45
# File 'lib/action_handle/base.rb', line 43

def key
  @key
end

Class Method Details

.claim(*args, &block) ⇒ Object



34
35
36
# File 'lib/action_handle/base.rb', line 34

def claim(*args, &block)
  new(*args, &block).claim
end

.create(*args, &block) ⇒ Object



22
23
24
# File 'lib/action_handle/base.rb', line 22

def create(*args, &block)
  new(*args, &block).create
end

.expire(*args, &block) ⇒ Object



26
27
28
# File 'lib/action_handle/base.rb', line 26

def expire(*args, &block)
  new(*args, &block).expire
end

.prefix(string = nil) ⇒ Object



11
12
13
14
# File 'lib/action_handle/base.rb', line 11

def prefix(string = nil)
  @prefix = string if string
  @prefix
end

.renew(*args, &block) ⇒ Object



30
31
32
# File 'lib/action_handle/base.rb', line 30

def renew(*args, &block)
  new(*args, &block).renew
end

.ttl(amount = nil) ⇒ Object



16
17
18
19
20
# File 'lib/action_handle/base.rb', line 16

def ttl(amount = nil)
  @ttl = amount if amount
  @ttl ||= 100
  @ttl
end

.value(*args, &block) ⇒ Object



38
39
40
# File 'lib/action_handle/base.rb', line 38

def value(*args, &block)
  new(*args, &block).value
end

Instance Method Details

#adapterObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/action_handle/base.rb', line 86

def adapter
  @adapter ||=
    case Configuration.adapter
    when Symbol, String
      klass = BUILTIN_ADAPTERS[Configuration.adapter.to_sym]

      klass&.new(*Configuration.redis_pool)
    else
      Configuration.adapter
    end
end

#claimObject



70
71
72
# File 'lib/action_handle/base.rb', line 70

def claim
  adapter.claim(handle_key, instance_value, ttl)
end

#createObject



50
51
52
# File 'lib/action_handle/base.rb', line 50

def create
  adapter.create(handle_key, instance_value, ttl)
end

#current?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/action_handle/base.rb', line 62

def current?
  adapter.current?(handle_key, instance_value)
end

#expireObject



74
75
76
# File 'lib/action_handle/base.rb', line 74

def expire
  adapter.expire(handle_key)
end

#handle_keyObject



82
83
84
# File 'lib/action_handle/base.rb', line 82

def handle_key
  ['AH', self.class.prefix, key].join('/')
end

#renewObject



54
55
56
# File 'lib/action_handle/base.rb', line 54

def renew
  adapter.renew(handle_key, instance_value, ttl)
end

#taken?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/action_handle/base.rb', line 58

def taken?
  adapter.taken?(handle_key)
end

#ttlObject



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

def ttl
  self.class.ttl
end

#valueObject



66
67
68
# File 'lib/action_handle/base.rb', line 66

def value
  adapter.value(handle_key)
end