module AttrSequence
Constants
- SequenceColumnExists
Public Class Methods
configuration()
click to toggle source
# File lib/attr_sequence.rb, line 16 def self.configuration @@configuration || configure end
configure() { |configuration| ... }
click to toggle source
# File lib/attr_sequence.rb, line 10 def self.configure @@configuration = Configuration.new yield(configuration) if block_given? configuration end
method_missing(method_sym, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/attr_sequence.rb, line 20 def self.method_missing(method_sym, *arguments, &block) if configuration.respond_to?(method_sym) configuration.send(method_sym) else super end end
respond_to?(method_sym, include_private = false)
click to toggle source
Calls superclass method
# File lib/attr_sequence.rb, line 28 def self.respond_to?(method_sym, include_private = false) if configuration.respond_to?(method_sym, include_private) true else super end end
Public Instance Methods
attr_sequence(options = {})
click to toggle source
Public: Defines ActiveRecord callbacks to set a sequential number scoped on a specific class.
Can be called multiple times to add hooks for different column names.
options - The Hash of options for configuration:
:scope - The Symbol representing the columm on which the number should be scoped (default: nil) :column - The Symbol representing the column that stores the number (default: :number) :start_at - The Integer value at which the sequence should start (default: 1) :skip - Skips the number generation when the lambda expression evaluates to nil. Gets passed the model object
Examples
class Answer < ActiveRecord::Base include AttrSequence belongs_to :question attr_sequence scope: :question_id end
Returns nothing.
# File lib/attr_sequence/attr_sequence.rb, line 37 def attr_sequence(options = {}) unless defined?(sequence_options) mattr_accessor :sequence_options, instance_accessor: false self.sequence_options = [] before_save :set_numbers end default_options = {column: AttrSequence.column, start_at: AttrSequence.start_at} options = default_options.merge(options) column_name = options[:column] if sequence_options.any? {|options| options[:column] == column_name} raise(SequenceColumnExists, <<-MSG.squish) Tried to set #{column_name} as a sequence but there was already a definition here. Did you accidentally call attr_sequence multiple times on the same column? MSG else sequence_options << options end end
Private Instance Methods
set_numbers()
click to toggle source
# File lib/attr_sequence/attr_sequence.rb, line 63 def set_numbers self.class.base_class.sequence_options.each do |options| AttrSequence::Generator.new(self, options).set end end