class Line::Message::Builder::Context

The Context class is a crucial component of the Line::Message::Builder DSL, enabling a flexible and dynamic environment for message construction. It acts as a wrapper around an optional user-provided context object and manages a separate hash of assigns (local variables for the DSL).

The primary purposes of the Context are:

  1. To provide access to helper methods: If a user passes a context object (e.g., a Rails view context, a presenter, or any custom object) during builder initialization, methods defined on that object become directly callable within the DSL blocks.

  2. To allow local data storage: The assigns hash allows for setting and retrieving temporary data that can be shared across different parts of a complex message construction block, without needing to pass it explicitly or pollute the user-provided context.

Method calls within the DSL that are not defined on the builder objects themselves are resolved by Context in the following order:

This mechanism allows for a clean and powerful way to integrate external logic and data into the message building process.

Example: Using a custom context

class MyContext
  def current_user_name
    "Alice"
  end
end

context = MyContext.new
Line::Message::Builder.with(context) do
  # current_user_name is resolved from context by Context
  text "Hello, #{current_user_name}!"
end