Class: SoftValidation::SoftValidations

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

Overview

A SoftValidations instance contains a set of SoftValidations and some code that tracks whether those validations have been fixed, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ SoftValidations

@param a instance of some ActiveRecord model



98
99
100
101
102
103
# File 'lib/soft_validation.rb', line 98

def initialize(instance)
  @validated = false
  @fixes_run = false
  @instance = instance
  @soft_validations = []
end

Instance Attribute Details

#fixedSymbol

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

Returns:

  • (Symbol)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/soft_validation.rb', line 94

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
    @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 = {})
    # TODO: Stub a generic TW Error and raise it instead
    raise "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s)
    raise "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]
    sv = SoftValidation.new
    sv.attribute = attribute 
    sv.message = message
    sv.fix = options[:fix]
    sv.fix_trigger = options[:fix_trigger]
    sv.fix_trigger ||= :automatic
    sv.success_message = options[:success_message] 
    sv.failure_message = options[:failure_message] 
    @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

  # @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

#fixes_runObject

Returns the value of attribute fixes_run



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

def fixes_run
  @fixes_run
end

#instanceObject

the object being validated, an instance of an ActiveRecord model



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/soft_validation.rb', line 94

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
    @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 = {})
    # TODO: Stub a generic TW Error and raise it instead
    raise "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s)
    raise "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]
    sv = SoftValidation.new
    sv.attribute = attribute 
    sv.message = message
    sv.fix = options[:fix]
    sv.fix_trigger = options[:fix_trigger]
    sv.fix_trigger ||= :automatic
    sv.success_message = options[:success_message] 
    sv.failure_message = options[:failure_message] 
    @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

  # @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

#soft_validationsArray

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

Returns:

  • (Array)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/soft_validation.rb', line 94

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
    @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 = {})
    # TODO: Stub a generic TW Error and raise it instead
    raise "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s)
    raise "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]
    sv = SoftValidation.new
    sv.attribute = attribute 
    sv.message = message
    sv.fix = options[:fix]
    sv.fix_trigger = options[:fix_trigger]
    sv.fix_trigger ||= :automatic
    sv.success_message = options[:success_message] 
    sv.failure_message = options[:failure_message] 
    @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

  # @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

#validatedBoolean

True if the soft validations methods have been called.

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/soft_validation.rb', line 94

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
    @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 = {})
    # TODO: Stub a generic TW Error and raise it instead
    raise "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s)
    raise "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]
    sv = SoftValidation.new
    sv.attribute = attribute 
    sv.message = message
    sv.fix = options[:fix]
    sv.fix_trigger = options[:fix_trigger]
    sv.fix_trigger ||= :automatic
    sv.success_message = options[:success_message] 
    sv.failure_message = options[:failure_message] 
    @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

  # @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

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

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/soft_validation.rb', line 108

def add(attribute, message, options = {})
  # TODO: Stub a generic TW Error and raise it instead
  raise "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s)
  raise "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]
  sv = SoftValidation.new
  sv.attribute = attribute 
  sv.message = message
  sv.fix = options[:fix]
  sv.fix_trigger = options[:fix_trigger]
  sv.fix_trigger ||= :automatic
  sv.success_message = options[:success_message] 
  sv.failure_message = options[:failure_message] 
  @soft_validations << sv
end

#complete?Boolean

Returns soft validations run and none were generated

Returns:

  • (Boolean)

    soft validations run and none were generated



144
145
146
# File 'lib/soft_validation.rb', line 144

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



150
151
152
153
154
155
156
157
158
159
# File 'lib/soft_validation.rb', line 150

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

#fixes_run?Boolean

Returns fixes on resultant soft validations have been run

Returns:

  • (Boolean)

    fixes on resultant soft validations have been run



138
139
140
# File 'lib/soft_validation.rb', line 138

def fixes_run?
  @fixes_run
end

#messagesObject



165
166
167
# File 'lib/soft_validation.rb', line 165

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

#messages_on(attribute) ⇒ Object



169
170
171
# File 'lib/soft_validation.rb', line 169

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

#on(attribute) ⇒ Object



161
162
163
# File 'lib/soft_validation.rb', line 161

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

#validated?Boolean

Returns soft validations have been run

Returns:

  • (Boolean)

    soft validations have been run



132
133
134
# File 'lib/soft_validation.rb', line 132

def validated?
  @validated
end