Sha256: f1037754ca2c0e0a982db73089c0d912cbc4280a1b672e0e150444516edfd3bb

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 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)
    raise ArgumentError, "The presenter must define the class method #for" unless presenter.respond_to?(:for)
    raise ArgumentError, "The presenter must define an initialize method" unless presenter.private_instance_methods(false).include?(:initialize)
    %w(content attributes).each do |method|
      raise ArgumentError, "The presenter must define the method ##{method}" unless presenter.method_defined?(method.to_sym)
    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

  def content
    @content
  end

  def attributes
    @attributes
  end

  private

  attr_reader :configuration

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shortcode-1.2.1 lib/shortcode/presenter.rb
shortcode-1.2.0 lib/shortcode/presenter.rb