class Livery::Presenter

Attributes

resource[R]

Public Class Methods

formatted_datetime(*names) click to toggle source
# File lib/livery/presenter.rb, line 14
def formatted_datetime(*names)
  names.each do |name|
    define_method name do
      value = @resource.public_send(name)
      I18n.l(value)
    end
  end
end
method_added(sym) click to toggle source
# File lib/livery/presenter.rb, line 23
def method_added(sym)
  raise "can't override resource on a subclass of Presenter" if sym == :resource
end
new(resource, options = {}) click to toggle source
# File lib/livery/presenter.rb, line 5
def initialize(resource, options = {})
  @resource = resource
  options.each do |k, v|
    raise ArgumentError, "cannot initialize `resource` variable" if k.to_s == "resource"
    instance_variable_set("@#{k}", v)
  end
end
presenter_association(*names, source: nil, namespace: nil) click to toggle source
# File lib/livery/presenter.rb, line 27
def presenter_association(*names, source: nil, namespace: nil)
  raise ArgumentError, '`source` must be a block' unless source.nil? || source.is_a?(Proc)

  names.each do |name|
    instance_variable_name = "@#{name}".to_sym

    define_method name do
      return instance_variable_get(instance_variable_name) if instance_variable_defined?(instance_variable_name)

      association_resource = source.nil? ? @resource.public_send(name) : @resource.instance_exec(&source)

      association_presenter = Presenter.to_presenter(association_resource, namespace: namespace)

      instance_variable_set(instance_variable_name, association_presenter)
      association_presenter
    end
  end
end
presenterize(klass, namespace: nil) click to toggle source
# File lib/livery/presenter.rb, line 46
def presenterize(klass, namespace: nil)
  presenter_klass_string = [namespace, "#{klass}Presenter"].compact.join('::')
  ActiveSupport::Inflector.constantize(presenter_klass_string)
end
resource(sym) click to toggle source
# File lib/livery/presenter.rb, line 51
def resource(sym)
  define_method sym do
    @resource
  end
end
to_presenter(object, namespace: nil) click to toggle source
# File lib/livery/presenter.rb, line 57
def to_presenter(object, namespace: nil)
  return object.map { |o| to_presenter_single(o, namespace: namespace) } if object.is_a?(Enumerable)
  to_presenter_single(object, namespace: namespace)
end

Private Class Methods

to_presenter_single(object, namespace: nil) click to toggle source
# File lib/livery/presenter.rb, line 64
def to_presenter_single(object, namespace: nil)
  return nil if object.nil?

  klass = object.class

  if klass < Presenter
    object
  else
    presenterize(klass, namespace: namespace).new(object)
  end
end

Public Instance Methods

t(*args) click to toggle source
# File lib/livery/presenter.rb, line 77
def t(*args)
  I18n.t(resolve_i18n_path(args.shift), *args)
end
t!(*args) click to toggle source
# File lib/livery/presenter.rb, line 81
def t!(*args)
  I18n.t!(resolve_i18n_path(args.shift), *args)
end
to_model() click to toggle source
# File lib/livery/presenter.rb, line 85
def to_model
  raise 'Presenter objects should not be used for forms. Call .resource on this Presenter'
end
to_param(*args) click to toggle source
# File lib/livery/presenter.rb, line 89
def to_param(*args)
  @resource.to_param(*args)
end

Private Instance Methods

resolve_i18n_path(key) click to toggle source
# File lib/livery/presenter.rb, line 95
def resolve_i18n_path(key)
  if key[0] != '.'
    key
  else
    ActiveSupport::Inflector.underscore(self.class.name).tr('/', '.') + key
  end
end