lib/csv2hash/validator.rb in csv2hash-0.6.6 vs lib/csv2hash/validator.rb in csv2hash-0.6.7
- old
+ new
@@ -23,20 +23,17 @@
protected
def validate_cell y, x, cell
value = data_source[y][x] rescue nil
begin
- raise unless value unless cell.rules.fetch :allow_blank
- if (extra_validator = cell.rules.fetch :extra_validator) && extra_validator.kind_of?(ExtraValidator)
- raise unless extra_validator.valid? cell.rules, value
+ verify_blank! cell, value
+ if extra_validator?(cell)
+ verify_extra_validator! cell, value
else
- if value && (values = cell.rules.fetch :values)
- if values.class == Range
- raise unless values.include?(value.to_f)
- else
- raise unless values.include?(value)
- end
+ if rang? cell, value
+ values = cell.rules.fetch(:values)
+ verify_rang! values, value
end
end
rescue => e
raise message(cell, y, x, value)
end
@@ -52,30 +49,53 @@
msg
end
private
+ def extra_validator? cell
+ (extra_validator = cell.rules.fetch(:extra_validator)) && extra_validator.kind_of?(ExtraValidator)
+ end
+
+ def verify_extra_validator! cell, value
+ raise unless cell.rules.fetch(:extra_validator).valid? cell.rules, value
+ end
+
+ def verify_blank! cell, value
+ raise unless value unless cell.rules.fetch :allow_blank
+ end
+
+ def rang? cell, value
+ value && (values = cell.rules.fetch(:values))
+ end
+
+ def verify_rang! values, value
+ if values.class == Range
+ raise unless values.include?(value.to_f)
+ else
+ raise unless values.include?(value)
+ end
+ end
+
def find_or_remove_dynamic_fields!
cells = definition.cells.dup
- _cells = []
-
- while(!cells.empty?) do
- cell = cells.pop
- _y, x = cell.rules.fetch(:position)
- if dynamic_field?(_y)
- begin
- _cell = find_dynamic_position cell
- _cells << _cell if _cell
- rescue => e
- self.errors << { y: (_y||y), x: x, message: e.message, key: cell.rules.fetch(:key) }
- raise if break_on_failure
+ # cells without optional and not found dynamic field
+ definition.cells = [].tap do |_cells|
+ while(!cells.empty?) do
+ cell = cells.pop
+ _y, x = cell.rules.fetch(:position)
+ if dynamic_field?(_y)
+ begin
+ _cell = find_dynamic_position cell
+ _cells << _cell
+ rescue => e
+ self.errors << { y: (_y||y), x: x, message: e.message, key: cell.rules.fetch(:key) }
+ raise if break_on_failure
+ end
+ else
+ _cells << cell
end
- else
- _cells << cell
end
- end
-
- definition.cells = _cells # cells without optional and not found dynamic field
+ end.compact
nil
end
def dynamic_field? field
field.is_a?(Array)