Sha256: 3a3ad7f062ab50e0169876aea256de119784a03c0cf83dd3dc4ebf6825014db5

Contents?: true

Size: 1.88 KB

Versions: 52

Compression:

Stored size: 1.88 KB

Contents

# frozen-string-literal: true

module Sequel
  module Plugins
    # StringStripper is a plugin that strips all input strings
    # when assigning to the model's values. Example:
    #
    #   album = Album.new(name: ' A ')
    #   album.name # => 'A'
    #
    # SQL::Blob instances and all non-strings are not modified by
    # this plugin.  Additionally, strings passed to a blob column
    # setter are also not modified.  You can explicitly set
    # other columns to skip the stripping:
    #
    #   Album.skip_string_stripping :foo
    #   Album.new(foo: ' A ').foo # => ' A '
    # 
    # Usage:
    #
    #   # Make all model subclass instances strip strings (called before loading subclasses)
    #   Sequel::Model.plugin :string_stripper
    #
    #   # Make the Album class strip strings
    #   Album.plugin :string_stripper
    module StringStripper
      def self.apply(model)
        model.plugin(:input_transformer, :string_stripper){|v| (v.is_a?(String) && !v.is_a?(SQL::Blob)) ? v.strip : v}
      end
      def self.configure(model)
        model.instance_exec{set_skipped_string_stripping_columns if @dataset}
      end

      module ClassMethods
        Plugins.after_set_dataset(self, :set_skipped_string_stripping_columns)

        # Skip stripping for the given columns.
        def skip_string_stripping(*columns)
          skip_input_transformer(:string_stripper, *columns)
        end

        # Return true if the column should not have values stripped.
        def skip_string_stripping?(column)
          skip_input_transformer?(:string_stripper, column)
        end

        private

        # Automatically skip stripping of blob columns
        def set_skipped_string_stripping_columns
          if @db_schema
            blob_columns = @db_schema.map{|k,v| k if v[:type] == :blob}.compact
            skip_string_stripping(*blob_columns)
          end
        end
      end
    end
  end
end

Version data entries

52 entries across 47 versions & 2 rubygems

Version Path
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/sequel-5.32.0/lib/sequel/plugins/string_stripper.rb
sequel-5.34.0 lib/sequel/plugins/string_stripper.rb
sequel-5.33.0 lib/sequel/plugins/string_stripper.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/sequel-5.26.0/lib/sequel/plugins/string_stripper.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/sequel-5.32.0/lib/sequel/plugins/string_stripper.rb
sequel-5.32.0 lib/sequel/plugins/string_stripper.rb
sequel-5.31.0 lib/sequel/plugins/string_stripper.rb
sequel-5.30.0 lib/sequel/plugins/string_stripper.rb
tdiary-5.1.1 vendor/bundle/ruby/2.7.0/gems/sequel-5.29.0/lib/sequel/plugins/string_stripper.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/sequel-5.26.0/lib/sequel/plugins/string_stripper.rb
sequel-5.29.0 lib/sequel/plugins/string_stripper.rb
sequel-5.28.0 lib/sequel/plugins/string_stripper.rb
sequel-5.27.0 lib/sequel/plugins/string_stripper.rb
tdiary-5.1.0 vendor/bundle/gems/sequel-5.26.0/lib/sequel/plugins/string_stripper.rb
sequel-5.26.0 lib/sequel/plugins/string_stripper.rb
sequel-5.25.0 lib/sequel/plugins/string_stripper.rb
sequel-5.24.0 lib/sequel/plugins/string_stripper.rb
sequel-5.23.0 lib/sequel/plugins/string_stripper.rb
sequel-5.22.0 lib/sequel/plugins/string_stripper.rb
sequel-5.21.0 lib/sequel/plugins/string_stripper.rb