Class: Dhall::Record

Inherits:
Expression show all
Includes:
Enumerable
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/normalize.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #annotate, #as_dhall, #cache_key, #call, #concat, #deep_merge_type, #dhall_eq, #digest, #fusion, #resolve, #to_binary, #to_cbor, #to_proc, #to_s, #|

Class Method Details

.decode(record) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/dhall/binary.rb', line 146

def self.decode(record)
	if record.empty?
		EmptyRecord.new
	else
		new(record: Hash[record.map { |k, v| [k, Dhall.decode(v)] }])
	end
end

.for(record) ⇒ Object



748
749
750
751
752
753
754
# File 'lib/dhall/ast.rb', line 748

def self.for(record)
	if record.empty?
		EmptyRecord.new
	else
		new(record: record)
	end
end

Instance Method Details

#==(other) ⇒ Object



822
823
824
# File 'lib/dhall/ast.rb', line 822

def ==(other)
	other.respond_to?(:record) && record.to_a == other.record.to_a
end

#[](k) ⇒ Object



773
774
775
# File 'lib/dhall/ast.rb', line 773

def [](k)
	record[k.to_s]
end

#as_jsonObject



834
835
836
# File 'lib/dhall/ast.rb', line 834

def as_json
	[8, Hash[record.to_a.map { |k, v| [k, v.as_json] }.sort]]
end

#deep_merge(other) ⇒ Object



802
803
804
805
806
807
808
809
# File 'lib/dhall/ast.rb', line 802

def deep_merge(other)
	other = other.as_dhall
	return super unless other.is_a?(Record)

	with(record: Hash[record.merge(other.record) { |_, v1, v2|
		v1.deep_merge(v2)
	}.sort])
end

#dig(*keys) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
# File 'lib/dhall/ast.rb', line 790

def dig(*keys)
	if keys.empty?
		raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
	end

	key, *rest = keys.map(&:to_s)
	v = record.fetch(key) { return nil }
	return v if rest.empty?

	v.dig(*rest)
end

#each(&block) ⇒ Object



756
757
758
759
# File 'lib/dhall/ast.rb', line 756

def each(&block)
	record.each(&block)
	self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


826
827
828
# File 'lib/dhall/ast.rb', line 826

def eql?(other)
	self == other
end

#fetch(k, default = nil, &block) ⇒ Object



777
778
779
# File 'lib/dhall/ast.rb', line 777

def fetch(k, default=nil, &block)
	record.fetch(k.to_s, *default, &block)
end

#keysObject



765
766
767
# File 'lib/dhall/ast.rb', line 765

def keys
	record.keys
end

#map(&block) ⇒ Object



818
819
820
# File 'lib/dhall/ast.rb', line 818

def map(&block)
	with(record: Hash[record.map(&block)])
end

#merge(other) ⇒ Object



811
812
813
814
815
816
# File 'lib/dhall/ast.rb', line 811

def merge(other)
	other = other.as_dhall
	return super unless other.is_a?(Record)

	with(record: Hash[record.merge(other.record).sort])
end

#normalizeObject



312
313
314
315
316
# File 'lib/dhall/normalize.rb', line 312

def normalize
	with(record: Hash[
		record.map { |k, v| [k, v.nil? ? v : v.normalize] }.sort
	])
end

#shift(amount, name, min_index) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/dhall/normalize.rb', line 318

def shift(amount, name, min_index)
	with(record: Hash[
		record.map { |k, v|
			[k, v.nil? ? v : v.shift(amount, name, min_index)]
		}.sort
	])
end

#slice(*keys) ⇒ Object



781
782
783
784
785
786
787
788
# File 'lib/dhall/ast.rb', line 781

def slice(*keys)
	keys = keys.map(&:to_s)
	if record.respond_to?(:slice)
		self.class.for(record.slice(*keys))
	else
		self.class.for(record.select { |k, _| keys.include?(k) })
	end
end

#substitute(var, with_expr) ⇒ Object



326
327
328
329
330
331
332
# File 'lib/dhall/normalize.rb', line 326

def substitute(var, with_expr)
	with(record: Hash[
		record.map { |k, v|
			[k, v.nil? ? v : v.substitute(var, with_expr)]
		}.sort
	])
end

#to_hObject



761
762
763
# File 'lib/dhall/ast.rb', line 761

def to_h
	record
end

#valuesObject



769
770
771
# File 'lib/dhall/ast.rb', line 769

def values
	record.values
end

#with(attrs) ⇒ Object



830
831
832
# File 'lib/dhall/ast.rb', line 830

def with(attrs)
	self.class.new({ record: record }.merge(attrs))
end