Module: Mongoid::Autoinc
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/autoinc.rb,
lib/autoinc/version.rb,
lib/autoinc/incrementor.rb
Overview
Include module to allow defining of autoincrementing fields.
Defined Under Namespace
Modules: ClassMethods Classes: Incrementor
Constant Summary collapse
- AlreadyAssignedError =
Class.new(StandardError)
- AutoIncrementsError =
Class.new(StandardError)
- VERSION =
'6.0.4'.freeze
Instance Method Summary collapse
-
#assign!(field) ⇒ Fixnum
Manually assign the next number to the passed autoinc field.
-
#evaluate_scope(scope) ⇒ Object
Asserts the validity of the passed scope.
-
#evaluate_step(step) ⇒ Integer
Returns the number to add to the current increment.
-
#evaluate_step_proc(step_proc) ⇒ Integer
Executes a proc and returns its
Integer
value. -
#increment!(field, options) ⇒ true
Set autoincrement value for the passed autoincrement field, using the passed options.
-
#update_auto_increments ⇒ true
Sets autoincrement values for all autoincrement fields.
Instance Method Details
#assign!(field) ⇒ Fixnum
Manually assign the next number to the passed autoinc field.
in the increments call for ‘field`
82 83 84 85 86 87 |
# File 'lib/autoinc.rb', line 82 def assign!(field) = self.class.incrementing_fields[field] fail AutoIncrementsError if [:auto] fail AlreadyAssignedError if send(field).present? increment!(field, ) end |
#evaluate_scope(scope) ⇒ Object
Asserts the validity of the passed scope
123 124 125 126 127 |
# File 'lib/autoinc.rb', line 123 def evaluate_scope(scope) return send(scope) if scope.is_a? Symbol return instance_exec(&scope) if scope.is_a? Proc fail ArgumentError, 'scope is not a Symbol or a Proc' end |
#evaluate_step(step) ⇒ Integer
Returns the number to add to the current increment
or Proc
to be evaluated
137 138 139 140 141 |
# File 'lib/autoinc.rb', line 137 def evaluate_step(step) return step if step.is_a? Integer return evaluate_step_proc(step) if step.is_a? Proc fail ArgumentError, 'step is not an Integer or a Proc' end |
#evaluate_step_proc(step_proc) ⇒ Integer
Executes a proc and returns its Integer
value
150 151 152 153 154 |
# File 'lib/autoinc.rb', line 150 def evaluate_step_proc(step_proc) result = instance_exec(&step_proc) return result if result.is_a? Integer fail 'step Proc does not evaluate to an Integer' end |
#increment!(field, options) ⇒ true
Set autoincrement value for the passed autoincrement field, using the passed options
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/autoinc.rb', line 105 def increment!(field, ) = .dup model_name = (.delete(:model_name) || self.class.model_name).to_s [:scope] = evaluate_scope([:scope]) if [:scope] [:step] = evaluate_step([:step]) if [:step] write_attribute( field.to_sym, Mongoid::Autoinc::Incrementor.new(model_name, field, ).inc ) end |
#update_auto_increments ⇒ true
Sets autoincrement values for all autoincrement fields.
92 93 94 95 96 |
# File 'lib/autoinc.rb', line 92 def update_auto_increments self.class.incrementing_fields.each do |field, | increment!(field, ) if [:auto] end && true end |