Class: Thrift::Union

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift/union.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, value = nil) ⇒ Union

Returns a new instance of Union.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/thrift/union.rb', line 23

def initialize(name = nil, value = nil)
  if name
    if name.is_a? Hash
      if name.size > 1
        raise "#{self.class} cannot be instantiated with more than one field!"
      end

      name, value = name.keys.first, name.values.first
    end

    if Thrift.type_checking
      raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s)
    end

    if value.nil?
      raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!"
    end

    Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking
  elsif !value.nil?
    raise Exception, "Value provided, but no name!"
  end
  @setfield = name
  @value = value
end

Class Method Details

.field_accessor(klass, field_info) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/thrift/union.rb', line 105

def self.field_accessor(klass, field_info)
  klass.send :define_method, field_info[:name] do
    if field_info[:name].to_sym == @setfield
      @value
    else
      raise RuntimeError, "#{field_info[:name]} is not union's set field."
    end
  end

  klass.send :define_method, "#{field_info[:name]}=" do |value|
    Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
    @setfield = field_info[:name].to_sym
    @value = value
  end
end

.generate_accessors(klass) ⇒ Object



127
128
129
130
131
132
# File 'lib/thrift/union.rb', line 127

def self.generate_accessors(klass)
  klass::FIELDS.values.each do |field_info|
    field_accessor(klass, field_info)
    qmark_isset_method(klass, field_info)
  end
end

.qmark_isset_method(klass, field_info) ⇒ Object



121
122
123
124
125
# File 'lib/thrift/union.rb', line 121

def self.qmark_isset_method(klass, field_info)
  klass.send :define_method, "#{field_info[:name]}?" do
    get_set_field == field_info[:name].to_sym && !get_value.nil?
  end
end

Instance Method Details

#<=>(other) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/thrift/union.rb', line 146

def <=>(other)
  if self.class == other.class
    if get_set_field == other.get_set_field
      if get_set_field.nil?
        0
      else
        get_value <=> other.get_value
      end
    else
      if get_set_field && other.get_set_field.nil?
        -1
      elsif get_set_field.nil? && other.get_set_field
        1
      elsif get_set_field.nil? && other.get_set_field.nil?
        0
      else
        name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s)
      end
    end
  else
    self.class <=> other.class
  end
end

#==(other) ⇒ Object Also known as: eql?



96
97
98
# File 'lib/thrift/union.rb', line 96

def ==(other)
  other.equal?(self) || other.instance_of?(self.class) && @setfield == other.get_set_field && @value == other.get_value
end

#get_set_fieldObject

get the symbol that indicates what the currently set field type is.



135
136
137
# File 'lib/thrift/union.rb', line 135

def get_set_field
  @setfield
end

#get_valueObject

get the current value of this union, regardless of what the set field is. generally, you should only use this method when you don't know in advance what field to expect.



142
143
144
# File 'lib/thrift/union.rb', line 142

def get_value
  @value
end

#hashObject



101
102
103
# File 'lib/thrift/union.rb', line 101

def hash
  [self.class.name, @setfield, @value].hash
end

#inspectObject



49
50
51
52
53
54
55
# File 'lib/thrift/union.rb', line 49

def inspect
  if get_set_field
    "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
  else
    "<#{self.class} >"
  end
end

#read(iprot) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/thrift/union.rb', line 57

def read(iprot)
  iprot.read_struct_begin
  fname, ftype, fid = iprot.read_field_begin
  handle_message(iprot, fid, ftype)
  iprot.read_field_end

  fname, ftype, fid = iprot.read_field_begin
  unless (ftype == Types::STOP)
    raise ProtocolException.new(ProtocolException::INVALID_DATA, "Too many fields for union")
  end

  iprot.read_struct_end
  validate
end

#write(oprot) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/thrift/union.rb', line 72

def write(oprot)
  validate
  oprot.write_struct_begin(self.class.name)

  fid = self.name_to_id(@setfield.to_s)

  field_info = struct_fields[fid]
  unless field_info
    raise ProtocolException.new(ProtocolException::INVALID_DATA, "set_field is not valid for this union!")
  end

  type = field_info[:type]
  if is_container? type
    oprot.write_field_begin(@setfield, type, fid)
    write_container(oprot, @value, field_info)
    oprot.write_field_end
  else
    oprot.write_field(@setfield, type, fid, @value)
  end

  oprot.write_field_stop
  oprot.write_struct_end
end