Class: BSV::Script::Builder
- Inherits:
-
Object
- Object
- BSV::Script::Builder
- Defined in:
- lib/bsv/script/builder.rb
Overview
Fluent builder for constructing scripts incrementally.
Provides a chainable API for building scripts from opcodes and data pushes, as an alternative to the template constructors on Script.
Instance Method Summary collapse
-
#build ⇒ Script
Finalise and return the constructed Script.
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
-
#push_data(data) ⇒ self
Push raw binary data (automatically selects PUSHDATA encoding).
-
#push_hex(hex) ⇒ self
Push hex-encoded data.
-
#push_op(opcode) ⇒ self
Push an opcode onto the script.
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
19 20 21 |
# File 'lib/bsv/script/builder.rb', line 19 def initialize @chunks = [] end |
Instance Method Details
#build ⇒ Script
Finalise and return the constructed Script.
54 55 56 |
# File 'lib/bsv/script/builder.rb', line 54 def build Script.from_chunks(@chunks) end |
#push_data(data) ⇒ self
Push raw binary data (automatically selects PUSHDATA encoding).
37 38 39 40 41 |
# File 'lib/bsv/script/builder.rb', line 37 def push_data(data) bytes = data.b @chunks << Chunk.new(opcode: push_opcode_for(bytes.bytesize), data: bytes) self end |
#push_hex(hex) ⇒ self
Push hex-encoded data.
47 48 49 |
# File 'lib/bsv/script/builder.rb', line 47 def push_hex(hex) push_data(BSV::Primitives::Hex.decode(hex, name: 'hex data')) end |