Sha256: 28939d0840434e368307b592d8c9e6b727ad6c37667bbf37dbc1570d1e26a87d

Contents?: true

Size: 1.34 KB

Versions: 44

Compression:

Stored size: 1.34 KB

Contents

class Shortcode::Presenter

  class << self
    attr_writer :presenters

    def presenters
      @presenters ||= {}
    end

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

    def 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
  end

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

  def content
    @content
  end

  def attributes
    @attributes
  end

  private

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

end

Version data entries

44 entries across 44 versions & 2 rubygems

Version Path
virgo-0.3.17 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.16 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.15 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.14 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.13 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.12 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.11 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.10 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.9 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.8 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.7 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.6 vendor/gems/shortcode/lib/shortcode/presenter.rb
shortcode-1.1.1 lib/shortcode/presenter.rb
virgo-0.3.4 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.3 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.2 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3.1 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.3 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.2.9 vendor/gems/shortcode/lib/shortcode/presenter.rb
virgo-0.2.8 vendor/gems/shortcode/lib/shortcode/presenter.rb