Sha256: f9b2ddc0aca92a210e30448ab4a0389d8a72bbaeb473bd68cb0c5c9f65f0d3ce

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require "normalize_line_endings/version"

require "active_support"

module NormalizeLineEndings
  extend ActiveSupport::Concern

  module ClassMethods
    def normalize_line_endings(*opts)
      @normalize_line_endings_options = opts.first.is_a?(Hash) ? opts.first : { only: opts } unless opts.empty?

      class << self; attr_reader :normalize_line_endings_options; end

      before_validation { |record| record.normalize_line_endings }
    end
    alias_method :normalize_line_endings_for, :normalize_line_endings
  end

  def normalize_line_endings
    attrs_for_normalized_line_endings.each do |attr, value|
      send("#{attr}=", value.gsub(/\r\n/, "\n")) if value.respond_to?(:gsub)
    end
  end

  def attrs_for_normalized_line_endings
    options = self.class.normalize_line_endings_options

    return attributes if options.blank?

    if options[:except]
      except = Array(options[:except]).flatten.map(&:to_s)

      attributes.stringify_keys.except(*except)
    elsif options[:only]
      only = Array(options[:only]).flatten.map(&:to_s)

      attributes.stringify_keys.slice(*only)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
normalize_line_endings-0.0.1 lib/normalize_line_endings.rb