Class: Line::Message::Builder::Flex::Partial

Inherits:
Object
  • Object
show all
Defined in:
lib/line/message/builder/flex/partial.rb

Overview

The Partial class is an abstract base for creating reusable snippets of Flex Message content. To define a partial, create a new class that inherits from Partial and implements the call instance method.

Within the call method, you can use the standard Flex Message builder DSL methods (e.g., text, box, button) as if you were directly inside the component where the partial is being rendered. This is possible because the Partial instance delegates unknown method calls to the component instance that is rendering it (passed as context during initialization).

Subclass and implement call to create a concrete partial.

See also:

  • HasPartial for how to render partials

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ Partial

Initializes a new Partial instance. This is typically called by the HasPartial#partial! method.

[context] The component instance (e.g., Box, Bubble) within which this partial is being rendered. This context provides the DSL methods (like text, box) used in the partial's call method.



90
91
92
# File 'lib/line/message/builder/flex/partial.rb', line 90

def initialize(context:)
  @context = context # The component (e.g., Box, Bubble) rendering this partial
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

:nodoc:



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

def method_missing(method_name, ...) # :nodoc:
  if @context.respond_to?(method_name)
    @context.public_send(method_name, ...)
  else
    super
  end
end

Instance Method Details

#callObject

This method must be implemented by subclasses to define the content of the partial. Inside this method, use the Flex Message builder DSL methods (e.g., text "Hello", button { ... }) which will be delegated to the rendering context.

Any arguments passed to partial! as assigns are available via context.assigns or directly if the rendering context's method_missing handles assigns lookup (as Line::Message::Builder::Context does).

Arguments passed from the partial! call's assigns can be explicitly defined as parameters here, or accessed via context.assigns.

Raises NotImplementedError if a subclass does not implement this method.

Raises:

  • (NotImplementedError)


107
108
109
110
# File 'lib/line/message/builder/flex/partial.rb', line 107

def call(*)
  raise NotImplementedError,
        "The #{self.class.name} class must implement the #call method to define its content."
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


112
113
114
# File 'lib/line/message/builder/flex/partial.rb', line 112

def respond_to_missing?(method_name, include_private = false) # :nodoc:
  @context.respond_to?(method_name, include_private) || super
end