Thrift::Struct

Public Class Methods

field_accessor(klass, field_info) click to toggle source
# File lib/thrift/struct.rb, line 153
def self.field_accessor(klass, field_info)
  field_name_sym = field_info[:name].to_sym
  klass.send :attr_reader, field_name_sym
  klass.send :define_method, "#{field_info[:name]}=" do |value|
    Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
    instance_variable_set("@#{field_name_sym}", value)
  end
end
generate_accessors(klass) click to toggle source
# File lib/thrift/struct.rb, line 162
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
new(d={}, &block) click to toggle source
# File lib/thrift/struct.rb, line 24
def initialize(d={}, &block)
  # get a copy of the default values to work on, removing defaults in favor of arguments
  fields_with_defaults = fields_with_default_values.dup
  
  # check if the defaults is empty, or if there are no parameters for this 
  # instantiation, and if so, don't bother overriding defaults.
  unless fields_with_defaults.empty? || d.empty?
    d.each_key do |name|
      fields_with_defaults.delete(name.to_s)
    end
  end
  
  # assign all the user-specified arguments
  unless d.empty?
    d.each do |name, value|
      unless name_to_id(name.to_s)
        raise Exception, "Unknown key given to #{self.class}.new: #{name}"
      end
      Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking
      instance_variable_set("@#{name}", value)
    end
  end
  
  # assign all the default values
  unless fields_with_defaults.empty?
    fields_with_defaults.each do |name, default_value|
      instance_variable_set("@#{name}", (default_value.dup rescue default_value))
    end
  end
  
  yield self if block_given?
end
qmark_isset_method(klass, field_info) click to toggle source
# File lib/thrift/struct.rb, line 169
def self.qmark_isset_method(klass, field_info)
  klass.send :define_method, "#{field_info[:name]}?" do
    !self.send(field_info[:name].to_sym).nil?
  end
end

Protected Class Methods

append_features(mod) click to toggle source
# File lib/thrift/struct.rb, line 201
def self.append_features(mod)
  if mod.ancestors.include? ::Exception
    mod.send :class_variable_set, :'@@__thrift_struct_real_initialize', mod.instance_method(:initialize)
    super
    # set up our custom initializer so `raise Xception, 'message'` works
    mod.send :define_method, :struct_initialize, mod.instance_method(:initialize)
    mod.send :define_method, :initialize, mod.instance_method(:exception_initialize)
  else
    super
  end
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/thrift/struct.rb, line 175
def <=>(other)
  if self.class == other.class
    each_field do |fid, field_info|
      v1 = self.send(field_info[:name])
      v1_set = !v1.nil?
      v2 = other.send(field_info[:name])
      v2_set = !v2.nil?
      if v1_set && !v2_set
        return -1
      elsif !v1_set && v2_set
        return 1
      elsif v1_set && v2_set
        cmp = v1 <=> v2
        if cmp != 0
          return cmp
        end
      end
    end
    0
  else
    self.class <=> other.class
  end
end
==(other) click to toggle source
# File lib/thrift/struct.rb, line 116
def ==(other)
  return false if other.nil?
  each_field do |fid, field_info|
    name = field_info[:name]
    return false unless other.respond_to?(name) && self.send(name) == other.send(name)
  end
  true
end
differences(other) click to toggle source
# File lib/thrift/struct.rb, line 140
def differences(other)
  diffs = []
  unless other.is_a?(self.class)
    diffs << "Different class!"
  else
    each_field do |fid, field_info|
      name = field_info[:name]
      diffs << "#{name} differs!" unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
    end
  end
  diffs
end
eql?(other) click to toggle source
# File lib/thrift/struct.rb, line 125
def eql?(other)
  self.class == other.class && self == other
end
fields_with_default_values() click to toggle source
# File lib/thrift/struct.rb, line 57
def fields_with_default_values
  fields_with_default_values = self.class.instance_variable_get(:@fields_with_default_values)
  unless fields_with_default_values
    fields_with_default_values = {}
    struct_fields.each do |fid, field_def|
      unless field_def[:default].nil?
        fields_with_default_values[field_def[:name]] = field_def[:default]
      end
    end
    self.class.instance_variable_set(:@fields_with_default_values, fields_with_default_values)
  end
  fields_with_default_values
end
hash() click to toggle source

This implementation of hash() is inspired by Apache's Java HashCodeBuilder class.

# File lib/thrift/struct.rb, line 130
def hash
  total = 17
  each_field do |fid, field_info|
    name = field_info[:name]
    value = self.send(name)
    total = (total * 37 + value.hash) & 0xffffffff
  end
  total
end
inspect(skip_optional_nulls = true) click to toggle source
# File lib/thrift/struct.rb, line 71
def inspect(skip_optional_nulls = true)
  fields = []
  each_field do |fid, field_info|
    name = field_info[:name]
    value = instance_variable_get("@#{name}")
    unless skip_optional_nulls && field_info[:optional] && value.nil?
      fields << "#{name}:#{inspect_field(value, field_info)}"
    end
  end
  "<#{self.class} #{fields.join(", ")}>"
end
read(iprot) click to toggle source
# File lib/thrift/struct.rb, line 83
def read(iprot)
  iprot.read_struct_begin
  loop do
    fname, ftype, fid = iprot.read_field_begin
    break if (ftype == Types::STOP)
    handle_message(iprot, fid, ftype)
    iprot.read_field_end
  end
  iprot.read_struct_end
  validate
end
write(oprot) click to toggle source
# File lib/thrift/struct.rb, line 95
def write(oprot)
  validate
  oprot.write_struct_begin(self.class.name)
  each_field do |fid, field_info|
    name = field_info[:name]
    type = field_info[:type]
    value = instance_variable_get("@#{name}")
    unless value.nil?
      if is_container? type
        oprot.write_field_begin(name, type, fid)
        write_container(oprot, value, field_info)
        oprot.write_field_end
      else
        oprot.write_field(name, type, fid, value)
      end
    end
  end
  oprot.write_field_stop
  oprot.write_struct_end
end

Protected Instance Methods

exception_initialize(*args, &block) click to toggle source
# File lib/thrift/struct.rb, line 213
def exception_initialize(*args, &block)
  if args.size == 1 and args.first.is_a? Hash
    # looks like it's a regular Struct initialize
    method(:struct_initialize).call(args.first)
  else
    # call the Struct initializer first with no args
    # this will set our field default values
    method(:struct_initialize).call()
    # now give it to the exception
    self.class.send(:class_variable_get, :'@@__thrift_struct_real_initialize').bind(self).call(*args, &block) if args.size > 0
    # self.class.instance_method(:initialize).bind(self).call(*args, &block)
  end
end
handle_message(iprot, fid, ftype) click to toggle source
# File lib/thrift/struct.rb, line 227
def handle_message(iprot, fid, ftype)
  field = struct_fields[fid]
  if field and field[:type] == ftype
    value = read_field(iprot, field)
    instance_variable_set("@#{field[:name]}", value)
  else
    iprot.skip(ftype)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.