Class: MetaTags::MetaTagsCollection

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

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

#initialize ⇒ MetaTagsCollection

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_tags ⇒ Hash[String | Symbol, untyped] (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.



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.



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

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

#apply_robots_value(result, name, value, processed) ⇒ nil, untyped



219
220
221
222
223
224
225
# File 'lib/meta_tags/meta_tags_collection.rb', line 219

def apply_robots_value(result, name, value, processed)
  name = name.to_s
  return if processed.include?(name)

  result[name] << value
  processed << name
end

#calculate_robots_attributes(result, attributes) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/meta_tags/meta_tags_collection.rb', line 207

def calculate_robots_attributes(result, attributes)
  processed = Set.new
  Array(attributes).each do |attribute|
    names, value = extract_robots_attribute(attribute)
    next unless value

    Array(names).each do |name|
      apply_robots_value(result, name, value, processed)
    end
  end
end

#delete(*names) ⇒ void

This method returns an undefined value.

Deletes specified meta tags.



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.



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

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

#extract_full_title ⇒ String

Extracts full page title and deletes all related meta tags.



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_robots ⇒ Hash<String,String>

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



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_robots_attribute(name) ⇒ Array<String>

Extracts robots attribute (noindex, nofollow, etc) name and value.



199
200
201
202
203
204
205
# File 'lib/meta_tags/meta_tags_collection.rb', line 199

def extract_robots_attribute(name)
  noindex       = extract(name)
  noindex_name  = noindex.kind_of?(String) || noindex.kind_of?(Array) ? noindex : 'robots'
  noindex_value = noindex ? name.to_s : nil

  [ noindex_name, noindex_value ]
end

#extract_separator ⇒ String

Extracts title separator as a string.



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_separator_section(name, default) ⇒ String

Extracts separator segment without deleting it from meta tags list. If the value is false, empty string will be returned.



190
191
192
# File 'lib/meta_tags/meta_tags_collection.rb', line 190

def extract_separator_section(name, default)
  meta_tags[name] == false ? '' : (meta_tags[name] || default)
end

#extract_title ⇒ Array<String>

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



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.



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

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

#normalize_open_graph(meta_tags) ⇒ ActiveSupport::HashWithIndifferentAccess

Converts input hash to HashWithIndifferentAccess and renames :open_graph to :og.



177
178
179
180
181
# File 'lib/meta_tags/meta_tags_collection.rb', line 177

def normalize_open_graph(meta_tags)
  meta_tags = meta_tags.with_indifferent_access
  meta_tags[:og] = meta_tags.delete(:open_graph) if meta_tags.key?(:open_graph)
  meta_tags
end

#page_title(defaults = {}) ⇒ String

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



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.



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.

Yields:

Yield Returns:



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