Sha256: 90d5f10475baa9d9af25cd9c5cc5f409a737ebcaf27e2d33432ff020f5de4311

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Metamagic
  class Renderer
    DEFAULT_TAG_TYPES = {
      title:   TitleTag,
      og:      PropertyTag,
      twitter: PropertyTag
    }

    class << self
      def tag_types
        @tag_types ||= DEFAULT_TAG_TYPES.dup
      end

      def register_tag_type(prefix, klass)
        tag_types[prefix.to_sym] = klass
      end

      def tag_type_for_key(key)
        prefix = key.split(":").first
        tag_types[prefix.to_sym] || MetaTag
      end
    end

    attr_reader :context

    def initialize(context)
      @context = context
    end

    def tags
      @tags ||= []
    end

    def add(hash = {})
      transform_hash(hash).each do |k, v|
        klass = self.class.tag_type_for_key(k)
        next if tags.any? { |t| t.class == klass && t.key == k }
        tags << if klass.is_a?(Proc)
          CustomTag.new(self, k, v, klass)
        else
          klass.new(self, k, v)
        end
      end
    end

    def render
      tags.map(&:to_html).join("\n").html_safe
    end

    def method_missing(*args)
      context.send(*args)
    end

    private

    # Transforms a nested hash into meta property keys.
    def transform_hash(hash, path = "")
      hash.each_with_object({}) do |(k, v), ret|
        key = path + k.to_s

        if v.is_a?(Hash)
          ret.merge! transform_hash(v, "#{key}:")
        else
          ret[key] = v
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metamagic-3.0.0.beta1 lib/metamagic/renderer.rb