Class: Bud::TupleStruct

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/bud/monkeypatch.rb

Overview

FIXME: Should likely override #hash and #eql? as well.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_struct(cols) ⇒ Object



20
21
22
23
24
# File 'lib/bud/monkeypatch.rb', line 20

def self.new_struct(cols)
  $struct_lock.synchronize {
    ($struct_classes[cols] ||= Bud::TupleStruct.new(*cols))
  }
end

Instance Method Details

#+(o) ⇒ Object



65
66
67
# File 'lib/bud/monkeypatch.rb', line 65

def +(o)
  self.to_ary + o.to_ary
end

#<=>(o) ⇒ Object

XXX: This only considers two TupleStruct instances to be equal if they have the same schema (column names) AND the same contents; unclear if structural equality (consider only values, not column names) would be better.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bud/monkeypatch.rb', line 29

def <=>(o)
  if o.class == self.class
    self.each_with_index do |e, i|
      other = o[i]
      next if e == other
      return e <=> other
    end
    return 0
  elsif o.nil?
    return nil
  else
    raise "Comparison (<=>) between #{o.class} and #{self.class} not implemented"
  end
end

#==(o) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bud/monkeypatch.rb', line 44

def ==(o)
  if o.class == self.class
    return super
  elsif o.class == Array
    return false if self.length != o.length
    self.each_with_index do |el, i|
      return false if el != o[i]
    end
    return true
  end
  false
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/bud/monkeypatch.rb', line 61

def eql?(o)
  self == o
end

#hashObject



57
58
59
# File 'lib/bud/monkeypatch.rb', line 57

def hash
  self.values.hash
end

#inspectObject Also known as: to_s



73
74
75
# File 'lib/bud/monkeypatch.rb', line 73

def inspect
  self.to_a.inspect
end

#to_msgpack(out = nil) ⇒ Object



69
70
71
# File 'lib/bud/monkeypatch.rb', line 69

def to_msgpack(out=nil)
  self.to_a.to_msgpack(out)
end