Module: AsJsonPresentable::ClassMethods

Defined in:
lib/as_json_presentable.rb

Instance Method Summary collapse

Instance Method Details

#define_json_presenter_class(klass) ⇒ Object

Allows the user to explicitly set the presenter class if the default naming convention isn’t desired.



30
31
32
# File 'lib/as_json_presentable.rb', line 30

def define_json_presenter_class(klass)
  @json_presenter_class = klass
end

#json_presenter_classObject

Returns the presenter class if it has been explicitly set or can be implied through naming conventions, or returns nil if no presenter class exists.

By default, the presenter class is inferred to be named after the model class, but with the suffix of “Presenter”.

e.g. If you have a model class called ‘Foo`, its presenter is assumed to be called `FooPresenter`

Alternatively, the presenter class can be explicitly defined with ‘::defined_json_presenter_class`



47
48
49
50
51
52
# File 'lib/as_json_presentable.rb', line 47

def json_presenter_class
  return @json_presenter_class if defined?(@json_presenter_class)

  klass_name = "#{self.name}Presenter"
  @json_presenter_class = Module.const_get(klass_name) if Module.const_defined?(klass_name)
end