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

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

Class Method Details

.decode(record) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/dhall/binary.rb', line 126

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



638
639
640
641
642
643
644
# File 'lib/dhall/ast.rb', line 638

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

Instance Method Details

#==(other) ⇒ Object



712
713
714
# File 'lib/dhall/ast.rb', line 712

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

#[](k) ⇒ Object



663
664
665
# File 'lib/dhall/ast.rb', line 663

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

#as_jsonObject



724
725
726
# File 'lib/dhall/ast.rb', line 724

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

#deep_merge(other) ⇒ Object



692
693
694
695
696
697
698
699
# File 'lib/dhall/ast.rb', line 692

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



680
681
682
683
684
685
686
687
688
689
690
# File 'lib/dhall/ast.rb', line 680

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



646
647
648
649
# File 'lib/dhall/ast.rb', line 646

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


716
717
718
# File 'lib/dhall/ast.rb', line 716

def eql?(other)
	self == other
end

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



667
668
669
# File 'lib/dhall/ast.rb', line 667

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

#keysObject



655
656
657
# File 'lib/dhall/ast.rb', line 655

def keys
	record.keys
end

#map(&block) ⇒ Object



708
709
710
# File 'lib/dhall/ast.rb', line 708

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

#merge(other) ⇒ Object



701
702
703
704
705
706
# File 'lib/dhall/ast.rb', line 701

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

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

#normalizeObject



282
283
284
285
286
# File 'lib/dhall/normalize.rb', line 282

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

#shift(amount, name, min_index) ⇒ Object



288
289
290
291
292
293
294
# File 'lib/dhall/normalize.rb', line 288

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



671
672
673
674
675
676
677
678
# File 'lib/dhall/ast.rb', line 671

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



296
297
298
299
300
301
302
# File 'lib/dhall/normalize.rb', line 296

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



651
652
653
# File 'lib/dhall/ast.rb', line 651

def to_h
	record
end

#valuesObject



659
660
661
# File 'lib/dhall/ast.rb', line 659

def values
	record.values
end

#with(attrs) ⇒ Object



720
721
722
# File 'lib/dhall/ast.rb', line 720

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