Module: Validatable::ActiveModelInterface::ValidationBase

Defined in:
lib/validatable/active_model.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



60
61
62
# File 'lib/validatable/active_model.rb', line 60

def attributes
  [self.attribute]
end

#kindObject



33
34
35
# File 'lib/validatable/active_model.rb', line 33

def kind
  self.macro.to_s.match(/^[a-z]+_([a-z]+)/)[1].to_sym
end

#kind=(kind) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/validatable/active_model.rb', line 37

def kind=(kind)
  if kind.to_s == "associated"
    self.instance_variable_set('@macro', "validates_associated".to_sym)
  else
    self.instance_variable_set('@macro', "validates_#{kind}_of".to_sym)
  end
end

#macroObject



45
46
47
# File 'lib/validatable/active_model.rb', line 45

def macro
  @macro ||= underscore(self.class).to_sym
end

#underscore(camel_cased_word) ⇒ Object

Borrowed from ActiveSupport::Inflector



50
51
52
53
54
55
56
57
58
# File 'lib/validatable/active_model.rb', line 50

def underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word = word.split('::').last
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end