Class: Bootstrap5Helper::Badge

Inherits:
Component show all
Defined in:
lib/bootstrap5_helper/badge.rb

Overview

Creates Bootstrap badge components that can be used anywhere.

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #uuid

Constructor Details

#initialize(template, context_or_options = nil, opts = {}, &block) ⇒ Badge

Class constructor

Parameters:

  • template (ActionView)
  • context_or_options (NilClass|String|Symbol|Hash) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • : (Hash)

    :data



15
16
17
18
19
20
21
22
23
24
# File 'lib/bootstrap5_helper/badge.rb', line 15

def initialize(template, context_or_options = nil, opts = {}, &block)
  super(template)
  @context, args = parse_context_or_options(context_or_options, opts)

  @attrs   = args
  @id      = @attrs.delete(:id)    { nil }
  @class   = @attrs.delete(:class) { '' }
  @data    = @attrs.delete(:data)  { {} }
  @content = block || proc { '' }
end

Instance Method Details

#to_sString

String representation of the object.

Returns:

  • (String)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bootstrap5_helper/badge.rb', line 30

def to_s
  (
    config({ badges: :base }, :span),
    {
      id:    @id,
      class: container_class,
      data:  @data
    }.merge(@attrs)
  ) do
    @content.call(self)
  end
end