Sha256: 86313c0997ab4086e12a6cb94448ba059fcc42b00d58f75cc8097e3ab1c10344

Contents?: true

Size: 834 Bytes

Versions: 2

Compression:

Stored size: 834 Bytes

Contents

require "sequel/enhancements"

module Sequel
  module Plugins
    module HashCleaner
      SURROUNDING_INVISIBLES = /\A[^[:graph:]]+|[^[:graph:]]+\z/.freeze

      def self.apply(model, *args)
        model.plugin(:input_transformer, :hash_cleaner){ |v| v.is_a?(Hash) ? clean(v) : v }
      end

      private

      def self.clean(value)
        if value.is_a?(Hash)
          value.each_with_object({}) do |(k, v), memo|
            cleaned = clean v
            memo[k] = cleaned unless exclude?(cleaned)
          end
        else
          Sequel::Enhancements.trim_invisible(value)
        end
      end

      def self.clean_value(value)
        value.is_a?(String) ? value.trim_invisible : value
      end

      def self.exclude?(value)
        value.nil? || (value.is_a?(String) && value.empty?)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sequel-enhancements-0.3.0 lib/sequel/plugins/hash_cleaner.rb
sequel-enhancements-0.2.0 lib/sequel/plugins/hash_cleaner.rb