Class: Arrow::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/struct.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, index) ⇒ Struct

Returns a new instance of Struct.



21
22
23
24
# File 'lib/arrow/struct.rb', line 21

def initialize(array, index)
  @array = array
  @index = index
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/arrow/struct.rb', line 60

def method_missing(name, *args, &block)
  if args.empty?
    field = @array.find_field(name)
    return field[@index] if field
  end
  super
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



20
21
22
# File 'lib/arrow/struct.rb', line 20

def index
  @index
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
# File 'lib/arrow/struct.rb', line 68

def ==(other)
  other.is_a?(self.class) and
    @array == other.array and
    @index == other.index
end

#[](field_name_or_field_index) ⇒ Object



26
27
28
29
30
# File 'lib/arrow/struct.rb', line 26

def [](field_name_or_field_index)
  field = @array.find_field(field_name_or_field_index)
  return nil if field.nil?
  field[@index]
end

#fieldsObject



32
33
34
# File 'lib/arrow/struct.rb', line 32

def fields
  @array.value_data_type.fields
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/arrow/struct.rb', line 55

def respond_to_missing?(name, include_private)
  return true if @array.find_field(name)
  super
end

#to_aObject



42
43
44
# File 'lib/arrow/struct.rb', line 42

def to_a
  values
end

#to_hObject



46
47
48
49
50
51
52
53
# File 'lib/arrow/struct.rb', line 46

def to_h
  attributes = {}
  field_arrays = @array.fields
  fields.each_with_index do |field, i|
    attributes[field.name] = field_arrays[i][@index]
  end
  attributes
end

#valuesObject



36
37
38
39
40
# File 'lib/arrow/struct.rb', line 36

def values
  @array.fields.collect do |field|
    field[@index]
  end
end