Sha256: 4f2c889eabefb03502bf7256884d13567d591455751f1336a1906040006e3d2a

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

class Shortcode::Presenter

  def self.register(configuration, presenter)
    validate presenter
    [*presenter.for].each { |k| configuration.presenters[k.to_sym] = presenter }
  end

  def self.validate(presenter)
    unless presenter.respond_to?(:for)
      raise ArgumentError, "The presenter must define the class method #for"
    end
    unless presenter.private_instance_methods(false).include?(:initialize)
      raise ArgumentError, "The presenter must define an initialize method"
    end
    %w[content attributes].each do |method|
      unless presenter.method_defined?(method.to_sym)
        raise ArgumentError, "The presenter must define the method ##{method}"
      end
    end
  end

  def initialize(name, configuration, attributes, content, additional_attributes)
    @configuration = configuration
    @attributes = attributes
    @content = content
    @additional_attributes = additional_attributes
    initialize_custom_presenter(name)
  end

  attr_reader :content

  attr_reader :attributes

  private

  attr_reader :configuration

  def initialize_custom_presenter(name)
    return unless configuration.presenters.key?(name.to_sym)
    presenter   = configuration.presenters[name.to_sym].new(@attributes, @content, @additional_attributes)
    @attributes = presenter.attributes
    @content    = presenter.content
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shortcode-2.0.0 lib/shortcode/presenter.rb
shortcode-2.0.0.pre lib/shortcode/presenter.rb