Class: Dux::NullObject

Inherits:
Object
  • Object
show all
Includes:
InspectID
Defined in:
lib/dux/null_object.rb

Overview

Null objects that can be used for smarter argument building.

Examples:

Usage

module Foo
  class Bar
    NULL = Dux.null 'Foo::Bar::NULL'

    # Making it private is not required,
    # but recommended if it is something
    # that should only be available
    # internally.
    private_constant :NULL

    def initialize(some_option: NULL)
      if some_option == NULL
        # Do some default logic
      else
        @some_option = some_option
      end
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, purpose: 'a null object') ⇒ NullObject

Returns a new instance of NullObject.

Parameters:

  • name (String) (defaults to: nil)
  • purpose (String) (defaults to: 'a null object')


45
46
47
48
49
50
# File 'lib/dux/null_object.rb', line 45

def initialize(name = nil, purpose: 'a null object')
  @name     = name || default_name
  @purpose  = purpose

  freeze
end

Instance Attribute Details

#nameString (readonly)

The name of this null object, for self-documenting / introspection.

In practice, this should be the object path, e.g. Foo::Bar::NULL

Returns:

  • (String)


35
36
37
# File 'lib/dux/null_object.rb', line 35

def name
  @name
end

#purposeString (readonly)

A purpose or description of this object, for self-documenting / introspection.

Returns:

  • (String)


41
42
43
# File 'lib/dux/null_object.rb', line 41

def purpose
  @purpose
end

Instance Method Details

#default_nameString (private)

Generates a default name for this object.

Returns:

  • (String)


57
58
59
# File 'lib/dux/null_object.rb', line 57

def default_name
  "Dux::NullObject(#{inspect_id(self)})"
end

#inspect_id(object = self) ⇒ String Originally defined in module InspectID

Note:

This is currently limited to the implementation used in MRI. Rubinius and JRuby have their own implementations that are not currently served by this.

Calculates the id shown in the default version of #inspect methods.

Parameters:

  • object (Object) (defaults to: self)

Returns:

  • (String)