Class: SparkConnect::Row
- Inherits:
-
Object
- Object
- SparkConnect::Row
- Includes:
- Enumerable
- Defined in:
- lib/spark_connect/row.rb
Overview
An ordered collection of named fields representing a single row of a DataFrame, returned by DataFrame#collect, DataFrame#take, etc.
Fields are accessible positionally (‘row`), by name (`row` or `row.id`), and the whole row converts cleanly to a Hash or Array.
Instance Attribute Summary collapse
-
#fields ⇒ Array<String>
readonly
The field names, in order.
-
#values ⇒ Array
readonly
The field values, in order.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#[](key) ⇒ Object?
Look up a value by zero-based index or by field name.
-
#each ⇒ Object
Iterate over the values in order.
-
#field(name) ⇒ Object
The value for ‘name`, raising if the field is absent.
- #hash ⇒ Object
-
#initialize(data = {}, fields: nil) ⇒ Row
constructor
A new instance of Row.
-
#length ⇒ Integer
(also: #size)
Number of fields.
-
#method_missing(name, *args) ⇒ Object
Allows ‘row.field_name` access for field names that are valid method names.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#to_a ⇒ Array
The row’s values, in order.
-
#to_h ⇒ Hash
(also: #as_dict)
An ordered Hash of field name to value.
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(hash) ⇒ Row #initialize(values, fields:) ⇒ Row
Returns a new instance of Row.
30 31 32 33 34 35 36 37 38 |
# File 'lib/spark_connect/row.rb', line 30 def initialize(data = {}, fields: nil) if fields @fields = fields.map(&:to_s) @values = data else @fields = data.keys.map(&:to_s) @values = data.values end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Allows ‘row.field_name` access for field names that are valid method names.
87 88 89 90 91 92 93 94 |
# File 'lib/spark_connect/row.rb', line 87 def method_missing(name, *args) key = name.to_s if args.empty? && @fields.include?(key) self[key] else super end end |
Instance Attribute Details
#fields ⇒ Array<String> (readonly)
Returns the field names, in order.
20 21 22 |
# File 'lib/spark_connect/row.rb', line 20 def fields @fields end |
#values ⇒ Array (readonly)
Returns the field values, in order.
23 24 25 |
# File 'lib/spark_connect/row.rb', line 23 def values @values end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
79 80 81 |
# File 'lib/spark_connect/row.rb', line 79 def ==(other) other.is_a?(Row) && other.fields == fields && other.values == values end |
#[](key) ⇒ Object?
Look up a value by zero-based index or by field name.
44 45 46 47 48 49 50 51 |
# File 'lib/spark_connect/row.rb', line 44 def [](key) case key when Integer then @values[key] else idx = @fields.index(key.to_s) idx && @values[idx] end end |
#each ⇒ Object
Iterate over the values in order.
65 |
# File 'lib/spark_connect/row.rb', line 65 def each(&) = @values.each(&) |
#field(name) ⇒ Object
Returns the value for ‘name`, raising if the field is absent.
72 73 74 75 76 77 |
# File 'lib/spark_connect/row.rb', line 72 def field(name) idx = @fields.index(name.to_s) raise IllegalArgumentError, "No such field: #{name}" unless idx @values[idx] end |
#hash ⇒ Object
84 |
# File 'lib/spark_connect/row.rb', line 84 def hash = [fields, values].hash |
#length ⇒ Integer Also known as: size
Returns number of fields.
68 |
# File 'lib/spark_connect/row.rb', line 68 def length = @values.length |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
96 97 98 |
# File 'lib/spark_connect/row.rb', line 96 def respond_to_missing?(name, include_private = false) @fields.include?(name.to_s) || super end |
#to_a ⇒ Array
Returns the row’s values, in order.
60 61 62 |
# File 'lib/spark_connect/row.rb', line 60 def to_a @values.dup end |
#to_h ⇒ Hash Also known as: as_dict
Returns an ordered Hash of field name to value.
54 55 56 |
# File 'lib/spark_connect/row.rb', line 54 def to_h @fields.zip(@values).to_h end |
#to_s ⇒ Object Also known as: inspect
100 101 102 |
# File 'lib/spark_connect/row.rb', line 100 def to_s "Row(#{@fields.zip(@values).map { |k, v| "#{k}=#{v.inspect}" }.join(', ')})" end |