Class: SoftValidation::SoftValidations

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

Overview

A SoftValidations instance contains a set of SoftValidation(s). It tracks whether the validations (set) have been run and fixed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ SoftValidations

@param a instance of some ActiveRecord model



24
25
26
27
28
29
# File 'lib/soft_validation/soft_validations.rb', line 24

def initialize(instance)
  @validated = false
  @fixes_run = false
  @instance = instance # Klass from here <- stupid
  @soft_validations = []
end

Instance Attribute Details

#fixes_runObject

Returns the value of attribute fixes_run.



17
18
19
# File 'lib/soft_validation/soft_validations.rb', line 17

def fixes_run
  @fixes_run
end

#instanceObject

the object being validated, an instance of an ActiveRecord model



21
# File 'lib/soft_validation/soft_validations.rb', line 21

attr_writer :instance

#soft_validationsArray

the set of SoftValidations (i.e. problems with a record/instance)

Returns:

  • (Array)


10
11
12
# File 'lib/soft_validation/soft_validations.rb', line 10

def soft_validations
  @soft_validations
end

#validatedBoolean

True if the soft validations methods have been called.

Returns:

  • (Boolean)


15
16
17
# File 'lib/soft_validation/soft_validations.rb', line 15

def validated
  @validated
end

Instance Method Details

#add(attribute, message, options = {}) ⇒ Object

Add a soft validation to a data instance.

Parameters:

  • attribute (Symbol)

    a column attribute or :base

  • message (String)

    a message describing the soft validation to the user, i.e. what has gone wrong

  • options (Hash) (defaults to: {})

    legal keys are :failure_message and :success_message

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/soft_validation/soft_validations.rb', line 39

def add(attribute, message, options = {})
  # this is impossible to test.
  method = caller[0][/`(block\ in\ )*([^']*)'/, 2].to_sym # janky, the caller of this method, that is the method referenced in `soft_validate()`, used to get the fix for this Instance added

  raise SoftValidationError, "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s)
  raise SoftValidationError, 'no :attribute or message provided to soft validation' if attribute.nil? || message.nil? || message.length == 0

  options.merge!(
    method_instance: @instance.class.soft_validation_methods[method], # inspected to expose method values
    attribute: attribute,
    message: message,
  )

  sv = ::SoftValidation::SoftValidation.new(options)

  @soft_validations << sv
end

#complete?Boolean

Returns soft validations run and none were generated.

Returns:

  • (Boolean)

    soft validations run and none were generated



71
72
73
# File 'lib/soft_validation/soft_validations.rb', line 71

def complete?
  validated? && soft_validations.count == 0
end

#fix_messagesHash<attribute><Array>

Returns a hash listing the results of the fixes.

Returns:

  • (Hash<attribute><Array>)

    a hash listing the results of the fixes



77
78
79
80
81
82
83
84
85
86
# File 'lib/soft_validation/soft_validations.rb', line 77

def fix_messages
  messages = {}
  if fixes_run?
    soft_validations.each do |v|
      messages[v.attribute] ||= []
      messages[v.attribute].push << v.result_message
    end
  end
  messages
end

#fixes_run?Boolean

Returns fixes on resultant soft validations have been run.

Returns:

  • (Boolean)

    fixes on resultant soft validations have been run



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

def fixes_run?
  @fixes_run
end

#messagesArray

Returns:

  • (Array)


95
96
97
# File 'lib/soft_validation/soft_validations.rb', line 95

def messages
  soft_validations.collect{ |v| v.message}
end

#messages_on(attribute) ⇒ Array

Parameters:

  • attribute (Symbol)

Returns:

  • (Array)


101
102
103
# File 'lib/soft_validation/soft_validations.rb', line 101

def messages_on(attribute)
  on(attribute).collect{|v| v.message}
end

#on(attribute) ⇒ Array

Parameters:

  • attribute (Symbol)

Returns:

  • (Array)


90
91
92
# File 'lib/soft_validation/soft_validations.rb', line 90

def on(attribute)
  soft_validations.select{|v| v.attribute == attribute}
end

#sizeObject



105
106
107
# File 'lib/soft_validation/soft_validations.rb', line 105

def size
  soft_validations.size
end

#validated?Boolean

Returns soft validations have been run.

Returns:

  • (Boolean)

    soft validations have been run



59
60
61
# File 'lib/soft_validation/soft_validations.rb', line 59

def validated?
  @validated
end