Class: Dommy::Blob
- Inherits:
-
Object
- Object
- Dommy::Blob
- Includes:
- Dommy::Bridge::Methods
- Defined in:
- lib/dommy/blob.rb
Overview
‘Blob` — opaque binary chunk with a MIME type, mirroring the File API’s ‘Blob` interface. Used by File, FormData, and any code that needs to round-trip bytes through the DOM (e.g. a `<input type=“file”>` test scenario).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#__dommy_bytes__ ⇒ Object
Raw binary bytes (Ruby ASCII-8BIT string).
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
-
#array_buffer ⇒ Object
Read the bytes as a real ArrayBuffer (the spec return type, wrapped so it crosses the JS boundary as a bare ArrayBuffer rather than an Array/typed array).
-
#initialize(parts = [], options = {}, window = nil) ⇒ Blob
constructor
Construct a Blob from a list of parts.
-
#slice(start = 0, last = @size, content_type = "") ⇒ Object
Return a new Blob over a byte range of this one.
-
#text ⇒ Object
Read the bytes as UTF-8 text.
Methods included from Dommy::Bridge::Methods
Constructor Details
#initialize(parts = [], options = {}, window = nil) ⇒ Blob
Construct a Blob from a list of parts. Each part can be:
- String (treated as binary bytes)
- Blob / File (their bytes are concatenated)
- Array<Integer> (byte values, like ArrayBuffer)
- anything else: coerced via to_s
‘options` sets the MIME type (lowercased per spec). `window` (optional) lets the JS-facing `text()`/`arrayBuffer()` return real Promises (they need a scheduler). A window-less Blob falls back to a synchronous result, which `await` still handles.
23 24 25 26 27 28 29 30 |
# File 'lib/dommy/blob.rb', line 23 def initialize(parts = [], = {}, window = nil) parts = [parts] unless parts.is_a?(Array) @data = collect_bytes(parts) @size = @data.bytesize raw_type = ["type"] || [:type] || "" @type = raw_type.to_s.downcase @window = window end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
11 12 13 |
# File 'lib/dommy/blob.rb', line 11 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/dommy/blob.rb', line 11 def type @type end |
Instance Method Details
#__dommy_bytes__ ⇒ Object
Raw binary bytes (Ruby ASCII-8BIT string). Used by FormData / fetch when serializing multipart bodies.
56 57 58 |
# File 'lib/dommy/blob.rb', line 56 def __dommy_bytes__ @data end |
#__js_call__(method, args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dommy/blob.rb', line 73 def __js_call__(method, args) case method when "slice" slice(args[0] || 0, args[1] || @size, args[2] || "") when "text" # WHATWG: Blob.text() returns a Promise<string>. promise_or_value(text) when "arrayBuffer" # WHATWG: Blob.arrayBuffer() returns a Promise<ArrayBuffer>. promise_or_value(array_buffer) end end |
#__js_get__(key) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/dommy/blob.rb', line 60 def __js_get__(key) case key when "size" @size when "type" @type end end |
#array_buffer ⇒ Object
Read the bytes as a real ArrayBuffer (the spec return type, wrapped so it crosses the JS boundary as a bare ArrayBuffer rather than an Array/typed array). The DOM spec returns a Promise<ArrayBuffer>; Dommy is synchronous.
50 51 52 |
# File 'lib/dommy/blob.rb', line 50 def array_buffer Bridge::ArrayBuffer.new(@data.bytes) end |
#slice(start = 0, last = @size, content_type = "") ⇒ Object
Return a new Blob over a byte range of this one. Negative indices are treated as offsets from the end (per spec).
34 35 36 37 38 39 |
# File 'lib/dommy/blob.rb', line 34 def slice(start = 0, last = @size, content_type = "") s = clamp_index(start.to_i, @size) e = clamp_index(last.to_i, @size) e = s if e < s Blob.new([@data.byteslice(s, e - s) || ""], {"type" => content_type.to_s}, @window) end |
#text ⇒ Object
Read the bytes as UTF-8 text. The DOM spec returns a Promise, but Dommy is synchronous, so callers can use the result directly.
43 44 45 |
# File 'lib/dommy/blob.rb', line 43 def text @data.dup.force_encoding(Encoding::UTF_8) end |