Class: Metamagic::Tag

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

Direct Known Subclasses

CustomTag, LinkTag, MetaTag, PropertyTag, TitleTag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, key, value) ⇒ Tag

Returns a new instance of Tag.



5
6
7
8
# File 'lib/metamagic/tag.rb', line 5

def initialize(context, key, value)
  @context, @key, @value = context, key.to_s, value
  @key = @key.gsub /^[^:]+:/, "" if remove_prefix?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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

def method_missing(method, *args, &block)
  return value if method.to_s == key.gsub(":", "_") # When calling e.g. `og_image`. Used for interpolating values.
  context.send(method, *args, &block)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/metamagic/tag.rb', line 3

def context
  @context
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/metamagic/tag.rb', line 3

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/metamagic/tag.rb', line 3

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



47
48
49
# File 'lib/metamagic/tag.rb', line 47

def <=>(other)
  [sort_order, self.class.name] <=> [other.sort_order, other.class.name]
end

#==(other) ⇒ Object



43
44
45
# File 'lib/metamagic/tag.rb', line 43

def ==(other)
  self.class == other.class && self.key == other.key
end

#interpolated_valuesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/metamagic/tag.rb', line 26

def interpolated_values
  @interpolated_values ||= Array(template).map do |template|
    case template
    when Proc
      instance_exec(&template)
    when Symbol
      send(template)
    when String
      ERB::Util.html_escape(template).gsub(/:\w+/) do |key|
        ERB::Util.html_escape(send(key[1..-1]))
      end.html_safe
    else
      raise "Unknown template type #{template.class}."
    end
  end.flatten.compact.uniq.map { |value| ERB::Util.html_escape(value) }
end

#remove_prefix?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/metamagic/tag.rb', line 18

def remove_prefix?
  true
end

#sort_orderObject



14
15
16
# File 'lib/metamagic/tag.rb', line 14

def sort_order
  1000
end

#templateObject



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

def template
  @template ||= template_for(key) || :value
end

#to_htmlObject



10
11
12
# File 'lib/metamagic/tag.rb', line 10

def to_html
  raise "#{self.class.name}#to_html must be overridden to render tag"
end