Class: Dhall::Optional

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

Direct Known Subclasses

OptionalNone

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #annotate, #as_dhall, #cache_key, #call, #concat, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #resolve, #shift, #slice, #substitute, #to_binary, #to_cbor, #to_proc, #|

Constructor Details

#initialize(attrs) ⇒ Optional

Returns a new instance of Optional.



584
585
586
587
# File 'lib/dhall/ast.rb', line 584

def initialize(attrs)
	@normalized = attrs.delete(:normalized) || false
	super(attrs)
end

Class Method Details

.as_dhallObject



580
581
582
# File 'lib/dhall/ast.rb', line 580

def self.as_dhall
	Builtins[:Natural]
end

.decode(type, value = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/dhall/binary.rb', line 104

def self.decode(type, value=nil)
	if value.nil?
		OptionalNone.new(value_type: Dhall.decode(type))
	else
		Optional.new(
			value:      Dhall.decode(value),
			value_type: type.nil? ? type : Dhall.decode(type)
		)
	end
end

.for(value, type: nil) ⇒ Object



572
573
574
575
576
577
578
# File 'lib/dhall/ast.rb', line 572

def self.for(value, type: nil)
	if value.nil?
		OptionalNone.new(value_type: type)
	else
		Optional.new(value: value, value_type: type)
	end
end

Instance Method Details

#as_jsonObject



610
611
612
# File 'lib/dhall/ast.rb', line 610

def as_json
	[5, @normalized ? nil : value_type&.as_json, value.as_json]
end

#map(type: nil, &block) ⇒ Object



598
599
600
# File 'lib/dhall/ast.rb', line 598

def map(type: nil, &block)
	with(value: block[value], value_type: type)
end

#normalizeObject



269
270
271
272
273
274
275
# File 'lib/dhall/normalize.rb', line 269

def normalize
	with(
		value:      value.normalize,
		value_type: value_type&.normalize,
		normalized: true
	)
end

#reduce(_, &block) ⇒ Object



602
603
604
# File 'lib/dhall/ast.rb', line 602

def reduce(_, &block)
	block[value]
end

#to_sObject



606
607
608
# File 'lib/dhall/ast.rb', line 606

def to_s
	value.to_s
end

#typeObject



589
590
591
592
593
594
595
596
# File 'lib/dhall/ast.rb', line 589

def type
	return unless value_type

	Dhall::Application.new(
		function: Builtins[:Optional],
		argument: value_type
	)
end