Module: Tins::Full

Defined in:
lib/tins/xt/full.rb

Overview

Provides methods for checking if objects are “full” (non-blank) and safely processing them in conditional contexts.

This module adds the ‘full?` and `all_full?` methods to all objects, enabling clean, readable patterns for validation and conditional processing.

Examples:

Basic usage

"hello".full?        # => "hello"
"".full?             # => nil

Method dispatch with block

user.full?(:name) { |name| "Hello #{name}" }  # Returns "Hello John" if name is full

Safe assignment and processing

if name = user.full?(:name)
  puts "Hello #{name}"
end

Instance Method Summary collapse

Instance Method Details

#all_full?Object?

Checks if all elements in a collection are “full” (not blank). If the object responds to all? and all elements pass the full? test, then the block is executed with the collection itself or the collection is returned.

Examples:

Basic usage

[1,2,3].all_full?    # => [1,2,3]
[1,nil,3].all_full?  # => nil

With block execution

[1,2,3].all_full? { |array| array.sum }

Returns:

  • (Object, nil)

    The collection if all elements are full, otherwise nil



74
75
76
77
78
# File 'lib/tins/xt/full.rb', line 74

def all_full?
  if respond_to?(:all?) && all?(&:full?)
    block_given? ? yield(self) : self
  end
end

#full?(dispatch = nil, *args) {|Object| ... } ⇒ Object?

Checks if the object is not blank, returning the object itself if it’s full, or nil if it’s blank. If a method name is provided as dispatch, that method is called on the object and the result is checked for being full.

dispatched result not nil

Examples:

Basic usage

"hello".full?        # => "hello"
"".full?             # => nil

Method dispatch

user.full?(:name)    # Returns user.name if not blank, nil otherwise

Method dispatch with arguments

user.full?(:method_with_args, arg1, arg2)

With block execution

user.full?(:name) { |name| "Hello #{name}" }

Parameters:

  • dispatch (Symbol, nil) (defaults to: nil)

    The method to call on the object (optional)

  • args (Array)

    Arguments to pass to the dispatched method (optional)

Yields:

  • (Object)

    Optional block to execute with the result if result or

Returns:

  • (Object, nil)

    The object itself if not blank, or the result of dispatching dispatch if provided and valid, or nil if the object is blank



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tins/xt/full.rb', line 46

def full?(dispatch = nil, *args)
  if blank?
    obj = nil
  elsif dispatch
    obj = __send__(dispatch, *args)
    obj = nil if obj.blank?
  else
    obj = self
  end
  if block_given? and obj
    yield obj
  else
    obj
  end
end