Class: Magic::Presenter::Base

Inherits:
Decorator::Base
  • Object
show all
Includes:
GlobalID, Helpers
Defined in:
lib/magic/presenter/base.rb

Overview

Magic Presenter

Any presenter should inherit ‘Magic::Presenter::Base`. It further inherits from `SimpleDelegator` and behaves like that.

Presenter class inference

Presenters provide automatic class inference for any model based on its class name powered by Magic Lookup. For example, ‘MyModel.new` looks for `MyPresenter` first. If not found, it looks for presenters of its ancestor classes, up to `ObjectPresenter`.

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Magic::Presenter::Helpers

Class Method Details

.descendantsObject



45
46
47
48
49
# File 'lib/magic/presenter/base.rb', line 45

def descendants
	Magic.eager_load :presenters

	super
end

.model_classObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/magic/presenter/base.rb', line 32

def model_class
	Presentable.classes
			.select { Presenter.for(_1) == self }
			.sole
rescue Enumerable::SoleItemExpectedError => error
	raise Lookup::Error, "#{error.message
			.sub('items', 'model classes')
			.sub('item',  'model class')
	} for #{self}"
end

.name_for(object_class) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/magic/presenter/base.rb', line 24

def name_for object_class
	object_class
			.to_s
			.delete_suffix('Model')
			.delete_suffix('Record')
			.then { "#{_1}Presenter" }
end