module Chic::Presents::ClassMethods

Public Instance Methods

present(object = nil, context = nil) { |presenter, object| ... } click to toggle source
# File lib/chic/presents.rb, line 12
def present(object = nil, context = nil, &block)
  presenter = new(object, context || _caller(&block))
  yield(presenter, object) if block_given?
end
present_each(objects, context = nil) { |presenter, object| ... } click to toggle source
# File lib/chic/presents.rb, line 17
def present_each(objects, context = nil, &block)
  context ||= _caller(&block)
  objects.map do |object|
    presenter = new(object, context)
    yield(presenter, object) if block_given?
  end
end

Private Instance Methods

_caller(&block) click to toggle source
# File lib/chic/presents.rb, line 27
def _caller(&block)
  block.binding.eval('self') if block_given?
end
_define_presents_method(attribute) click to toggle source
# File lib/chic/presents.rb, line 42
def _define_presents_method(attribute)
  define_method attribute do
    memoized = "@#{attribute}"
    return instance_variable_get(memoized) if instance_variable_defined?(memoized)

    result = present(object.public_send(attribute))
    instance_variable_set(memoized, result)
  end
end
_define_presents_with_options_method(attribute, options) click to toggle source
# File lib/chic/presents.rb, line 52
def _define_presents_with_options_method(attribute, options)
  define_method attribute do
    memoized = "@#{attribute}"
    return instance_variable_get(memoized) if instance_variable_defined?(memoized)

    with = _presents_options_presenter_class(options)
    value = _presents_options_value(attribute, options)
    presenter = present(value, with: with)
    instance_variable_set(memoized, presenter)
  end
end
_raise_presents_options_not_valid(message, attribute) click to toggle source

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity

# File lib/chic/presents.rb, line 83
def _raise_presents_options_not_valid(message, attribute)
  raise PresentsOptionsNotValid,
        "#{name} `presents` :#{attribute}: #{message}"
end
_validate_presents_options!(attribute, options) click to toggle source

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity

# File lib/chic/presents.rb, line 67
def _validate_presents_options!(attribute, options)
  _raise_presents_options_not_valid 'options must be a hash or a class', attribute \
    unless options.is_a?(Hash) || options.is_a?(Class)

  return unless options.is_a?(Hash)

  _raise_presents_options_not_valid '`with` must be a class', attribute \
    if options.key?(:with) && !options[:with].is_a?(Class)

  _raise_presents_options_not_valid '`value` must be a symbol or a lambda', attribute \
    if options.key?(:value) && !options[:value].is_a?(Symbol) && !options[:value].is_a?(Proc)
end
presents(*attributes, **with_options) click to toggle source
# File lib/chic/presents.rb, line 31
def presents(*attributes, **with_options)
  attributes.each do |attribute|
    _define_presents_method(attribute)
  end

  with_options.each do |attribute, options|
    _validate_presents_options!(attribute, options)
    _define_presents_with_options_method(attribute, options)
  end
end