Class: Spree::Core::NumberGenerator
- Inherits:
-
Module
- Object
- Module
- Spree::Core::NumberGenerator
- Defined in:
- lib/spree/core/number_generator.rb
Constant Summary collapse
- BASE =
10
- DEFAULT_LENGTH =
9
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) ⇒ NumberGenerator
constructor
A new instance of NumberGenerator.
Constructor Details
#initialize(options) ⇒ NumberGenerator
Returns a new instance of NumberGenerator.
9 10 11 12 13 |
# File 'lib/spree/core/number_generator.rb', line 9 def initialize() @prefix = .fetch(:prefix) @length = .fetch(:length, DEFAULT_LENGTH) @letters = [:letters] end |
Instance Attribute Details
#length ⇒ Object
Returns the value of attribute length.
7 8 9 |
# File 'lib/spree/core/number_generator.rb', line 7 def length @length end |
#prefix ⇒ Object
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/spree/core/number_generator.rb', line 7 def prefix @prefix end |
Instance Method Details
#included(host) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spree/core/number_generator.rb', line 15 def included(host) generator_method = method(:generate_permalink) generator_instance = self host.class_eval do validates(:number, presence: true, uniqueness: { allow_blank: true }) before_validation do |instance| instance.number ||= generator_method.call(host) end define_singleton_method(:number_generator) { generator_instance } end end |