Class: MetaTags::MetaTagsCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_tags/meta_tags_collection.rb

Overview

This class represents a collection of meta tags. Basically a wrapper around HashWithIndifferentAccess, with some additional helper methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetaTagsCollection

Initializes a new instance of MetaTagsCollection.



10
11
12
# File 'lib/meta_tags/meta_tags_collection.rb', line 10

def initialize
  @meta_tags = ActiveSupport::HashWithIndifferentAccess.new
end

Instance Attribute Details

#meta_tagsObject (readonly)

Returns the value of attribute meta_tags.



7
8
9
# File 'lib/meta_tags/meta_tags_collection.rb', line 7

def meta_tags
  @meta_tags
end

Instance Method Details

#[](name) ⇒ Object

Returns meta tag value by name.

Parameters:

  • name (String, Symbol)

    meta tag name.

Returns:

  • meta tag value.



18
19
20
# File 'lib/meta_tags/meta_tags_collection.rb', line 18

def [](name)
  @meta_tags[name]
end

#[]=(name, value) ⇒ Object

Sets meta tag value by name.

Parameters:

  • name (String, Symbol)

    meta tag name.

  • value

    meta tag value.

Returns:

  • meta tag value.



27
28
29
# File 'lib/meta_tags/meta_tags_collection.rb', line 27

def []=(name, value)
  @meta_tags[name] = value
end

#delete(*names) ⇒ Object

Deletes specified meta tags.

Parameters:

  • names (Array<String, Symbol>)

    list of meta tags to delete.



93
94
95
# File 'lib/meta_tags/meta_tags_collection.rb', line 93

def delete(*names)
  names.each { |name| @meta_tags.delete(name) }
end

#extract(name) ⇒ Object

Deletes and returns a meta tag value by name.

Parameters:

  • name (String, Symbol)

    meta tag name.

Returns:

  • (Object)

    meta tag value.



86
87
88
# File 'lib/meta_tags/meta_tags_collection.rb', line 86

def extract(name)
  @meta_tags.delete(name)
end

#extract_full_titleString

Extracts full page title and deletes all related meta tags.

Returns:

  • (String)

    page title.



100
101
102
103
104
105
106
107
# File 'lib/meta_tags/meta_tags_collection.rb', line 100

def extract_full_title
  site_title = extract(:site) || ""
  title = extract_title
  separator = extract_separator
  reverse = extract(:reverse) == true

  TextNormalizer.normalize_title(site_title, title, separator, reverse)
end

#extract_robotsHash{String => String}

Extracts noindex settings as a Hash mapping noindex tag name to value.

Returns:

  • (Hash{String => String})

    noindex attributes.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/meta_tags/meta_tags_collection.rb', line 143

def extract_robots
  # @type var result: Hash[String, Array[String]]
  result = Hash.new { |h, k| h[k] = [] }

  [
    # noindex has higher priority than index
    [:noindex, :index],
    # follow has higher priority than nofollow
    [:follow, :nofollow],
    :noarchive
  ].each do |attributes|
    calculate_robots_attributes(result, attributes)
  end

  result.transform_values { |v| v.join(", ") }
end

#extract_separatorString

Extracts title separator as a string.

Returns:

  • (String)

    page title separator.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/meta_tags/meta_tags_collection.rb', line 126

def extract_separator
  if meta_tags[:separator] == false
    # Special case: if separator is hidden, do not display suffix/prefix
    prefix = separator = suffix = ""
  else
    prefix = extract_separator_section(:prefix, " ")
    separator = extract_separator_section(:separator, "|")
    suffix = extract_separator_section(:suffix, " ")
  end
  delete(:separator, :prefix, :suffix)

  TextNormalizer.safe_join([prefix, separator, suffix], "")
end

#extract_titleArray<String>

Extracts page title as an array of segments without site title and separators.

Returns:

  • (Array<String>)

    segments of page title.



112
113
114
115
116
117
118
119
120
121
# File 'lib/meta_tags/meta_tags_collection.rb', line 112

def extract_title
  title = extract(:title).presence
  return [] unless title

  # @type var title: Array[String]
  title = Array(title)
  return title.map(&:downcase) if extract(:lowercase) == true

  title
end

#full_title(defaults = {}) ⇒ String

Constructs the full title as if it would be rendered in title meta tag.

Parameters:

  • defaults (Hash) (defaults to: {})

    list of default meta tag attributes.

Returns:

  • (String)

    page title.



63
64
65
# File 'lib/meta_tags/meta_tags_collection.rb', line 63

def full_title(defaults = {})
  with_defaults(defaults) { extract_full_title }
end

#page_title(defaults = {}) ⇒ String

Constructs the title without site title (for normalized parameters). When title is empty, use the site title instead.

Parameters:

  • defaults (Hash) (defaults to: {})

    list of default meta tag attributes.

Returns:

  • (String)

    page title.



72
73
74
75
76
77
78
79
80
# File 'lib/meta_tags/meta_tags_collection.rb', line 72

def page_title(defaults = {})
  had_site = @meta_tags.key?(:site)
  old_site = @meta_tags[:site]
  @meta_tags[:site] = nil if had_site
  full_title = with_defaults(defaults) { extract_full_title }
  full_title.presence || old_site || ""
ensure
  @meta_tags[:site] = old_site if had_site
end

#update(object = {}) ⇒ Hash

Recursively merges a Hash of meta tag attributes into the current list.

Parameters:

  • object (Hash, #to_meta_tags) (defaults to: {})

    Hash of meta tags (or object responding to #to_meta_tags and returning a hash) to merge into the current list.

Returns:

  • (Hash)

    result of the merge.



36
37
38
39
40
41
42
43
44
45
# File 'lib/meta_tags/meta_tags_collection.rb', line 36

def update(object = {})
  meta_tags = if object.respond_to?(:to_meta_tags)
    # @type var object: _MetaTagish & Object
    object.to_meta_tags
  else
    # @type var object: Hash[String | Symbol, untyped]
    object
  end
  @meta_tags.deep_merge! normalize_open_graph(meta_tags)
end

#with_defaults(defaults = {}) ⇒ Object

Temporarily merges defaults with the current meta tags list and yields the block.

Parameters:

  • defaults (Hash) (defaults to: {})

    list of default meta tag attributes.

Returns:

  • result of the block call.



51
52
53
54
55
56
57
# File 'lib/meta_tags/meta_tags_collection.rb', line 51

def with_defaults(defaults = {})
  old_meta_tags = @meta_tags
  @meta_tags = normalize_open_graph(defaults).deep_merge!(@meta_tags)
  yield
ensure
  @meta_tags = old_meta_tags
end