Sha256: 8a7d6c6a1c8df8e6b0225f0e90c3d30a986243c4a797f2f6f28dba9e0fbcd07a

Contents?: true

Size: 977 Bytes

Versions: 1

Compression:

Stored size: 977 Bytes

Contents

class Object
  # Credit goes to the active_support contributors
  # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
  #
  # Invokes the public method whose name goes as first argument just like
  # +public_send+ does, except that if the receiver does not respond to it the
  # call returns +nil+ rather than raising an exception.
  def try(*args, &b)
    if args.empty? && block_given?
      yield self
    else
      public_send(*args, &b) if respond_to?(args.first)
    end
  end

  # Chained try allows writing
  #
  #    str.ctry(:mb_chars, :downcase, :dasherize)
  #
  # instead of
  #
  #    str.try(:mb_chars).try(:downcase).try(:dasherize)
  #
  # Only works for methods that don't take any arguments.
  def ctry(*args)
    first, *rest = args
    if rest.any?
      try(first).ctry(*rest)
    else
      try(first)
    end
  end
end

class NilClass
  def try(*args)
    nil
  end

  def ctry(*args)
    nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inactive_support-1.1.0 lib/inactive_support/object/try.rb