Class: Charming::Layout::Builder
- Inherits:
-
Object
- Object
- Charming::Layout::Builder
- Defined in:
- lib/charming/presentation/layout/builder.rb
Overview
Builder turns a declarative ‘screen_layout { … }` block into a layout tree of ScreenLayout → Split → Pane nodes. The block DSL is `split(direction) { … }`, `pane(name) { … }`, and `overlay { … }`. Unknown method calls in the block are forwarded to the underlying view so view helpers (e.g., `text`) work inside layout blocks.
Class Method Summary collapse
-
.build(screen:, view:, background: nil) ⇒ Object
Builds the layout tree by evaluating the block in the builder’s context.
Instance Method Summary collapse
-
#build ⇒ Object
Evaluates block in the builder’s context, then returns the root ScreenLayout node.
-
#initialize(screen:, view:, background: nil) ⇒ Builder
constructor
A new instance of Builder.
- #method_missing(name) ⇒ Object
-
#overlay(content = nil, top: :center, left: :center, **options, &block) ⇒ Object
Adds an Overlay node to the root ScreenLayout.
-
#pane(name = nil, content = nil, **options, &block) ⇒ Object
Adds a Pane leaf node to the current scope.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Forwards unknown method calls to the underlying view so helpers like ‘text` work inside layout blocks.
-
#split(direction, gap: 0, **options) ⇒ Object
Adds a Split node to the current scope.
Constructor Details
#initialize(screen:, view:, background: nil) ⇒ Builder
Returns a new instance of Builder.
16 17 18 19 20 |
# File 'lib/charming/presentation/layout/builder.rb', line 16 def initialize(screen:, view:, background: nil) @view = view @root = ScreenLayout.new(screen: screen, background: background) @stack = [@root] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
58 59 60 61 62 |
# File 'lib/charming/presentation/layout/builder.rb', line 58 def method_missing(name, ...) return view.__send__(name, ...) if view.respond_to?(name, true) super end |
Class Method Details
.build(screen:, view:, background: nil) ⇒ Object
Builds the layout tree by evaluating the block in the builder’s context. Returns the root ScreenLayout node.
12 13 14 |
# File 'lib/charming/presentation/layout/builder.rb', line 12 def self.build(screen:, view:, background: nil, &) new(screen: screen, view: view, background: background).build(&) end |
Instance Method Details
#build ⇒ Object
Evaluates block in the builder’s context, then returns the root ScreenLayout node.
23 24 25 26 |
# File 'lib/charming/presentation/layout/builder.rb', line 23 def build(&) instance_eval(&) if block_given? root end |
#overlay(content = nil, top: :center, left: :center, **options, &block) ⇒ Object
Adds an Overlay node to the root ScreenLayout. top and left default to :center. The block, if given, is evaluated in the view’s context.
48 49 50 |
# File 'lib/charming/presentation/layout/builder.rb', line 48 def (content = nil, top: :center, left: :center, **, &block) root.(Overlay.new(content: content, block: block, view: view, top: top, left: left, **)) end |
#pane(name = nil, content = nil, **options, &block) ⇒ Object
Adds a Pane leaf node to the current scope. name (optional) is the focus slot name; content (or a block) is the body. options are forwarded to Pane.
40 41 42 43 44 |
# File 'lib/charming/presentation/layout/builder.rb', line 40 def pane(name = nil, content = nil, **, &block) node = Pane.new(name: name, content: content, block: block, view: view, **) append(node) node end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Forwards unknown method calls to the underlying view so helpers like ‘text` work inside layout blocks.
54 55 56 |
# File 'lib/charming/presentation/layout/builder.rb', line 54 def respond_to_missing?(name, include_private = false) view.respond_to?(name, include_private) || super end |
#split(direction, gap: 0, **options) ⇒ Object
Adds a Split node to the current scope. direction is ‘:horizontal` or `:vertical`. gap (in cells) is inserted between children. Additional options are forwarded to Split. The block, if given, is evaluated in the split’s scope (for nested children).
31 32 33 34 35 36 |
# File 'lib/charming/presentation/layout/builder.rb', line 31 def split(direction, gap: 0, **, &) node = Split.new(direction: direction, gap: gap, **) append(node) within(node, &) node end |