Class: MetaTags::MetaTagsCollection
- Inherits:
-
Object
- Object
- MetaTags::MetaTagsCollection
- 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
-
#meta_tags ⇒ Object
readonly
Returns the value of attribute meta_tags.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns meta tag value by name.
-
#[]=(name, value) ⇒ Object
Sets meta tag value by name.
-
#delete(*names) ⇒ Object
Deletes specified meta tags.
-
#extract(name) ⇒ Object
Deletes and returns a meta tag value by name.
-
#extract_full_title ⇒ String
Extracts full page title and deletes all related meta tags.
-
#extract_robots ⇒ Hash{String => String}
Extracts noindex settings as a Hash mapping noindex tag name to value.
-
#extract_separator ⇒ String
Extracts title separator as a string.
-
#extract_title ⇒ Array<String>
Extracts page title as an array of segments without site title and separators.
-
#full_title(defaults = {}) ⇒ String
Constructs the full title as if it would be rendered in title meta tag.
-
#initialize ⇒ MetaTagsCollection
constructor
Initializes a new instance of MetaTagsCollection.
-
#page_title(defaults = {}) ⇒ String
Constructs the title without site title (for normalized parameters).
-
#update(object = {}) ⇒ Hash
Recursively merges a Hash of meta tag attributes into the current list.
-
#with_defaults(defaults = {}) ⇒ Object
Temporarily merges defaults with the current meta tags list and yields the block.
Constructor Details
#initialize ⇒ MetaTagsCollection
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_tags ⇒ Object (readonly)
Returns the value of attribute meta_tags.
7 8 9 |
# File 'lib/meta_tags/meta_tags_collection.rb', line 7 def @meta_tags end |
Instance Method Details
#[](name) ⇒ Object
Returns meta tag value by name.
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.
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.
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.
86 87 88 |
# File 'lib/meta_tags/meta_tags_collection.rb', line 86 def extract(name) @meta_tags.delete(name) end |
#extract_full_title ⇒ String
Extracts full page title and deletes all related meta tags.
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_robots ⇒ Hash{String => String}
Extracts noindex settings as a Hash mapping noindex tag name to value.
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_separator ⇒ String
Extracts title separator as a string.
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 [: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_title ⇒ Array<String>
Extracts page title as an array of segments without site title and separators.
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.
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.
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.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/meta_tags/meta_tags_collection.rb', line 36 def update(object = {}) = if object.respond_to?(:to_meta_tags) # @type var object: _MetaTagish & Object object. else # @type var object: Hash[String | Symbol, untyped] object end @meta_tags.deep_merge! normalize_open_graph() end |
#with_defaults(defaults = {}) ⇒ Object
Temporarily merges defaults with the current meta tags list and yields the block.
51 52 53 54 55 56 57 |
# File 'lib/meta_tags/meta_tags_collection.rb', line 51 def with_defaults(defaults = {}) = @meta_tags @meta_tags = normalize_open_graph(defaults).deep_merge!(@meta_tags) yield ensure @meta_tags = end |