Module: Anodator::Utils
- Defined in:
- lib/anodator/utils.rb
Class Method Summary collapse
-
.load_input_spec_from_csv_file(file_path) ⇒ Object
load input_spec from csv file.
-
.load_output_spec_from_csv_file(file_path, target = Anodator::OutputSpec::TARGET_ERROR, include_no_error = false) ⇒ Object
load output_spec from csv file.
-
.load_rule_from_csv_file(file_path, validators) ⇒ Object
load rule_set from csv file.
-
.load_validators_from_csv_file(file_path) ⇒ Object
load validators from csv file.
-
.options_reader(row, options = { }, options_index_from = 4) ⇒ Object
options reader for option values for validator specs.
Class Method Details
.load_input_spec_from_csv_file(file_path) ⇒ Object
load input_spec from csv file
File encoding: UTF-8 Columns:
- column identification(string)
- column name(string)
- column type(STRING, NUMERIC or DATE, default is STRING)
Arguments:
- file_path: file path for input spec.
Return:
InputSpec instance
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/anodator/utils.rb', line 16 def self.load_input_spec_from_csv_file(file_path) first = true header = nil spec = [] CSV.open(file_path, "r") do |row| # skip header if first header = row first = false next end spec << { :number => row[0], :name => row[1], :type => row[2] } end return InputSpec.new(spec) end |
.load_output_spec_from_csv_file(file_path, target = Anodator::OutputSpec::TARGET_ERROR, include_no_error = false) ⇒ Object
load output_spec from csv file
When target is TARGET_ERROR generate output line by one error, when target is TARGET_DATA generate output line by one data. When set include_no_error is true and target is TARGET_DATA, generate output line has no error.
File encoding: UTF-8 Columns:
- output column identification(string)
- output special column(string)
- target_numbers(for error)
- target_names(for one error)
- target_values(for one error)
- error_message(for one error)
- error_level(for one error)
- error_count(for one data)
- warning_count(for one data)
- error_and_warning_count(for one data)
Arguments:
- file_path: file path for output spec.
- target: OutputSpec::TARGET_ERROR or OutputSpec::TARGET_DATA
- include_no_error: true or false(default false)
Return:
OutputSpec instance
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/anodator/utils.rb', line 58 def self.load_output_spec_from_csv_file(file_path, target = Anodator::OutputSpec::TARGET_ERROR, include_no_error = false) first = true header = nil spec = [] CSV.open(file_path, "r") do |row| # skip header if first header = row first = false next end if row.first.nil? || row.first.split(//).length.zero? spec << row.last.to_sym else spec << row.first.to_s end end return Anodator::OutputSpec.new(spec, :target => target, :include_no_error => include_no_error) end |
.load_rule_from_csv_file(file_path, validators) ⇒ Object
load rule_set from csv file
File encoding: UTF-8 Columns:
- rule identification(string)
- rule description(string)
- target expression(column identification or column name)
- validator identification
- prerequisite validator identification(allow blank and multiple validator identification)
- error level(ERROR or WARNING)
- error message holder(string)
Return:
RuleSet instance
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/anodator/utils.rb', line 189 def self.load_rule_from_csv_file(file_path, validators) first = true header = nil rule_set = RuleSet.new CSV.open(file_path, "r") do |row| # skip header if first header = row first = false next end description = row[1] target_expression = row[2].split(",") validator = validators[row[3]] if !row[4].nil? && row[4].include?(",") prerequisite = row[4].split(",").map do |validator_id| raise "Unknown validator identifier '#{validator_id}'" if validators[validator_id].nil? next validators[validator_id] end else prerequisite = validators[row[4]] end if validator.nil? raise "Unknown validator identifier '#{row[3]}'" end if !row[4].nil? && prerequisite.nil? raise "Unknown validator identifier '#{row[4]}'" end if Rule::ERROR_LEVEL_NAMES.values.include?(row[5]) level = Rule::ERROR_LEVELS[Rule::ERROR_LEVEL_NAMES.index(row[5])] else raise "Unknown error type '#{row[5]}'" end = Message.new(row[6]) rule_set << Rule.new(target_expression, , validator, prerequisite, level, description) end return rule_set end |
.load_validators_from_csv_file(file_path) ⇒ Object
load validators from csv file
File encoding: UTF-8 Columns:
- validator identification(string)
- validation name(string)
- validator type(string)
- target expression(string)
- options...
Return:
Hash for validators(key is identification, value is validator)
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 173 174 |
# File 'lib/anodator/utils.rb', line 120 def self.load_validators_from_csv_file(file_path) first = true header = nil validators = { } CSV.open(file_path, "r") do |row| # skip header if first header = row first = false next end if validators.keys.include?(row[0]) raise ArgumentError.new("Duplicated validator number '#{row[0]}'!") end = (row, { :description => row[1] }) validator = nil case row[2] when "presence" validator = Validator::PresenceValidator when "blank" validator = Validator::BlankValidator when "date" validator = Validator::DateValidator when "format" validator = Validator::FormatValidator when "inclusion" validator = Validator::InclusionValidator unless [:in].nil? [:in] = [:in].split(",") end when "length" validator = Validator::LengthValidator when "numeric" validator = Validator::NumericValidator when "complex" [:validators] = row[3].split(",").map do |validator_number| validators[validator_number] end validators[row[0]] = Validator::ComplexValidator.new() else raise ArgumentError.new("Unknown validator type '#{row[2]}'!") end if validator validators[row[0]] = validator.new(row[3], ) end end return validators end |
.options_reader(row, options = { }, options_index_from = 4) ⇒ Object
options reader for option values for validator specs
When value is ‘true’ or ‘false’, convert true or false instance. The others set to string.
Arguments:
- row: validator line string
- options: default options
- options_index_from: index for option column be started on row(default: 4)
Return:
Hash
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/anodator/utils.rb', line 92 def self.(row, = { }, = 4) if row.size > row[..-1].each do |column| next if column.nil? key, value = column.split(":", 2) case value when "true", "false" [key.to_sym] = eval(value) else [key.to_sym] = value end end end return end |