Class: Line::Message::Builder::Base
- Inherits:
-
Object
- Object
- Line::Message::Builder::Base
- Defined in:
- lib/line/message/builder/base.rb
Overview
The Base class serves as the foundation for all message builder classes within the Line::Message::Builder DSL. It provides core functionality for defining options, handling initialization, and delegating method calls to a context object.
This class is not typically used directly but is inherited by specific message type builders (e.g., Text, Flex::Builder).
Direct Known Subclasses
Actions::Message, Actions::Postback, Actions::Uri, Flex::Box, Flex::Bubble, Flex::Builder, Flex::Button, Flex::Carousel, Flex::Icon, Flex::Image, Flex::Separator, Flex::Span, Flex::Text, QuickReply, Text
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
The context object used for method delegation.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize(context: nil, **options, &block) ⇒ Base
constructor
Initializes a new instance of a builder class.
-
#method_missing(method_name) ⇒ Object
:nodoc:.
-
#quick_reply ⇒ Object
Defines a quick reply for the message.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:nodoc:.
-
#to_h ⇒ Object
Converts the builder to a hash representation.
Constructor Details
#initialize(context: nil, **options, &block) ⇒ Base
Initializes a new instance of a builder class.
[context]
An optional external context object. Methods not defined in the builder
will be delegated to this context if it responds to them. This allows
for using helper methods or accessing data from the surrounding environment
within the builder DSL.
[options]
A hash of options to set on the builder instance. These options are
typically defined using the option method in the builder class.
[block]
An optional block that is instance-eval'd within the new builder instance.
This is the primary way the DSL is used to define message content.
Example
builder = MyBuilder.new(context: view_context) do
text "Hello from context"
end
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/line/message/builder/base.rb', line 100 def initialize(context: nil, **, &block) @context = context @quick_reply = nil () self.class..each do |option| send(option, [option]) if .key?(option) end instance_eval(&block) if ::Kernel.block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
:nodoc:
140 141 142 143 144 145 146 |
# File 'lib/line/message/builder/base.rb', line 140 def method_missing(method_name, ...) # :nodoc: if context.respond_to?(method_name) context.public_send(method_name, ...) else super end end |
Instance Attribute Details
#context ⇒ Object (readonly)
The context object used for method delegation.
When methods are called on the builder that it doesn't respond to, they are delegated to this context object if it responds to them. This allows accessing helper methods and variables from the surrounding environment within the builder DSL.
79 80 81 |
# File 'lib/line/message/builder/base.rb', line 79 def context @context end |
Class Method Details
.inherited(subclass) ⇒ Object
:nodoc:
15 16 17 18 |
# File 'lib/line/message/builder/base.rb', line 15 def inherited(subclass) # :nodoc: super subclass.extend ClassMethods end |
Instance Method Details
#quick_reply ⇒ Object
Defines a quick reply for the message.
A quick reply consists of a set of buttons that are displayed along with the message, allowing users to make quick responses.
The provided block is executed in the context of a new QuickReply instance, where you can define the quick reply buttons.
:yields: quick_reply
Example
do
text "Please choose an option:"
quick_reply do
action: :message, label: "Option A", text: "You chose A"
action: :camera, label: "Open Camera"
end
end
132 133 134 |
# File 'lib/line/message/builder/base.rb', line 132 def quick_reply(&) @quick_reply = QuickReply.new(context: context, &) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:nodoc:
136 137 138 |
# File 'lib/line/message/builder/base.rb', line 136 def respond_to_missing?(method_name, include_private = false) # :nodoc: context.respond_to?(method_name, include_private) || super end |
#to_h ⇒ Object
Converts the builder to a hash representation.
The format of the hash depends on the mode (API or SDK v2) determined
by the builder's configuration. Subclasses must implement to_api and
to_sdkv2 methods to provide the actual conversion logic.
[return] A hash representation of the message in the appropriate format.
Example
builder = Text.new { text "Hello" }
builder.to_h
# => { type: "text", text: "Hello" }
162 163 164 165 166 |
# File 'lib/line/message/builder/base.rb', line 162 def to_h return to_sdkv2 if sdkv2? to_api end |