Class: MetaTags::MetaTagsCollection
- Inherits:
-
Object
- Object
- MetaTags::MetaTagsCollection
- Defined in:
- lib/meta_tags-rails/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_noindex ⇒ 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.
-
#update(meta_tags = {}) ⇒ 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.
9 10 11 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 9 def initialize @meta_tags = HashWithIndifferentAccess.new end |
Instance Attribute Details
#meta_tags ⇒ Object (readonly)
Returns the value of attribute meta_tags.
5 6 7 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 5 def @meta_tags end |
Instance Method Details
#[](name) ⇒ Object
Returns meta tag value by name.
18 19 20 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 18 def [](name) @meta_tags[name] end |
#[]=(name, value) ⇒ Object
Sets meta tag value by name.
28 29 30 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 28 def []=(name, value) @meta_tags[name] = value end |
#delete(*names) ⇒ Object
Deletes specified meta tags.
76 77 78 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 76 def delete(*names) names.each { |name| @meta_tags.delete(name) } end |
#extract(name) ⇒ Object
Deletes and returns a meta tag value by name.
68 69 70 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 68 def extract(name) @meta_tags.delete(name) end |
#extract_full_title ⇒ String
Extracts full page title and deletes all related meta tags.
84 85 86 87 88 89 90 91 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 84 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_noindex ⇒ Hash<String,String>
Extracts noindex settings as a Hash mapping noindex tag name to value.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 128 def extract_noindex noindex_name, noindex_value = extract_noindex_attribute(:noindex) nofollow_name, nofollow_value = extract_noindex_attribute(:nofollow) if noindex_name == nofollow_name { noindex_name => [noindex_value, nofollow_value].compact.join(', ') } else { noindex_name => noindex_value, nofollow_name => nofollow_value } end end |
#extract_separator ⇒ String
Extracts title separator as a string.
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 110 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.
97 98 99 100 101 102 103 104 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 97 def extract_title title = extract(:title).presence return unless title title = Array(title) title.each(&: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.
59 60 61 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 59 def full_title(defaults = {}) with_defaults(defaults) { extract_full_title } end |
#update(meta_tags = {}) ⇒ Hash
Recursively merges a Hash of meta tag attributes into current list.
37 38 39 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 37 def update( = {}) @meta_tags.deep_merge! normalize_open_graph() end |
#with_defaults(defaults = {}) ⇒ Object
Temporary merges defaults with current meta tags list and yields the block.
46 47 48 49 50 51 52 |
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 46 def with_defaults(defaults = {}) = @meta_tags @meta_tags = normalize_open_graph(defaults).deep_merge!(self.) yield ensure @meta_tags = end |