Class: SoftValidation::SoftValidations

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

Overview

and some code that tracks whether those validations have been fixed, etc.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SoftValidations) initialize(instance)

@param a instance of some ActiveRecord model



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

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

Instance Attribute Details

- (Symbol) fixed

True if fix() has been called. Note that this does not imply that all SoftValidations have been fixed!

Returns:

  • (Symbol)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/soft_validation/soft_validations.rb', line 19

class SoftValidations
  attr_accessor :soft_validations, :instance, :validated, :fixes_run

  # @param[ActiveRecord] a instance of some ActiveRecord model
  def initialize(instance)
    @validated = false
    @fixes_run = false
    @instance = instance # Klass from here
    @soft_validations = []
  end

  # @param [Symbol] attribute a column attribute or :base
  # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong
  # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. 
  def add(attribute, message,  options = {}) 
    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, "invalid :fix_trigger" if !options[:fix_trigger].blank? && ![:all, :automatic, :requested].include?(options[:fix_trigger])
    return false if attribute.nil? || message.nil? || message.length == 0
    return false if (options[:success_message] || options[:failure_message]) && !options[:fix]

    options[:attribute] = attribute
    options[:message] = message

    options[:resolution] = resolution_for(options[:resolution_with])
    options.delete(:resolution_with)

    sv = SoftValidation.new(options)
    sv.fix_trigger ||= :automatic

    @soft_validations << sv
  end

 #  def soft_validations(scope = :all)
 #    set = ( scope == :all ? [:automatic, :requested] : [scope] )
 #    @soft_validations.select{|v| set.include?(v.fix_trigger)}
 #  end

  # @return [Boolean]
  #   soft validations have been run
  def validated?
    @validated
  end

  def resolution_for(method)
    return [] if method.nil?
    self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution
  end

  # @return [Boolean]
  #   fixes on resultant soft validations have been run
  def fixes_run?
    @fixes_run
  end

  # @return [Boolean]
  #   soft validations run and none were generated 
  def complete?
    validated? && soft_validations.count == 0 
  end

  # @return [Hash<attribute><Array>]
  #   a hash listing the results of the fixes 
  def fix_messages
    messages = {}
    if fixes_run?
      soft_validations.each do |v| 
        messages[v.attribute] ||= []
        messages[v.attribute] << (v.result_message) 
      end
    end
    messages
  end

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

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

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

- (Object) fixes_run

Returns the value of attribute fixes_run



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

def fixes_run
  @fixes_run
end

- (Object) instance

the object being validated, an instance of an ActiveRecord model



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/soft_validation/soft_validations.rb', line 19

class SoftValidations
  attr_accessor :soft_validations, :instance, :validated, :fixes_run

  # @param[ActiveRecord] a instance of some ActiveRecord model
  def initialize(instance)
    @validated = false
    @fixes_run = false
    @instance = instance # Klass from here
    @soft_validations = []
  end

  # @param [Symbol] attribute a column attribute or :base
  # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong
  # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. 
  def add(attribute, message,  options = {}) 
    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, "invalid :fix_trigger" if !options[:fix_trigger].blank? && ![:all, :automatic, :requested].include?(options[:fix_trigger])
    return false if attribute.nil? || message.nil? || message.length == 0
    return false if (options[:success_message] || options[:failure_message]) && !options[:fix]

    options[:attribute] = attribute
    options[:message] = message

    options[:resolution] = resolution_for(options[:resolution_with])
    options.delete(:resolution_with)

    sv = SoftValidation.new(options)
    sv.fix_trigger ||= :automatic

    @soft_validations << sv
  end

 #  def soft_validations(scope = :all)
 #    set = ( scope == :all ? [:automatic, :requested] : [scope] )
 #    @soft_validations.select{|v| set.include?(v.fix_trigger)}
 #  end

  # @return [Boolean]
  #   soft validations have been run
  def validated?
    @validated
  end

  def resolution_for(method)
    return [] if method.nil?
    self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution
  end

  # @return [Boolean]
  #   fixes on resultant soft validations have been run
  def fixes_run?
    @fixes_run
  end

  # @return [Boolean]
  #   soft validations run and none were generated 
  def complete?
    validated? && soft_validations.count == 0 
  end

  # @return [Hash<attribute><Array>]
  #   a hash listing the results of the fixes 
  def fix_messages
    messages = {}
    if fixes_run?
      soft_validations.each do |v| 
        messages[v.attribute] ||= []
        messages[v.attribute] << (v.result_message) 
      end
    end
    messages
  end

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

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

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

- (Array) soft_validations

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

Returns:

  • (Array)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/soft_validation/soft_validations.rb', line 19

class SoftValidations
  attr_accessor :soft_validations, :instance, :validated, :fixes_run

  # @param[ActiveRecord] a instance of some ActiveRecord model
  def initialize(instance)
    @validated = false
    @fixes_run = false
    @instance = instance # Klass from here
    @soft_validations = []
  end

  # @param [Symbol] attribute a column attribute or :base
  # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong
  # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. 
  def add(attribute, message,  options = {}) 
    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, "invalid :fix_trigger" if !options[:fix_trigger].blank? && ![:all, :automatic, :requested].include?(options[:fix_trigger])
    return false if attribute.nil? || message.nil? || message.length == 0
    return false if (options[:success_message] || options[:failure_message]) && !options[:fix]

    options[:attribute] = attribute
    options[:message] = message

    options[:resolution] = resolution_for(options[:resolution_with])
    options.delete(:resolution_with)

    sv = SoftValidation.new(options)
    sv.fix_trigger ||= :automatic

    @soft_validations << sv
  end

 #  def soft_validations(scope = :all)
 #    set = ( scope == :all ? [:automatic, :requested] : [scope] )
 #    @soft_validations.select{|v| set.include?(v.fix_trigger)}
 #  end

  # @return [Boolean]
  #   soft validations have been run
  def validated?
    @validated
  end

  def resolution_for(method)
    return [] if method.nil?
    self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution
  end

  # @return [Boolean]
  #   fixes on resultant soft validations have been run
  def fixes_run?
    @fixes_run
  end

  # @return [Boolean]
  #   soft validations run and none were generated 
  def complete?
    validated? && soft_validations.count == 0 
  end

  # @return [Hash<attribute><Array>]
  #   a hash listing the results of the fixes 
  def fix_messages
    messages = {}
    if fixes_run?
      soft_validations.each do |v| 
        messages[v.attribute] ||= []
        messages[v.attribute] << (v.result_message) 
      end
    end
    messages
  end

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

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

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

- (Boolean) validated

True if the soft validations methods have been called.

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/soft_validation/soft_validations.rb', line 19

class SoftValidations
  attr_accessor :soft_validations, :instance, :validated, :fixes_run

  # @param[ActiveRecord] a instance of some ActiveRecord model
  def initialize(instance)
    @validated = false
    @fixes_run = false
    @instance = instance # Klass from here
    @soft_validations = []
  end

  # @param [Symbol] attribute a column attribute or :base
  # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong
  # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. 
  def add(attribute, message,  options = {}) 
    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, "invalid :fix_trigger" if !options[:fix_trigger].blank? && ![:all, :automatic, :requested].include?(options[:fix_trigger])
    return false if attribute.nil? || message.nil? || message.length == 0
    return false if (options[:success_message] || options[:failure_message]) && !options[:fix]

    options[:attribute] = attribute
    options[:message] = message

    options[:resolution] = resolution_for(options[:resolution_with])
    options.delete(:resolution_with)

    sv = SoftValidation.new(options)
    sv.fix_trigger ||= :automatic

    @soft_validations << sv
  end

 #  def soft_validations(scope = :all)
 #    set = ( scope == :all ? [:automatic, :requested] : [scope] )
 #    @soft_validations.select{|v| set.include?(v.fix_trigger)}
 #  end

  # @return [Boolean]
  #   soft validations have been run
  def validated?
    @validated
  end

  def resolution_for(method)
    return [] if method.nil?
    self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution
  end

  # @return [Boolean]
  #   fixes on resultant soft validations have been run
  def fixes_run?
    @fixes_run
  end

  # @return [Boolean]
  #   soft validations run and none were generated 
  def complete?
    validated? && soft_validations.count == 0 
  end

  # @return [Hash<attribute><Array>]
  #   a hash listing the results of the fixes 
  def fix_messages
    messages = {}
    if fixes_run?
      soft_validations.each do |v| 
        messages[v.attribute] ||= []
        messages[v.attribute] << (v.result_message) 
      end
    end
    messages
  end

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

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

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

Instance Method Details

- (Object) add(attribute, message, options = {})

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{fix: :method_name, success_message: String, failure_message: String }) (defaults to: {})

    the method identified by :fix should fully resolve the SoftValidation.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/soft_validation/soft_validations.rb', line 33

def add(attribute, message,  options = {}) 
  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, "invalid :fix_trigger" if !options[:fix_trigger].blank? && ![:all, :automatic, :requested].include?(options[:fix_trigger])
  return false if attribute.nil? || message.nil? || message.length == 0
  return false if (options[:success_message] || options[:failure_message]) && !options[:fix]

  options[:attribute] = attribute
  options[:message] = message

  options[:resolution] = resolution_for(options[:resolution_with])
  options.delete(:resolution_with)

  sv = SoftValidation.new(options)
  sv.fix_trigger ||= :automatic

  @soft_validations << sv
end

- (Boolean) complete?

Returns soft validations run and none were generated

Returns:

  • (Boolean)

    soft validations run and none were generated



75
76
77
# File 'lib/soft_validation/soft_validations.rb', line 75

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

- (Hash<attribute><Array>) fix_messages

Returns a hash listing the results of the fixes

Returns:

  • (Hash<attribute><Array>)

    a hash listing the results of the fixes



81
82
83
84
85
86
87
88
89
90
# File 'lib/soft_validation/soft_validations.rb', line 81

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

- (Boolean) fixes_run?

Returns fixes on resultant soft validations have been run

Returns:

  • (Boolean)

    fixes on resultant soft validations have been run



69
70
71
# File 'lib/soft_validation/soft_validations.rb', line 69

def fixes_run?
  @fixes_run
end

- (Object) messages



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

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

- (Object) messages_on(attribute)



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

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

- (Object) on(attribute)



92
93
94
# File 'lib/soft_validation/soft_validations.rb', line 92

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

- (Object) resolution_for(method)



62
63
64
65
# File 'lib/soft_validation/soft_validations.rb', line 62

def resolution_for(method)
  return [] if method.nil?
  self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution
end

- (Boolean) validated?

Returns soft validations have been run

Returns:

  • (Boolean)

    soft validations have been run



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

def validated?
  @validated
end