Class: PropInitializer::Properties::Schema Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/prop_initializer/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.

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/prop_initializer/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/prop_initializer/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/prop_initializer/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/prop_initializer/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/prop_initializer/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/prop_initializer/properties/schema.rb', line 37

def each(&)
	@sorted_properties.each(&)
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.



45
46
47
48
49
50
51
# File 'lib/prop_initializer/properties/schema.rb', line 45

def generate_initializer(buffer = +"")
	buffer << "def initialize(#{generate_initializer_params})\n"
	generate_initializer_body(buffer)
	buffer << "" \
		"  after_initialize if respond_to?(:after_initialize)\n" \
		"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.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/prop_initializer/properties/schema.rb', line 53

def generate_to_h(buffer = +"")
	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"
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/prop_initializer/properties/schema.rb', line 41

def size
	@sorted_properties.size
end