strip_control_chars 1.1 → 2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +15 -5
- data/lib/strip_control_chars.rb +2 -35
- data/lib/strip_control_chars/ar_extend.rb +37 -0
- data/lib/strip_control_chars/railtie.rb +15 -0
- data/test/test_helper.rb +2 -1
- metadata +14 -12
- data/init.rb +0 -2
- data/rails/init.rb +0 -2
    
        data/README.rdoc
    CHANGED
    
    | @@ -23,14 +23,24 @@ your database. | |
| 23 23 |  | 
| 24 24 | 
             
            === Installation
         | 
| 25 25 |  | 
| 26 | 
            -
             | 
| 26 | 
            +
            1. On rails 2.3
         | 
| 27 27 |  | 
| 28 | 
            -
              gem install strip_control_chars
         | 
| 29 | 
            -
              add  | 
| 28 | 
            +
              * gem install strip_control_chars
         | 
| 29 | 
            +
              * add the following to your environment.rb
         | 
| 30 30 |  | 
| 31 | 
            -
             | 
| 31 | 
            +
                config.gem 'strip_control_chars', version => "1.0"
         | 
| 32 32 |  | 
| 33 | 
            -
             | 
| 33 | 
            +
            2. On rails 3
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              * gem install strip_control_chars
         | 
| 36 | 
            +
              * add the following to your Gemfile
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                gem 'strip_control_chars', ">=2.0"
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            === Compatibility
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            On rails 2.x you must use version 1.x of this gem. On rails 3.x you must use
         | 
| 43 | 
            +
            version 2.x of this gem.
         | 
| 34 44 |  | 
| 35 45 | 
             
            === Caveats
         | 
| 36 46 |  | 
    
        data/lib/strip_control_chars.rb
    CHANGED
    
    | @@ -1,35 +1,2 @@ | |
| 1 | 
            -
             | 
| 2 | 
            -
             | 
| 3 | 
            -
              def strip_control_chars!(options = nil)
         | 
| 4 | 
            -
                before_validation do |record|
         | 
| 5 | 
            -
                  attributes = StripControlChars.narrow(record.attributes, options)
         | 
| 6 | 
            -
                  attributes.each do |attr, value|
         | 
| 7 | 
            -
                    if value.respond_to?(:tr)
         | 
| 8 | 
            -
                      value = value.tr("\x00\x01\x02\x03\x04\x05\x06\x07\x08","         ")
         | 
| 9 | 
            -
                      value = value.tr("\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14","         ")
         | 
| 10 | 
            -
                      value = value.tr("\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D","         ")
         | 
| 11 | 
            -
                      value = value.tr("\x1E\x1F","  ")
         | 
| 12 | 
            -
                      record[attr] = value
         | 
| 13 | 
            -
                    end
         | 
| 14 | 
            -
                  end
         | 
| 15 | 
            -
                end
         | 
| 16 | 
            -
              end
         | 
| 17 | 
            -
             | 
| 18 | 
            -
              # Necessary because Rails has removed the narrowing of attributes using :only
         | 
| 19 | 
            -
              # and :except on Base#attributes
         | 
| 20 | 
            -
              def self.narrow(attributes, options)
         | 
| 21 | 
            -
                if options.nil?
         | 
| 22 | 
            -
                  attributes
         | 
| 23 | 
            -
                else
         | 
| 24 | 
            -
                  if except = options[:except]
         | 
| 25 | 
            -
                    except = Array(except).collect { |attribute| attribute.to_s }
         | 
| 26 | 
            -
                    attributes.except(*except)
         | 
| 27 | 
            -
                  elsif only = options[:only]
         | 
| 28 | 
            -
                    only = Array(only).collect { |attribute| attribute.to_s }
         | 
| 29 | 
            -
                    attributes.slice(*only)
         | 
| 30 | 
            -
                  else
         | 
| 31 | 
            -
                    raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
         | 
| 32 | 
            -
                  end
         | 
| 33 | 
            -
                end
         | 
| 34 | 
            -
              end
         | 
| 35 | 
            -
            end
         | 
| 1 | 
            +
            require 'strip_control_chars/ar_extend'
         | 
| 2 | 
            +
            require 'strip_control_chars/railtie'
         | 
| @@ -0,0 +1,37 @@ | |
| 1 | 
            +
            module StripControlChars
         | 
| 2 | 
            +
              module ArExtend
         | 
| 3 | 
            +
                # Strips ASCII control chars from attributes before they get saved
         | 
| 4 | 
            +
                def strip_control_chars!(options = nil)
         | 
| 5 | 
            +
                  before_validation do |record|
         | 
| 6 | 
            +
                    attributes = StripControlChars::ArExtend.narrow(record.attributes, options)
         | 
| 7 | 
            +
                    attributes.each do |attr, value|
         | 
| 8 | 
            +
                      if value.respond_to?(:tr)
         | 
| 9 | 
            +
                        value = value.tr("\x00\x01\x02\x03\x04\x05\x06\x07\x08","         ")
         | 
| 10 | 
            +
                        value = value.tr("\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14","         ")
         | 
| 11 | 
            +
                        value = value.tr("\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D","         ")
         | 
| 12 | 
            +
                        value = value.tr("\x1E\x1F","  ")
         | 
| 13 | 
            +
                        record[attr] = value
         | 
| 14 | 
            +
                      end
         | 
| 15 | 
            +
                    end
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                # Necessary because Rails has removed the narrowing of attributes using :only
         | 
| 20 | 
            +
                # and :except on Base#attributes
         | 
| 21 | 
            +
                def self.narrow(attributes, options)
         | 
| 22 | 
            +
                  if options.nil?
         | 
| 23 | 
            +
                    attributes
         | 
| 24 | 
            +
                  else
         | 
| 25 | 
            +
                    if except = options[:except]
         | 
| 26 | 
            +
                      except = Array(except).collect { |attribute| attribute.to_s }
         | 
| 27 | 
            +
                      attributes.except(*except)
         | 
| 28 | 
            +
                    elsif only = options[:only]
         | 
| 29 | 
            +
                      only = Array(only).collect { |attribute| attribute.to_s }
         | 
| 30 | 
            +
                      attributes.slice(*only)
         | 
| 31 | 
            +
                    else
         | 
| 32 | 
            +
                      raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
         | 
| 33 | 
            +
                    end
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
            end
         | 
| @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            require 'strip_control_chars'
         | 
| 2 | 
            +
            require 'rails'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module StripControlChars
         | 
| 5 | 
            +
              class Railtie < Rails::Railtie
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                initializer "strip_control_chars.active_record" do |app|
         | 
| 8 | 
            +
                  if defined?(::ActiveRecord)
         | 
| 9 | 
            +
                    ::ActiveRecord::Base.extend(StripControlChars::ArExtend)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
    
        data/test/test_helper.rb
    CHANGED
    
    | @@ -5,10 +5,11 @@ require 'active_record' | |
| 5 5 | 
             
            PLUGIN_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
         | 
| 6 6 |  | 
| 7 7 | 
             
            $LOAD_PATH.unshift "#{PLUGIN_ROOT}/lib"
         | 
| 8 | 
            -
            require  | 
| 8 | 
            +
            require 'strip_control_chars'
         | 
| 9 9 |  | 
| 10 10 | 
             
            class ActiveRecord::Base
         | 
| 11 11 | 
             
              alias_method :save, :valid?
         | 
| 12 | 
            +
              extend StripControlChars::ArExtend
         | 
| 12 13 | 
             
              def self.columns()
         | 
| 13 14 | 
             
                @columns ||= []
         | 
| 14 15 | 
             
              end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,12 +1,12 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: strip_control_chars
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 4 | 
            +
              hash: 3
         | 
| 5 5 | 
             
              prerelease: false
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 | 
            -
              -  | 
| 8 | 
            -
              -  | 
| 9 | 
            -
              version: " | 
| 7 | 
            +
              - 2
         | 
| 8 | 
            +
              - 0
         | 
| 9 | 
            +
              version: "2.0"
         | 
| 10 10 | 
             
            platform: ruby
         | 
| 11 11 | 
             
            authors: 
         | 
| 12 12 | 
             
            - James Healy
         | 
| @@ -23,13 +23,15 @@ dependencies: | |
| 23 23 | 
             
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 24 24 | 
             
                none: false
         | 
| 25 25 | 
             
                requirements: 
         | 
| 26 | 
            -
                - -  | 
| 26 | 
            +
                - - ">="
         | 
| 27 27 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 28 | 
            -
                    hash:  | 
| 28 | 
            +
                    hash: 7712042
         | 
| 29 29 | 
             
                    segments: 
         | 
| 30 | 
            -
                    -  | 
| 31 | 
            -
                    -  | 
| 32 | 
            -
                     | 
| 30 | 
            +
                    - 3
         | 
| 31 | 
            +
                    - 0
         | 
| 32 | 
            +
                    - 0
         | 
| 33 | 
            +
                    - rc
         | 
| 34 | 
            +
                    version: 3.0.0.rc
         | 
| 33 35 | 
             
              type: :runtime
         | 
| 34 36 | 
             
              version_requirements: *id001
         | 
| 35 37 | 
             
            description: a small ActiveRecord plugin that removes ASCII control chars from attributes
         | 
| @@ -41,16 +43,16 @@ extensions: [] | |
| 41 43 | 
             
            extra_rdoc_files: []
         | 
| 42 44 |  | 
| 43 45 | 
             
            files: 
         | 
| 44 | 
            -
            - init.rb
         | 
| 45 | 
            -
            - rails/init.rb
         | 
| 46 46 | 
             
            - lib/strip_control_chars.rb
         | 
| 47 | 
            +
            - lib/strip_control_chars/ar_extend.rb
         | 
| 48 | 
            +
            - lib/strip_control_chars/railtie.rb
         | 
| 47 49 | 
             
            - Rakefile
         | 
| 48 50 | 
             
            - MIT-LICENSE
         | 
| 49 51 | 
             
            - README.rdoc
         | 
| 50 52 | 
             
            - test/strip_control_chars_test.rb
         | 
| 51 53 | 
             
            - test/test_helper.rb
         | 
| 52 54 | 
             
            has_rdoc: true
         | 
| 53 | 
            -
            homepage: http://github.com/yob/strip_control_chars | 
| 55 | 
            +
            homepage: http://github.com/yob/strip_control_chars
         | 
| 54 56 | 
             
            licenses: []
         | 
| 55 57 |  | 
| 56 58 | 
             
            post_install_message: 
         | 
    
        data/init.rb
    DELETED
    
    
    
        data/rails/init.rb
    DELETED