File: coder.rb

package info (click to toggle)
ruby-pg 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,316 kB
  • sloc: ansic: 9,403; ruby: 3,160; makefile: 10
file content (108 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- ruby -*-
# frozen_string_literal: true

module PG

	class Coder

		module BinaryFormatting
			def initialize(hash={}, **kwargs)
				warn("PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}", category: :deprecated) unless hash.empty?
				super(format: 1, **hash, **kwargs)
			end
		end


		# Create a new coder object based on the attribute Hash.
		def initialize(hash=nil, **kwargs)
			warn("PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}", category: :deprecated) if hash

			(hash || kwargs).each do |key, val|
				send("#{key}=", val)
			end
		end

		def dup
			self.class.new(**to_h)
		end

		# Returns coder attributes as Hash.
		def to_h
			{
				oid: oid,
				format: format,
				flags: flags,
				name: name,
			}
		end

		def ==(v)
			self.class == v.class && to_h == v.to_h
		end

		def marshal_dump
			Marshal.dump(to_h)
		end

		def marshal_load(str)
			initialize(**Marshal.load(str))
		end

		def inspect
			str = self.to_s
			oid_str = " oid=#{oid}" unless oid==0
			format_str = " format=#{format}" unless format==0
			name_str = " #{name.inspect}" if name
			str[-1,0] = "#{name_str} #{oid_str}#{format_str}"
			str
		end

		def inspect_short
			str = case format
				when 0 then "T"
				when 1 then "B"
				else format.to_s
			end
			str += "E" if respond_to?(:encode)
			str += "D" if respond_to?(:decode)

			"#{name || self.class.name}:#{str}"
		end
	end

	class CompositeCoder < Coder
		def to_h
			h = { **super,
				elements_type: elements_type,
				needs_quotation: needs_quotation?,
				delimiter: delimiter,
			}
			h[:dimensions] = dimensions if dimensions # Write only when set, for Marshal compat with pg<1.6
			h
		end

		def inspect
			str = super
			str[-1,0] = " elements_type=#{elements_type.inspect} #{needs_quotation? ? 'needs' : 'no'} quotation#{dimensions && " #{dimensions} dimensions"}"
			str
		end
	end

	class CopyCoder < Coder
		def to_h
			{ **super,
				type_map: type_map,
				delimiter: delimiter,
				null_string: null_string,
			}
		end
	end

	class RecordCoder < Coder
		def to_h
			{ **super,
				type_map: type_map,
			}
		end
	end
end # module PG