Module: HasTokenable::Concern::ClassMethods

Defined in:
lib/has_tokenable/concern.rb

Instance Method Summary collapse

Instance Method Details

#default_token_optionsObject

Default options as well as an overwrite point so you can assign different defaults to different models



26
27
28
29
30
31
32
# File 'lib/has_tokenable/concern.rb', line 26

def default_token_options
  @default_token_options ||= begin
    options = HasTokenable.default_token_options
    options[:prefix] ||= self.name[0, 1]
    options
  end
end

#generate_method(options) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/has_tokenable/concern.rb', line 34

def generate_method(options)
  args = [ options[:method_random], options[:length].to_i ]
  
  args.slice!(-1) if options[:method_random] == 'uuid'

  SecureRandom.send(*args)
end

#generate_unique_tokenObject

Generates a unique token based on the options



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/has_tokenable/concern.rb', line 43

def generate_unique_token
  record, options = true, @has_tokenable_options
  conditions = {}

  token = loop do
    random_token = self.generate_method(options)

    conditions[options[:param_name].to_sym] = random_token

    break random_token unless self.exists?(conditions)
  end

  token
end

#method_random=(value) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/has_tokenable/concern.rb', line 17

def method_random=(value)
  values = value.is_a?(Array) ? value : [value]
  values.map! { |v| v.to_s }
  
  raise ArgumentError.new('Method is not supported') if (values - METHODS).size > 0
  values
end