Sha256: 6f1c67a808f498730d0a509595c1562666ef0ecc9a1042044a36384ea1b0b1b9

Contents?: true

Size: 678 Bytes

Versions: 1

Compression:

Stored size: 678 Bytes

Contents

# NOTE: https://blog.daveallie.com/clean-monkey-patching/

require 'active_support/core_ext/array'

module Ensurance
  module ArrayEnsure
    def self.prepended(base)
      base.singleton_class.prepend(ClassMethods)
    end

    module ClassMethods
      def ensure(thing)
        case thing.class.name
        when 'NilClass'
          nil
        when 'String'
          thing.split(',')
        else
          Array(thing)
        end
      end

      def ensure!(thing)
        result = self.ensure(thing)
        raise ArgumentError, "Cannot Array.ensure(#{thing || 'nil'})" unless result
        result
      end
    end
  end
end

::Array.prepend(Ensurance::ArrayEnsure)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ensurance-0.1.12 lib/ensurance/array_ensure.rb