Module: TokenizeAttributes::Attribute

Defined in:
lib/tokenize_attributes/attribute.rb

Instance Method Summary collapse

Instance Method Details

#tokenized_attribute(attrb, method = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tokenize_attributes/attribute.rb', line 9

def tokenized_attribute(attrb, method = nil)
  self.send(:define_method, "reset_tokenization_for_#{attrb}") do
    loop do
      token = method.try(:call) || TokenizeAttributes.configuration.tokenizer.call
      self.public_send("#{attrb}=", token)
    break unless self.class.where(attrb => token).exists?
    end
  end

  before_validation do |record|
    record.public_send("reset_tokenization_for_#{attrb}") if record.public_send(attrb).blank?
  end
end

#tokenized_attributes(*attrbs) ⇒ Object



4
5
6
7
# File 'lib/tokenize_attributes/attribute.rb', line 4

def tokenized_attributes(*attrbs)
  @method = attrbs.pop if attrbs.last.is_a?(Proc)
  attrbs.each { |attrb| tokenized_attribute(attrb, @method) }
end