Module: CodeGenerator::Tokenify::ClassMethods

Defined in:
lib/code_generator/tokenify.rb

Instance Method Summary collapse

Instance Method Details

#tokenify!(field, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/code_generator/tokenify.rb', line 6

def tokenify!(field, opts = {})
  opts[:uniqueness] = { model: self.to_s.underscore, 
                        field: field } if opts.delete(:unique)
  
  after_initialize do |record|
    if record.send(field).blank?
      token = CodeGenerator::Generator.generate(opts)
      record.send("#{field}=", token)
    end
  end
end