diff --git a/.rspec b/.rspec index 34c5164..58731eb 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,4 @@ ---format documentation +--format progress +--warnings --color --require spec_helper diff --git a/CHANGELOG.md b/CHANGELOG.md index 57cc6d1..23cae13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [0.1.6] - 2025-02-04 + +- Move `Validations::Message` to inside HeavyKeeper namespace to avoid +conflict + ## [0.1.5] - 2024-06-13 - Support latest version of xx-hash diff --git a/Gemfile.redis.4 b/Gemfile.redis.4 index 295373d..b4aa06f 100644 --- a/Gemfile.redis.4 +++ b/Gemfile.redis.4 @@ -11,7 +11,7 @@ gem 'rake', '~> 13.0' gem 'redis', '~> 4.0' group :test do - gem 'mock_redis' + gem 'mock_redis', '< 0.47.0' gem 'rspec' gem 'pry-byebug' end diff --git a/lib/heavy_keeper/top_k.rb b/lib/heavy_keeper/top_k.rb index 5d49f37..7f282da 100644 --- a/lib/heavy_keeper/top_k.rb +++ b/lib/heavy_keeper/top_k.rb @@ -3,7 +3,7 @@ require 'dry-schema' require 'securerandom' require 'xxhash' -require_relative '../validations/message' +require_relative 'validations/message' module HeavyKeeper class TopK # rubocop:disable Metrics/ClassLength @@ -217,7 +217,7 @@ def validate(options) result = Validator.call(options) if result.failure? - error = ::Validations::Message.new.build(result.errors.to_h).join('. ') + error = Validations::Message.new.build(result.errors.to_h).join('. ') raise HeavyKeeper::Error, error end diff --git a/lib/heavy_keeper/validations/message.rb b/lib/heavy_keeper/validations/message.rb new file mode 100644 index 0000000..7646d3d --- /dev/null +++ b/lib/heavy_keeper/validations/message.rb @@ -0,0 +1,37 @@ +module HeavyKeeper + module Validations + class Message + CLASSIFY_SEPARATOR = '_'.freeze + TITLEIZE_SEPARATOR = ' '.freeze + + # @errors [Hash | Array] output of dry-validation + # after validating params + # @parent [Nil | String] key name of a field that has `errors` + # after validating params + # Output: array of string that can be used to feed into + # Errors::InvalidParamsError + def build(errors, parent = nil) + case errors + when Hash + errors.flat_map do |key, value| + child = [parent, key].compact.join(' ') + build(value, child) + end + when Array + errors.flat_map do |error| + "#{titleize(parent.to_s)} #{build(error)}" + end + else + errors + end + end + + private + + def titleize(string) + # NOTE: this is not a robust implementation of titleize + string.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR) + end + end + end +end diff --git a/lib/heavy_keeper/version.rb b/lib/heavy_keeper/version.rb index b12ad4c..0211d0e 100644 --- a/lib/heavy_keeper/version.rb +++ b/lib/heavy_keeper/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module HeavyKeeper - VERSION = '0.1.5' + VERSION = '0.1.6' end diff --git a/lib/validations/message.rb b/lib/validations/message.rb deleted file mode 100644 index f580124..0000000 --- a/lib/validations/message.rb +++ /dev/null @@ -1,35 +0,0 @@ -module Validations - class Message - CLASSIFY_SEPARATOR = '_'.freeze - TITLEIZE_SEPARATOR = ' '.freeze - - # @errors [Hash | Array] output of dry-validation - # after validating params - # @parent [Nil | String] key name of a field that has `errors` - # after validating params - # Output: array of string that can be used to feed into - # Errors::InvalidParamsError - def build(errors, parent = nil) - case errors - when Hash - errors.flat_map do |key, value| - child = [parent, key].compact.join(' ') - build(value, child) - end - when Array - errors.flat_map do |error| - "#{titleize(parent.to_s)} #{build(error)}" - end - else - errors - end - end - - private - - def titleize(string) - # NOTE: this is not a robust implementation of titleize - string.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR) - end - end -end