Module: SafetyCone::Configuration

Included in:
SafetyCone
Defined in:
lib/safety_cone/configuration.rb

Overview

Module for configuring safety measures

Constant Summary collapse

VALID_OPTION_KEYS =
i[method controller action name].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authObject

Returns the value of attribute auth.



6
7
8
# File 'lib/safety_cone/configuration.rb', line 6

def auth
  @auth
end

#conesObject

Returns the value of attribute cones.



6
7
8
# File 'lib/safety_cone/configuration.rb', line 6

def cones
  @cones
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/safety_cone/configuration.rb', line 6

def options
  @options
end

#redisObject

Returns the value of attribute redis.



6
7
8
# File 'lib/safety_cone/configuration.rb', line 6

def redis
  @redis
end

Instance Method Details

#add(options = {}) ⇒ Object

Method add a route or method to be managed by safety cone

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/safety_cone/configuration.rb', line 9

def add(options = {})
  self.options = options

  raise(ArgumentError, 'Mandatory param :name missing') unless options[:name]

  cones[make_key] = options
end

#configure {|_self| ... } ⇒ Object

Configuration method for Rails initializer

Yields:

  • (_self)

Yield Parameters:



30
31
32
33
34
35
# File 'lib/safety_cone/configuration.rb', line 30

def configure
  self.cones = {}
  self.auth = { username: nil, password: nil }

  yield self
end

#make_keyObject

Method to generate a key from the options



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

def make_key
  if options.key? :method
    options[:method].to_sym
  elsif options.include?(:controller) && options.include?(:action)
    "#{options[:controller]}_#{options[:action]}".to_sym
  else
    raise(ArgumentError,
          'Options should contain :controller and :action or :method.')
  end
end