Class: MetaTags::MetaTagsCollection

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

Overview

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.



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

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.



20
21
22
# File 'lib/meta_tags/meta_tags_collection.rb', line 20

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.



30
31
32
# File 'lib/meta_tags/meta_tags_collection.rb', line 30

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

#delete(*names) ⇒ Object

Deletes specified meta tags.

Parameters:

  • names (Array<String, Symbol>)

    a list of meta tags to delete.



100
101
102
# File 'lib/meta_tags/meta_tags_collection.rb', line 100

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.



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

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.



108
109
110
111
112
113
114
115
# File 'lib/meta_tags/meta_tags_collection.rb', line 108

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.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/meta_tags/meta_tags_collection.rb', line 154

def extract_robots
  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.



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/meta_tags/meta_tags_collection.rb', line 136

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.



121
122
123
124
125
126
127
128
129
130
# File 'lib/meta_tags/meta_tags_collection.rb', line 121

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.



69
70
71
# File 'lib/meta_tags/meta_tags_collection.rb', line 69

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.

Returns:

  • (String)

    page title.



78
79
80
81
82
83
84
85
# File 'lib/meta_tags/meta_tags_collection.rb', line 78

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

#update(object = {}) ⇒ Hash

Recursively merges a Hash of meta tag attributes into 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.



40
41
42
43
44
45
46
47
48
49
# File 'lib/meta_tags/meta_tags_collection.rb', line 40

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

Temporary merges defaults with 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.



56
57
58
59
60
61
62
# File 'lib/meta_tags/meta_tags_collection.rb', line 56

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