Class: FlexScaffold::Validations

Inherits:
Object
  • Object
show all
Defined in:
lib/validations.rb

Constant Summary collapse

VALIDATIONS_INFO =
%w( message with on in maximum minimum too_short too_long )
SCHEMA_INFO =
%w( type limit default )

Instance Method Summary collapse

Constructor Details

#initialize(base = {}, attrs = {}) ⇒ Validations

:nodoc:



8
9
10
# File 'lib/validations.rb', line 8

def initialize(base={},attrs={}) # :nodoc:
  @record, @attrs, @schema_attrs = base, attrs, {}
end

Instance Method Details

#add(attr_name, type, configuration = {}) ⇒ Object

Adds a validation of an attribute (attr_name) to the record and returns the individual set that was added



14
15
16
17
18
# File 'lib/validations.rb', line 14

def add(attr_name, type, configuration={})
   attribute = add_type_to_hash(attr_name, type)
   VALIDATIONS_INFO.each do |rule| attribute.update(rule.to_s => configuration[rule.to_sym]) unless configuration[rule.to_sym].nil? end      
  @attrs
end

#clearObject

Removes all the validations that have been added



40
41
42
# File 'lib/validations.rb', line 40

def clear
  @attrs = {}
end

#eachObject

Yields each attribute and associated details per validation added



50
51
52
# File 'lib/validations.rb', line 50

def each
  @attrs.each_key { |attr| @attrs[attr].each { |msg| yield attr, msg } }
end

#empty?Boolean

Returns true if no validations have been added

Returns:

  • (Boolean)


55
56
57
# File 'lib/validations.rb', line 55

def empty?
  @attrs.empty?
end

#get_schema_info(class_name = @record) ⇒ Object

Returns the schema-based validation information of the attributes. This information includes the type, limit, and default values. It is categorised by the attribute name



28
29
30
31
32
33
34
35
36
37
# File 'lib/validations.rb', line 28

def get_schema_info(class_name=@record)
  klass = Object.const_get(class_name.to_s.camelize) rescue nil
  if klass && klass < ActiveRecord::Base
     klass.columns.each do |col|
        attribute = add_schema_to_hash(col.name)
        SCHEMA_INFO.each do |info| attribute.update(info => eval("col.#{info}.to_s")) end
    end
  end
  @schema_attrs
end

#load_schema_info(class_name = @record) ⇒ Object

Adds the schema information into the validation of the attributes. This information includes the type, limit, and default values.



22
23
24
# File 'lib/validations.rb', line 22

def load_schema_info(class_name=@record)
  @attrs.merge(get_schema_info(class_name))
end

#sizeObject

Returns the number of attributes with validations



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

def size
  @attrs.size
end

#to_sObject

Returns the string of attributes with validations



65
66
67
# File 'lib/validations.rb', line 65

def to_s
  @attrs.to_s
end

#to_xmlObject

Returns an XML representation of the validation object



70
71
72
# File 'lib/validations.rb', line 70

def to_xml
  @attrs.to_xml
end

#validationsObject

Returns the attribute validations



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

def validations
  @attrs
end