Module: Dhall::Util

Defined in:
lib/dhall/util.rb

Defined Under Namespace

Modules: ArrayAllTheSame Classes: AllOf, ArrayOf, Deadline, HashOf, LazyPromise, NoDeadline, Not

Class Method Summary collapse

Class Method Details

.indent_size(str) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/dhall/util.rb', line 203

def self.indent_size(str)
	if str.end_with?("\n")
		0
	else
		str
			.scan(/^[ \t]*(?=[^ \t\n]|\Z)/)
			.map(&:chars)
			.reduce(&method(:longest_common_prefix))&.length.to_i
	end
end

.longest_common_prefix(a, b) ⇒ Object



199
200
201
# File 'lib/dhall/util.rb', line 199

def self.longest_common_prefix(a, b)
	a.zip(b).take_while { |(x, y)| x == y }.map(&:first)
end

.match_result_promises(xs = nil, ys = nil) ⇒ Object



130
131
132
133
134
# File 'lib/dhall/util.rb', line 130

def self.match_result_promises(xs=nil, ys=nil)
	match_results(yield(Array(xs)), ys) do |promise, promises|
		promises.each { |p| p.fulfill(promise) }
	end
end

.match_results(xs = nil, ys = nil) ⇒ Object



124
125
126
127
128
# File 'lib/dhall/util.rb', line 124

def self.match_results(xs=nil, ys=nil)
	Array(xs).each_with_index.map do |r, idx|
		yield r, ys[idx]
	end
end

.net_http_req_with_timeout(uri, req, timeout:) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/dhall/util.rb', line 222

def self.net_http_req_with_timeout(uri, req, timeout:)
	Net::HTTP.start(
		uri.hostname,
		uri.port,
		use_ssl:       uri.scheme == "https",
		open_timeout:  timeout,
		ssl_timeout:   timeout,
		read_timeout:  timeout,
		write_timeout: timeout
	) { |http| http.request(req) }
end

.path_components_to_uri(*components) ⇒ Object



214
215
216
# File 'lib/dhall/util.rb', line 214

def self.path_components_to_uri(*components)
	URI("/#{components.map(&method(:uri_escape)).join("/")}")
end

.promise_all_hash(hash) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dhall/util.rb', line 136

def self.promise_all_hash(hash)
	keys, promises = hash.to_a.transpose

	return Promise.resolve(hash) unless keys

	Promise.all(promises).then do |values|
		Hash[Util.match_results(keys, values) do |k, v|
			[k, v]
		end]
	end
end

.psych_coder_for(tag, v) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dhall/util.rb', line 148

def self.psych_coder_for(tag, v)
	c = Psych::Coder.new(tag)
	case v
	when Hash
		c.map = v
	when Array
		c.seq = v
	else
		c.scalar = v
	end
end

.psych_coder_from(tag, o) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dhall/util.rb', line 160

def self.psych_coder_from(tag, o)
	coder = Psych::Coder.new(tag)

	if o.respond_to?(:encode_with)
		o.encode_with(coder)
	else
		o.instance_variables.each do |ivar|
			coder[ivar.to_s[1..-1]] = o.instance_variable_get(ivar)
		end
	end

	coder
end

.text_or_binary(str) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dhall/util.rb', line 185

def self.text_or_binary(str)
	unless str.valid_encoding?
		raise ArgumentError, "invalid byte sequence in #{str.encoding}"
	end

	if str.encoding == Encoding::BINARY
		return str if str =~ /(?!\s)[[:cntrl:]]/

		utf8_if_possible(str)
	else
		str.encode(Encoding::UTF_8)
	end
end

.transform_keys(hash_or_not) ⇒ Object



174
175
176
177
178
# File 'lib/dhall/util.rb', line 174

def self.transform_keys(hash_or_not)
	return hash_or_not unless hash_or_not.is_a?(Hash)

	Hash[hash_or_not.map { |k, v| [(yield k), v] }]
end

.uri_escape(s) ⇒ Object



218
219
220
# File 'lib/dhall/util.rb', line 218

def self.uri_escape(s)
	::URI.encode_www_form_component(s).gsub("+", "%20")
end

.utf8_if_possible(str) ⇒ Object



180
181
182
183
# File 'lib/dhall/util.rb', line 180

def self.utf8_if_possible(str)
	utf8 = str.dup.force_encoding(Encoding::UTF_8)
	utf8.valid_encoding? ? utf8 : str
end