Class: Literal::Data
Constant Summary
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
#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
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_initializer ⇒ Object
.generate_literal_reader(attribute) ⇒ Object
Instance Method Details
#marshal_dump ⇒ Object
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 @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
|