Sha256: a6a7182718c51cf26345f91734de2b1bc1b52773a48f6b622d4aae57418a9780

Contents?: true

Size: 1.93 KB

Versions: 166

Compression:

Stored size: 1.93 KB

Contents

module TSV

  class CleanSerializer
    def self.dump(o); o end
    def self.load(o); o end
  end

  class IntegerSerializer
    def self.dump(i); [i].pack("l"); end
    def self.load(str); str.unpack("l").first; end
  end

  class FloatSerializer
    def self.dump(i); [i].pack("d"); end
    def self.load(str); str.unpack("d").first; end
  end

  class IntegerArraySerializer
    def self.dump(a); a.pack("l*"); end
    def self.load(str); str.unpack("l*"); end
  end

  class FloatArraySerializer
    def self.dump(a); a.pack("d*"); end
    def self.load(str); str.unpack("d*"); end
  end

  class StringSerializer
    def self.dump(str); str.to_s; end
    def self.load(str); str.dup; end
  end

  class StringArraySerializer
    def self.dump(array)
      array.collect{|a| a.to_s} * "\t"
    end

    def self.load(string)
      return [] if string.nil?
      string.split("\t", -1)
    end
  end

  class StringDoubleArraySerializer
    def self.dump(array)
      array.collect{|a| a.collect{|a| a.to_s} * "|"} * "\t"
    end

    def self.load(string)
      return [] if string.nil?
      string.split("\t", -1).collect{|l| l.split("|", -1)}
    end
  end

  class TSVMarshalSerializer
    def self.dump(tsv)
      Marshal.dump(tsv.dup)
    end

    def self.load(string)
      TSV.setup Marshal.load(string)
    end
  end

  class TSVSerializer
    def self.dump(tsv)
      tsv.to_s
    end

    def self.load(string)
      TSV.open StringIO.new(string)
    end
  end

  SERIALIZER_ALIAS = {
    :integer => IntegerSerializer, 
    :float => FloatSerializer, 
    :integer_array => IntegerArraySerializer,
    :float_array => FloatArraySerializer,
    :marshal => Marshal,
    :single => StringSerializer,
    :string => StringSerializer,
    :list => StringArraySerializer,
    :flat => StringArraySerializer,
    :double => StringDoubleArraySerializer,
    :clean => CleanSerializer,
    :tsv => TSVSerializer,
    :marshal_tsv => TSVMarshalSerializer
  }

end

Version data entries

166 entries across 166 versions & 1 rubygems

Version Path
rbbt-util-5.17.63 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.62 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.61 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.60 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.59 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.58 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.57 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.56 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.55 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.54 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.53 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.52 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.51 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.50 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.49 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.48 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.47 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.46 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.45 lib/rbbt/tsv/serializers.rb
rbbt-util-5.17.44 lib/rbbt/tsv/serializers.rb