Problem Statement

The developer seems finding an alternative for the .present? method in the Hanami framework.

In Ruby on Rails, the .present? method is commonly used to check if an object is not nil and not empty. However, when working with Hanami, which is a different Ruby web framework, developers may not find a direct equivalent method.

The question seeks to address this gap by exploring whether Hanami offers a similar method or if there are alternative approaches to achieve the same functionality. Essentially, the problem statement is about identifying a Hanami-compatible way to determine if an object contains meaningful data, similar to how .present? works in Rails.

Initial Attempt

The initial attempt may involve trying to use the .present? method directly within a Hanami application. Developers may start by assuming that Hanami provides the same method as Rails, given that Hanami is inspired by Rails and aims to offer similar functionality.

Desired Output

The developer seeks a method or approach in Hanami that behaves similarly to Rails’ .present? method. In Rails, .present? returns true if the object is not nil and not empty (e.g., an empty string or whitespace).

Solutions

Using Hanami::Utils::Blank:

require 'hanami/utils/blank'

Hanami::Utils::Blank.blank?(nil)     #=> true
Hanami::Utils::Blank.blank?(' ')     #=> true
Hanami::Utils::Blank.blank?('Artur') #=> false

However, it’s important to note that Hanami::Utils::Blank.blank? is considered part of a private API, and there’s a concern that it may be removed or changed in the future.

Extending Object with custom methods :

class Object
  def blank?
    respond_to?(:empty?) ? !!empty? : !self
  end

  def present?
    !blank?
  end
end

This approach provides custom methods .blank? and .present?, allowing similar functionality to Rails.

Using ActiveSupport without Ruby on Rails

gem 'activesupport', '~> 6.1'
require 'active_support/core_ext/object/blank'
nil.blank?        #=> true
' '.blank?        #=> true
'Artur'.blank?    #=> false

ActiveSupport is a separate gem from Ruby on Rails and can be used independently. It provides methods like .blank? and .present?.

Using pure Ruby methods:

nil.nil?          #=> true
''.empty?         #=> true
'Artur'.empty?    #=> false

This approach relies on pure Ruby methods such as .nil? and .empty?, which can provide similar functionality depending on the use case.

Conclusion

While Hanami may not provide a direct equivalent to Rails’ .present? method, developers have several options to achieve similar functionality. They can use Hanami::Utils::Blank, extend Object with custom methods, leverage ActiveSupport without Ruby on Rails, or rely on pure Ruby methods.

Each approach has its pros and cons, and the choice depends on factors such as the specific use case, project requirements, and long-term maintenance considerations. Developers should carefully evaluate these options and select the one that aligns best with their project goals and development philosophy.

Ultimately, by understanding these alternatives and their implications, developers can effectively handle scenarios where they need to check for non-nil and non-empty values in Hanami applications, ensuring robustness and clarity in their codebase.

Support On Demand!

                                         
Ruby on Rails