Class: DatashiftJourney::ReferenceGenerator
- Inherits:
-
Module
- Object
- Module
- DatashiftJourney::ReferenceGenerator
- Defined in:
- lib/datashift_journey/reference_generator.rb
Constant Summary collapse
- BASE =
10
- DEFAULT_LENGTH =
9
- NUMBERS =
(0..9).to_a.freeze
- LETTERS =
('A'..'Z').to_a.freeze
Instance Attribute Summary collapse
-
#length ⇒ Object
Returns the value of attribute length.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
- #included(host) ⇒ Object
-
#initialize(options) ⇒ ReferenceGenerator
constructor
A new instance of ReferenceGenerator.
Constructor Details
#initialize(options) ⇒ ReferenceGenerator
Returns a new instance of ReferenceGenerator.
11 12 13 14 15 16 |
# File 'lib/datashift_journey/reference_generator.rb', line 11 def initialize() @random = Random.new @prefix = .fetch(:prefix) @length = .fetch(:length, DEFAULT_LENGTH) @candidates = NUMBERS + ([:letters] ? LETTERS : []) end |
Instance Attribute Details
#length ⇒ Object
Returns the value of attribute length.
9 10 11 |
# File 'lib/datashift_journey/reference_generator.rb', line 9 def length @length end |
#prefix ⇒ Object
Returns the value of attribute prefix.
9 10 11 |
# File 'lib/datashift_journey/reference_generator.rb', line 9 def prefix @prefix end |
Instance Method Details
#included(host) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/datashift_journey/reference_generator.rb', line 18 def included(host) generator_method = method(:generate_permalink) generator_instance = self host.class_eval do validates(:reference, presence: true, uniqueness: { allow_blank: true }) before_validation do |instance| instance.reference ||= generator_method.call(host) end define_singleton_method(:reference_generator) { generator_instance } end end |