Class: PageMeta::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/page_meta/base.rb

Constant Summary collapse

DEFAULT_META_TAGS =
%i[
  language
  charset
  title
  viewport
  keywords
  description
].freeze
DEFAULT_ORDER =
99
META_TAG_ORDER =
{
  pragma: -1,
  cache_control: -1,
  expires: -1,
  refresh: -1,
  dns_prefetch_control: 4
}.freeze
{
  preconnect: 6,
  preload: 7,
  modulepreload: 8,
  prefetch: 9,
  dns_prefetch: 10
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Base

Returns a new instance of Base.



36
37
38
39
40
# File 'lib/page_meta/base.rb', line 36

def initialize(controller)
  @controller = controller
  @description = {}
  @store = {}
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



32
33
34
# File 'lib/page_meta/base.rb', line 32

def controller
  @controller
end

#storeObject (readonly)

Returns the value of attribute store.



32
33
34
# File 'lib/page_meta/base.rb', line 32

def store
  @store
end

Instance Method Details

#base(href) ⇒ Object

Add a new ‘<base>` tag.



47
48
49
50
51
52
53
54
55
# File 'lib/page_meta/base.rb', line 47

def base(href)
  items << {
    type: :tag,
    name: :base,
    value: {href:},
    order: 2,
    open: true
  }
end

#delete(name) ⇒ Object

Delete all matching meta tags by name.



58
59
60
# File 'lib/page_meta/base.rb', line 58

def delete(name)
  items.delete_if { _1[:name] == name }
end

#description(html: false) ⇒ Object

The description translation.



85
86
87
88
89
90
91
# File 'lib/page_meta/base.rb', line 85

def description(html: false)
  @description[html] ||= Translator.new(
    :description,
    naming,
    store.merge(html:)
  )
end

#html(name, value, open: false, order: DEFAULT_ORDER) ⇒ Object

Add new html tag, like ‘<base>` or `<title>`.



75
76
77
# File 'lib/page_meta/base.rb', line 75

def html(name, value, open: false, order: DEFAULT_ORDER)
  items << {type: :tag, name:, order:, open:, value:}
end

#itemsObject



42
43
44
# File 'lib/page_meta/base.rb', line 42

def items
  @items ||= []
end

#keywordsObject

The keywords translation.



94
95
96
# File 'lib/page_meta/base.rb', line 94

def keywords
  @keywords ||= Translator.new(:keywords, naming, store)
end

Add a new link tag.



69
70
71
72
# File 'lib/page_meta/base.rb', line 69

def link(rel, order: nil, **options)
  order = order || LINK_ORDER[rel] || DEFAULT_ORDER
  items << {type: :link, rel:, options:, order:}
end

#renderObject Also known as: to_s



98
99
100
101
102
103
104
105
106
# File 'lib/page_meta/base.rb', line 98

def render
  compute_default_items

  items
    .sort_by { _1[:order] }
    .map { send(:"build_#{_1[:type]}", _1).render }
    .join
    .html_safe
end

#tag(name, value = nil, order: nil, **kwargs) ⇒ Object

Add a new meta tag.



63
64
65
66
# File 'lib/page_meta/base.rb', line 63

def tag(name, value = nil, order: nil, **kwargs)
  order = order || META_TAG_ORDER[name] || DEFAULT_ORDER
  items << {type: :meta, name:, value: value || kwargs, order:}
end

#titleObject

The title translation.



80
81
82
# File 'lib/page_meta/base.rb', line 80

def title
  @title ||= Translator.new(:title, naming, store)
end