Class: MetaTags::MetaTagsCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeMetaTagsCollection

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_tagsObject (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
  @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.



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.

Parameters:

  • name (String, Symbol)

    meta tag name.

  • value

    meta tag value.

Returns:

  • meta tag value.



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.

Parameters:

  • names (Array<String, Symbol>)

    a list of meta tags to delete.



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.

Parameters:

  • name (String, Symbol)

    meta tag name.

Returns:

  • (Object)

    meta tag value.



68
69
70
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 68

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.



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_noindexHash<String,String>

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

Returns:

  • (Hash<String,String>)

    noindex attributes.



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_separatorString

Extracts title separator as a string.

Returns:

  • (String)

    page title separator.



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



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.

Parameters:

  • defaults (Hash) (defaults to: {})

    list of default meta tag attributes.

Returns:

  • (String)

    page title.



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.

Parameters:

  • meta_tags (Hash) (defaults to: {})

    meta tags Hash to merge into current list.

Returns:

  • (Hash)

    result of the merge.



37
38
39
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 37

def update(meta_tags = {})
  @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.



46
47
48
49
50
51
52
# File 'lib/meta_tags-rails/meta_tags_collection.rb', line 46

def with_defaults(defaults = {})
  old_meta_tags = @meta_tags
  @meta_tags = normalize_open_graph(defaults).deep_merge!(self.meta_tags)
  yield
ensure
  @meta_tags = old_meta_tags
end