Sha256: b6c026a477967dbe2783bf18376b06d0c353c77a861a07e1c63ce5c8178c9b47

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

module Parliament
  module Grom
    module Decorator
      module Helpers
        # Namespace for utility methods
        module Utils
          class << self
            # Checks that a collection responds to first and that the first element is of the given class
            #
            # @param [Enumerable] enumerable object which responds to #first
            # @param [Class] klass the class we expect the first element to be
            #
            # @example When the first element is of the expected class
            #   Parliament::Grom::Decorator::Helpers::Utils.type_safe_first(['hello'], String) #=> 'hello'
            #
            # @example When the first element is not of the expected class
            #   Parliament::Grom::Decorator::Helpers::Utils.type_safe_first(['hello'], Fixnum) #=> nil
            #
            # @example When the enumerable does not respond to first
            #   Parliament::Grom::Decorator::Helpers::Utils.type_safe_first('hello', String) #=> nil
            #
            # @return [String] formatted date range
            def type_safe_first(enumerable, klass)
              return nil unless enumerable.respond_to?(:first)

              first_element = enumerable.first

              return nil unless first_element.is_a?(klass)

              first_element
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
parliament-grom-decorators-1.0.6 lib/parliament/grom/decorator/helpers/utils.rb
parliament-grom-decorators-1.0.5 lib/parliament/grom/decorator/helpers/utils.rb
parliament-grom-decorators-1.0.4 lib/parliament/grom/decorator/helpers/utils.rb
parliament-grom-decorators-1.0.3 lib/parliament/grom/decorator/helpers/utils.rb
parliament-grom-decorators-1.0.2 lib/parliament/grom/decorator/helpers/utils.rb
parliament-grom-decorators-1.0.0 lib/parliament/grom/decorator/helpers/utils.rb
parliament-grom-decorators-1.0.0.pre.pre lib/parliament/grom/decorator/helpers/utils.rb