Class: TokenMaster::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/token_master/config.rb

Overview

TokenMaster::Config manages the configuration options for tokenable actions. These can be set with the initializer provided with the generator. Default values will be used if options are not specified

Constant Summary collapse

DEFAULT_VALUES =

Provides default values for a tokenable action:

  • token_lifetime is an integer representing the number of days before the token expires
  • required_params is an array of symbols, e.g., [:password, :password_confirmation]
  • token_length is an integer representing the number of characters in the token
{
  token_lifetime: 14,
  required_params: [],
  token_length: 20
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Creates a new instance of TokenMaster::Config



18
19
20
# File 'lib/token_master/config.rb', line 18

def initialize
  @options = {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/token_master/config.rb', line 15

def options
  @options
end

Instance Method Details

#add_tokenable_options(key, **params) ⇒ Object

Sets the key-value pairs needed to complete a tokenable action
Key-value pairs used to complete a tokenable action are the token_lifetime, required_params (can be blank), and token_length

Examples:

Set a Tokenable Option

config.add_tokenable_options(:invite, { token_lifetime: 10, required_params: [:password, :password_confirmation], token_length: 12 }) #=>
{ invite: {
    token_lifetime: 10,
    required_params: [:password, :password_confirmation],
    token_length: 12
   }
 }


33
34
35
# File 'lib/token_master/config.rb', line 33

def add_tokenable_options(key, **params)
  @options[key] = params
end

#get_required_params(key) ⇒ Array

Retrieves the required_params for a tokenable_action, either as set by the application, or by the default
Used to update model attributes as needed for a given tokenable action



40
41
42
# File 'lib/token_master/config.rb', line 40

def get_required_params(key)
  get_option(key, :required_params)
end

#get_token_length(key) ⇒ Integer

Retrieves the token_length for a tokenable action, either as set by the application, or by the default



54
55
56
# File 'lib/token_master/config.rb', line 54

def get_token_length(key)
  get_option(key, :token_length)
end

#get_token_lifetime(key) ⇒ Integer

Retrieves the token_lifetime for a tokenable action, either as set by the application, or by the default



47
48
49
# File 'lib/token_master/config.rb', line 47

def get_token_lifetime(key)
  get_option(key, :token_lifetime)
end

#options_set?(key) ⇒ Boolean

Determines whether options are provided for a tokenable action



61
62
63
# File 'lib/token_master/config.rb', line 61

def options_set?(key)
  @options.key? key
end