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,
  rel:         LinkTag,
  og:          OpenGraphTag,
  twitter:     TwitterTag
}
DEFAULT_SEPARATOR =
" - "

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Renderer

Returns a new instance of Renderer.


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

def initialize(context)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object


97
98
99
# File 'lib/metamagic/renderer.rb', line 97

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.


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

def context
  @context
end

#separatorObject


87
88
89
# File 'lib/metamagic/renderer.rb', line 87

def separator
  @separator ||= DEFAULT_SEPARATOR
end

#siteObject


81
82
83
# File 'lib/metamagic/renderer.rb', line 81

def site
  @site or raise "Metamagic site not set. Please use `metamagic site: 'My Site'` to set it."
end

#title_templateObject


75
76
77
# File 'lib/metamagic/renderer.rb', line 75

def title_template
  @title_template ||= ":title"
end

Class Method Details

.register_tag_type(prefix, klass) ⇒ Object


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

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

.tag_type_for_key(key) ⇒ Object


24
25
26
27
# File 'lib/metamagic/renderer.rb', line 24

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

.tag_typesObject


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

def tag_types
  @tag_types ||= DEFAULT_TAG_TYPES.dup
end

Instance Method Details

#add(hash = {}, enable_templates = false) ⇒ Object

Raises:

  • (ArgumentError)

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metamagic/renderer.rb', line 40

def add(hash = {}, enable_templates = false)
  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 |key, value|
    if enable_templates && is_template?(value)
      add_template key, value
      value = nil
    end

    klass = self.class.tag_type_for_key(key)
    tag = if klass.is_a?(Proc)
      CustomTag.new(self, key, value, klass)
    else
      klass.new(self, key, value)
    end
    tags << tag unless tags.include?(tag)
  end
end

#add_template(key, value) ⇒ Object


63
64
65
# File 'lib/metamagic/renderer.rb', line 63

def add_template(key, value)
  templates[key] = value
end

#has_tag_type?(prefix) ⇒ Boolean

Returns:

  • (Boolean)

71
72
73
# File 'lib/metamagic/renderer.rb', line 71

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

#renderObject


93
94
95
# File 'lib/metamagic/renderer.rb', line 93

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

#tagsObject


36
37
38
# File 'lib/metamagic/renderer.rb', line 36

def tags
  @tags ||= []
end

#template_for(key) ⇒ Object


67
68
69
# File 'lib/metamagic/renderer.rb', line 67

def template_for(key)
  templates[key]
end

#templatesObject


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

def templates
  @templates ||= {}
end