Class: Literal::Data

Inherits:
Structish show all
Defined in:
lib/literal/data.rb

Constant Summary

Constants included from Attributable

Attributable::Visibility

Constants included from Monads

Monads::Either, Monads::Left, Monads::Maybe, Monads::Nothing, Monads::Result, Monads::Right, Monads::Some

Instance Attribute Summary

Attributes inherited from Structish

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Structish

#==, #[], #[]=, #deconstruct, #deconstruct_keys, #to_h

Methods included from Attributable

#attribute, #define_literal_methods, #literal_attributes, #literal_extension

Methods included from Types

#_Any, #_Array, #_Boolean, #_Callable, #_Class, #_Constraint, #_Descendant, #_Enumerable, #_Falsy, #_Float, #_Frozen, #_Hash, #_Integer, #_Interface, #_Intersection, #_Is, #_JSONData, #_Lambda, #_Map, #_Never, #_Nilable, #_Not, #_Procable, #_Range, #_Set, #_Shape, #_String, #_Symbol, #_Truthy, #_Tuple, #_Union, #_Void

Methods included from Monads

#Either, #Left, #Maybe, #Result, #Right, #Some, #Success

Class Method Details

.attribute(name, type, special = nil, reader: :public, positional: false, default: nil) ⇒ Object



5
6
7
# File 'lib/literal/data.rb', line 5

def attribute(name, type, special = nil, reader: :public, positional: false, default: nil)
	super(name, type, special, reader:, writer: false, positional:, default:)
end

.define_literal_methods(attribute) ⇒ Object (private)



11
12
13
14
15
16
17
18
19
# File 'lib/literal/data.rb', line 11

def define_literal_methods(attribute)
	literal_extension.module_eval <<~RUBY, __FILE__, __LINE__ + 1
		# frozen_string_literal: true

		#{generate_literal_initializer}

		#{generate_literal_reader(attribute) if attribute.reader?}
	RUBY
end

.generate_literal_initializerObject (private)



21
22
23
# File 'lib/literal/data.rb', line 21

def generate_literal_initializer
	Generators::DataInitializer.new(literal_attributes).call
end

.generate_literal_reader(attribute) ⇒ Object (private)



25
26
27
# File 'lib/literal/data.rb', line 25

def generate_literal_reader(attribute)
	Generators::StructReader.new(attribute).call
end

Instance Method Details

#marshal_dumpObject



67
68
69
# File 'lib/literal/data.rb', line 67

def marshal_dump
	[2, @attributes]
end

#marshal_load(data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/literal/data.rb', line 55

def marshal_load(data)
	case data
	when Hash # TODO: Remove this branch.
		@attributes = data[:attributes]
	when Array
		@attributes = data[1]
	end

	@literal_attributes = self.class.literal_attributes
	freeze
end

#with(**new_attributes) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/literal/data.rb', line 30

def with(**new_attributes)
	new_attributes.each do |name, value|
		if Literal::TYPE_CHECKS
			unless (type = @literal_attributes[name].type)
				raise Literal::ArgumentError, "Unknown attribute `#{name.inspect}`."
			end

			unless type === value
				raise Literal::TypeError.expected(value, to_be_a: attribute.type)
			end

			unless value.frozen?
				new_attributes[name] = value.dup.tap(&:freeze)
			end
		end

		new_attributes[name] = value.frozen? ? value : value.dup
	end

	copy = dup
	copy.instance_variable_set(:@attributes, @attributes.merge(new_attributes))
	copy.freeze
	copy
end