Class: Literal::Properties::Schema Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/literal/properties/schema.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

DataSchema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties_index: {}, sorted_properties: []) ⇒ Schema

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Schema.



7
8
9
10
11
# File 'lib/literal/properties/schema.rb', line 7

def initialize(properties_index: {}, sorted_properties: [])
	@properties_index = properties_index
	@sorted_properties = sorted_properties
	@mutex = Mutex.new
end

Instance Attribute Details

#properties_indexObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/literal/properties/schema.rb', line 13

def properties_index
  @properties_index
end

Instance Method Details

#<<(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
22
23
24
25
26
27
28
# File 'lib/literal/properties/schema.rb', line 19

def <<(value)
	@mutex.synchronize do
		@properties_index[value.name] = value
		# ruby's sort is unstable, this trick makes it stable
		n = 0
		@sorted_properties = @properties_index.values.sort_by! { |it| n += 1; [it, n] }
	end

	self
end

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/literal/properties/schema.rb', line 15

def [](key)
	@properties_index[key]
end

#dupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
33
34
35
# File 'lib/literal/properties/schema.rb', line 30

def dup
	self.class.new(
		properties_index: @properties_index.dup,
		sorted_properties: @sorted_properties.dup,
	)
end

#eachObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
# File 'lib/literal/properties/schema.rb', line 37

def each(&)
	@sorted_properties.each(&)
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


45
46
47
# File 'lib/literal/properties/schema.rb', line 45

def empty?
	@sorted_properties.empty?
end

#generate_after_initializer(buffer = +"")) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
# File 'lib/literal/properties/schema.rb', line 64

def generate_after_initializer(buffer = +"")
	buffer << "  after_initialize if respond_to?(:after_initialize, true)\n"
end

#generate_eq(buffer = +"")) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/literal/properties/schema.rb', line 98

def generate_eq(buffer = +"")
	buffer << "def ==(other)\n"
	buffer << "  return false unless self.class === other && other.class.literal_properties.size == self.class.literal_properties.size\n"

	sorted_properties = @sorted_properties
	i, n = 0, sorted_properties.size
	while i < n
		property = sorted_properties[i]
		buffer << "  @#{property.name.name} == other.instance_variable_get(:@#{property.name.name})"
		buffer << " &&\n  " if i < n - 1
		i += 1
	end
	buffer << "  true" if n.zero?
	buffer << "\nend\n"
	buffer << "alias eql? ==\n"
end

#generate_hash(buffer = +"")) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/literal/properties/schema.rb', line 84

def generate_hash(buffer = +"")
	buffer << "def hash\n  [self.class,\n"

	sorted_properties = @sorted_properties
	i, n = 0, sorted_properties.size
	while i < n
		property = sorted_properties[i]
		buffer << "  @" << property.name.name << ",\n"
		i += 1
	end

	buffer << "  ].hash\n" << "end\n"
end

#generate_initializer(buffer = +"")) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/literal/properties/schema.rb', line 49

def generate_initializer(buffer = +"")
	buffer << "alias initialize initialize\n" \
		"def initialize("
	generate_initializer_params(buffer)
	buffer << ")\n"
	generate_initializer_body(buffer)
	buffer << "" \
		"rescue Literal::TypeError => error\n" \
		"  error.set_backtrace(caller(2))\n" \
		"  raise\n" \
		"else\n"
	generate_after_initializer(buffer)
	buffer << "end\n"
end

#generate_to_h(buffer = +"")) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/literal/properties/schema.rb', line 68

def generate_to_h(buffer = +"")
	buffer << "alias to_h to_h\n"
	buffer << "def to_h\n" << "  {\n"

	sorted_properties = @sorted_properties
	i, n = 0, sorted_properties.size
	while i < n
		property = sorted_properties[i]
		buffer << "    " << property.name.name << ": @" << property.name.name << ",\n"
		i += 1
	end

	buffer << "  }\n" << "end\n"
	buffer << "alias to_hash to_h\n"
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
# File 'lib/literal/properties/schema.rb', line 41

def size
	@sorted_properties.size
end