Class: Naught::NullClassBuilder::Commands::NullSafeProxy Private

Inherits:
Naught::NullClassBuilder::Command show all
Defined in:
lib/naught/null_class_builder/commands/null_safe_proxy.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Enables null-safe proxy wrapping via the NullSafe() conversion function

When enabled, the generated null class gains a NullSafe() function that wraps any value in a proxy. The proxy intercepts all method calls and wraps return values, replacing nil with the null object.

Examples:

Enable null-safe proxy

NullObject = Naught.build do |config|
  config.null_safe_proxy
end

include NullObject::Conversions

user = nil
NullSafe(user).name.upcase      # => <null>

user = OpenStruct.new(name: nil)
NullSafe(user).name.upcase      # => <null>

user = OpenStruct.new(name: "Bob")
NullSafe(user).name.upcase      # => "BOB"

Instance Attribute Summary

Attributes inherited from Naught::NullClassBuilder::Command

#builder

Instance Method Summary collapse

Methods inherited from Naught::NullClassBuilder::Command

#initialize

Constructor Details

This class inherits a constructor from Naught::NullClassBuilder::Command

Instance Method Details

#callvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Install the NullSafe conversion function



33
34
35
36
37
38
39
40
# File 'lib/naught/null_class_builder/commands/null_safe_proxy.rb', line 33

def call
  null_equivs = builder.null_equivalents
  defer_class do |null_class|
    proxy_class = build_proxy_class(null_class, null_equivs)
    null_class.const_set(:NullSafeProxy, proxy_class)
    install_null_safe_conversion(null_class, proxy_class, null_equivs)
  end
end