Module: HasHelpers::CoreExt::Object

Includes:
Kernel
Defined in:
lib/has_helpers/core_ext/object.rb

Instance Method Summary collapse

Instance Method Details

#identityObject

Since there has been discussion about this for years without anything being done, I shall implement Object#identity myself...



53
54
55
# File 'lib/has_helpers/core_ext/object.rb', line 53

def identity
  self
end

#present(klass_or_proc = "::#{ self.class.name }::Presenter".safe_constantize, **options) ⇒ Object

A chainable method which wraps the receiver in an instance of another class. If the wrapper class is nil then the receiver object is returned. The argument may also be a proc which accepts one argument (the receiver) and returns the a class to use as the wrapper.

Examples

class MyClass; end class MyClass::Presenter ... end

instance = MyClass.new
instance.present  # => #<MyClass::Presenter>

The wrapper class is detected and used.

class MyClass; end

instance = MyClass.new
instance.present  # => instance

No wrapper class (with the default naming convention) was found, so the receiver is return.

class MyClass; end
class MegaPresenter; end

instance = MyClass.new
instance.present(MegaPresenter)  # => #<MegaPresenter>

Using an explicit wrapper class.

class MyClass; end
class MegaPresenter; end

instance = MyClass.new
presenter_proc = ->(obj) { MegaPresenter }
instance.present(presenter_proc)  # => #<MegaPresenter>

Passing a proc.



47
48
49
50
# File 'lib/has_helpers/core_ext/object.rb', line 47

def present(klass_or_proc = "::#{ self.class.name }::Presenter".safe_constantize, **options)
  klass = klass_or_proc.respond_to?(:call) ? klass_or_proc.call(self) : klass_or_proc
  (klass && klass.new(self, **options)) || self
end