Class: GatedRelease::Gate

Inherits:
ApplicationRecord show all
Defined in:
app/models/gated_release/gate.rb

Constant Summary collapse

OPEN =
'open'
CLOSED =
'closed'
LIMITED =
'limited'
PERCENTAGE =
'percentage'
STATES =
[OPEN, CLOSED, LIMITED, PERCENTAGE]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(name) ⇒ Object



24
25
26
# File 'app/models/gated_release/gate.rb', line 24

def self.get(name)
  find_or_create_by(name: name)
end

.search(query) ⇒ Object



19
20
21
22
# File 'app/models/gated_release/gate.rb', line 19

def self.search(query)
  query = "%#{query.tr('*','%')}%"
  where('name like ?', query).newest_first
end

Instance Method Details

#allow_more!(count) ⇒ Object



28
29
30
31
# File 'app/models/gated_release/gate.rb', line 28

def allow_more!(count)
  increment!(:max_attempts, count)
  self
end

#close!Object



38
39
40
41
# File 'app/models/gated_release/gate.rb', line 38

def close!
  update_attributes!(state: CLOSED)
  self
end

#limit!Object



43
44
45
46
# File 'app/models/gated_release/gate.rb', line 43

def limit!
  update_attributes!(state: LIMITED)
  self
end

#open!Object



33
34
35
36
# File 'app/models/gated_release/gate.rb', line 33

def open!
  update_attributes!(state: OPEN)
  self
end

#percentage!(value) ⇒ Object



48
49
50
51
# File 'app/models/gated_release/gate.rb', line 48

def percentage!(value)
  update_attributes!(state: PERCENTAGE, percent_open: value)
  self
end

#run(args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/gated_release/gate.rb', line 53

def run(args)
  if self.state == OPEN
    run_open_code(args)
  elsif self.state == PERCENTAGE && Kernel.rand(100) < self.percent_open
    run_open_code(args)
  elsif self.state == LIMITED && self.attempts < self.max_attempts
    increment_attempts
    run_open_code(args)
  else
    get_closed_code(args).call
  end
end