Module: HasTokenable::Concern::ClassMethods
- Defined in:
- lib/has_tokenable/concern.rb
Instance Method Summary collapse
-
#default_token_options ⇒ Object
Default options as well as an overwrite point so you can assign different defaults to different models.
- #generate_method(options) ⇒ Object
-
#generate_unique_token ⇒ Object
Generates a unique token based on the options.
- #method_random=(value) ⇒ Object
Instance Method Details
#default_token_options ⇒ Object
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 ||= begin = HasTokenable. [:prefix] ||= self.name[0, 1] end end |
#generate_method(options) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/has_tokenable/concern.rb', line 34 def generate_method() args = [ [:method_random], [:length].to_i ] args.slice!(-1) if [:method_random] == 'uuid' SecureRandom.send(*args) end |
#generate_unique_token ⇒ Object
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, = true, @has_tokenable_options conditions = {} token = loop do random_token = self.generate_method() conditions[[:param_name].to_sym] = random_token break random_token unless self.exists?(conditions) end token end |
#method_random=(value) ⇒ Object
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 |