Class: Metamagic::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/metamagic/renderer.rb

Constant Summary collapse

DEFAULT_TAG_TYPES =
{
  title:       TitleTag,
  description: MetaTag,
  keywords:    MetaTag,
  property:    PropertyTag,
  og:          PropertyTag,
  twitter:     PropertyTag
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Renderer

Returns a new instance of Renderer.



29
30
31
# File 'lib/metamagic/renderer.rb', line 29

def initialize(context)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



59
60
61
# File 'lib/metamagic/renderer.rb', line 59

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



27
28
29
# File 'lib/metamagic/renderer.rb', line 27

def context
  @context
end

Class Method Details

.register_tag_type(prefix, klass) ⇒ Object



17
18
19
# File 'lib/metamagic/renderer.rb', line 17

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

.tag_type_for_key(key) ⇒ Object



21
22
23
24
# File 'lib/metamagic/renderer.rb', line 21

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

.tag_typesObject



13
14
15
# File 'lib/metamagic/renderer.rb', line 13

def tag_types
  @tag_types ||= DEFAULT_TAG_TYPES.dup
end

Instance Method Details

#add(hash = {}) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/metamagic/renderer.rb', line 37

def add(hash = {})
  raise ArgumentError, "Defining meta properties via arrays has been removed in Metamagic v3.0 and replaced by some pretty helpers. Please see the readme at https://github.com/lassebunk/metamagic for more info." if hash.is_a?(Array)

  transform_hash(hash).each do |k, v|
    klass = self.class.tag_type_for_key(k)
    tag = if klass.is_a?(Proc)
      CustomTag.new(self, k, v, klass)
    else
      klass.new(self, k, v)
    end
    tags << tag unless tags.include?(tag)
  end
end

#has_tag_type?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/metamagic/renderer.rb', line 51

def has_tag_type?(prefix)
  self.class.tag_types.has_key?(prefix)
end

#renderObject



55
56
57
# File 'lib/metamagic/renderer.rb', line 55

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

#tagsObject



33
34
35
# File 'lib/metamagic/renderer.rb', line 33

def tags
  @tags ||= []
end