lib/meibo/converter.rb in meibo-0.25.3 vs lib/meibo/converter.rb in meibo-0.26.0

- old
+ new

@@ -11,10 +11,12 @@ required boolean date datetime enum + format + fullwidth mext_grade_code integer status user_ids year @@ -41,13 +43,13 @@ def build_converter_list(fields:, converters:, write_or_parser:) TYPES.filter_map do |converter_type| fields_to_be_converted = converters[converter_type] method_name = "build_#{converter_type}_field_#{write_or_parser}_converter" if fields_to_be_converted && respond_to?(method_name, true) - if converter_type == :enum - enum_definition = fields_to_be_converted.transform_keys { |field| fields.index(field) } - send(method_name, enum_definition) + if %i[enum format].include?(converter_type) + definition = fields_to_be_converted.transform_keys { |field| fields.index(field) } + send(method_name, definition) else indexes = fields_to_be_converted.map { |field| fields.index(field) } send(method_name, indexes) end end @@ -144,10 +146,31 @@ enum_definition = enum_definition.dup.freeze lambda do |field, field_info| return field unless field enum = enum_definition[field_info.index] - raise InvalidDataTypeError if enum&.none? { |pat| field.match?(pat) } + raise InvalidDataTypeError if enum&.none? { |pat| pat.is_a?(String) ? field == pat : field.match?(pat) } + + field + end + end + + def build_format_field_parser_converter(format_definition) + format_definition = format_definition.dup.freeze + lambda do |field, field_info| + return field unless field + + format = format_definition[field_info.index] + raise InvalidDataTypeError if format && !field.match?(format) + + field + end + end + + def build_fullwidth_field_parser_converter(fullwidth_field_indexes) + fullwidth_field_indexes = fullwidth_field_indexes.dup.freeze + lambda do |field, field_info| + raise InvalidDataTypeError if field && fullwidth_field_indexes.include?(field_info.index) && field.match?(/[\p{In_Halfwidth_and_Fullwidth_Forms}&&\p{Katakana}]/) field end end