Class: MetaTags::MetaTagsCollection
- Inherits:
-
Object
- Object
- MetaTags::MetaTagsCollection
- 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
-
#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 current list.
-
#with_defaults(defaults = {}) ⇒ Object
Temporary merges defaults with current meta tags list and yields the block.
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 ⇒ 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.
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 |
#delete(*names) ⇒ Object
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_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 [: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.
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 |
#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 = {}) = 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
Temporary merges defaults with current meta tags list and yields the block.
56 57 58 59 60 61 62 |
# File 'lib/meta_tags/meta_tags_collection.rb', line 56 def with_defaults(defaults = {}) = @meta_tags @meta_tags = normalize_open_graph(defaults).deep_merge!(@meta_tags) yield ensure @meta_tags = end |