Class: Line::Message::Builder::Flex::Span

Inherits:
Base
  • Object
show all
Includes:
Line::Message::Builder::Flex::Size::Shared
Defined in:
lib/line/message/builder/flex/span.rb

Overview

Represents a span component in a LINE Flex Message.

Span components are used within a Text component to apply different styling to specific portions of text. They offer various styling options, including size, weight, color, style and decoration. Unlike Text components, spans cannot have actions attached to them.

A decoration set on the surrounding Text cannot be overridden by a span.

Example

Line::Message::Builder.with do
flex alt_text: "Span Example" do
  bubble do
    body do
      text do
        span "Hello, ", color: "#FF0000"
        span "Flex ", weight: :bold
        span "World!", size: :xl, decoration: :underline
      end
    end
  end
end
end

See also:

Instance Attribute Summary collapse

Attributes inherited from Base

#context

Instance Method Summary collapse

Methods included from Line::Message::Builder::Flex::Size::Shared

included

Methods inherited from Base

inherited, #method_missing, #quick_reply, #respond_to_missing?

Constructor Details

#initialize(text_content, context: nil, **options) ⇒ Span

Initializes a new Flex Message Span component.

[text_content] The text to display. This is required [context] An optional context for the builder [options] A hash of options to set instance variables (e.g., :color, :weight, :decoration, and options from included modules) [block] An optional block, typically not used for spans

Example

span "Hello", color: "#FF0000", weight: :bold


116
117
118
119
120
# File 'lib/line/message/builder/flex/span.rb', line 116

def initialize(text_content, context: nil, **options, &)
  @text = text_content # The text content is mandatory.

  super(context: context, **options, &) # Sets options and evals block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Line::Message::Builder::Base

Instance Attribute Details

#textObject (readonly)

The actual text content to be displayed. This is a required attribute.



39
40
41
# File 'lib/line/message/builder/flex/span.rb', line 39

def text
  @text
end

Instance Method Details

#bold!Object

Sets weight to bold.

Example

span "Bold text" do
bold!
end


129
130
131
# File 'lib/line/message/builder/flex/span.rb', line 129

def bold!
  weight(:bold)
end

#italic!Object

Sets style to italic.

Example

span "Emphasised text" do
italic!
end


140
141
142
# File 'lib/line/message/builder/flex/span.rb', line 140

def italic!
  style(:italic)
end

#line_through!Object

Sets decoration to line-through.

Example

span "Strikethrough text" do
line_through!
end


162
163
164
# File 'lib/line/message/builder/flex/span.rb', line 162

def line_through!
  decoration(:"line-through")
end

#to_hObject

:nodoc:

Raises:



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/line/message/builder/flex/span.rb', line 167

def to_h
  raise RequiredError, "text content is required for a span component" if text.nil?

  {
    type: "span",
    text: text,
    color: color,
    size: size,
    weight: weight,
    style: style,
    decoration: decoration
  }.compact
end

#underline!Object

Sets decoration to underline.

Example

span "Underlined text" do
underline!
end


151
152
153
# File 'lib/line/message/builder/flex/span.rb', line 151

def underline!
  decoration(:underline)
end