Module: Mmi::PropertyAttributes

Included in:
Asset, AssetsProcessor, ModFileProcessor, Modloader::Fabric, Source::Github, Source::Modrinth, Source::Url
Defined in:
lib/mmi/property_attributes.rb

Defined Under Namespace

Modules: ClassMethods Classes: ValidationError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object



59
60
61
# File 'lib/mmi/property_attributes.rb', line 59

def self.prepended(klass)
	klass.extend(ClassMethods)
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
# File 'lib/mmi/property_attributes.rb', line 11

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

#initialize(hash) ⇒ Object



3
4
5
# File 'lib/mmi/property_attributes.rb', line 3

def initialize(hash)
	@hash = hash
end

#parse!Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mmi/property_attributes.rb', line 120

def parse!
	validate_constraints!
	
	self.class.registered_properties.each do |method_name, configuration|
		if !@hash.key?(configuration.key)
			if configuration.required || !configuration.default.nil?
				parsed_property_store[method_name] = configuration.default
			else
				# Do nothing.
			end
		elsif configuration.type.nil?
			parsed_property_store[method_name] = self[configuration.key]
		else
			deduced_type =
				if self.class.valid_hash_type?(configuration.type)
					configuration.type[:types].fetch(self[configuration.key][configuration.type[:field]])
				else
					configuration.type
				end
			
			initializer_proc = proc do |item|
				deduced_type.parse(item)
			end
			
			parsed_property_store[method_name] =
				self[configuration.key].then do |value|
					value.is_a?(Array) ? value.map(&initializer_proc) : initializer_proc[value]
				end
		end
	end
	
	self
end

#parsed_property_storeObject



63
64
65
# File 'lib/mmi/property_attributes.rb', line 63

def parsed_property_store
	@parsed_property_store ||= {}
end

#to_hObject



7
8
9
# File 'lib/mmi/property_attributes.rb', line 7

def to_h
	@hash
end

#update_properties!(properties) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mmi/property_attributes.rb', line 154

def update_properties!(properties)
	raise(ArgumentError, 'argument must be a Hash'                                ) if !properties.is_a?(Hash)
	raise(ArgumentError, 'argument can only have keys that are defined properties') if (properties.keys - self.class.registered_properties.keys).any?
	
	old_property_store     = @parsed_property_store
	@parsed_property_store = nil
	
	old_properties = properties.map do |method_name, value|
		key = self.class.registered_properties[method_name].key
		
		old_value = @hash[key]
		
		if !value.nil?
			@hash[key] = value
		elsif @hash.key?(key)
			@hash.delete(key)
		end
		
		[method_name, old_value]
	end
	
	parse!
rescue ValidationError
	@parsed_property_store = old_property_store
	
	old_properties.each do |method_name, old_value|
		key = self.class.registered_properties[method_name].key
		
		if !old_value.nil?
			@hash[key] = old_value
		elsif @hash.key?(key)
			@hash.delete(key)
		end
	end
	
	raise
end

#validate_constraints!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mmi/property_attributes.rb', line 69

def validate_constraints!
	errors =
		self.class.registered_properties.map do |method_name, configuration|
			[
				method_name,
				[].tap do |property_errors|
					if configuration.required && !@hash.key?(configuration.key)
						property_errors << "missing field #{configuration.key}"
					elsif !configuration.required && @hash.key?(configuration.key)
						if (missing_requires = configuration.requires.reject{ |e| @hash.key?(e) }).any?
							property_errors << "missing required field(s) #{missing_requires.map(&:inspect).join(', ')}"
						end
						
						if (present_conflicts = configuration.conflicts.select{ |e| @hash.key?(e) }).any?
							property_errors << "conflicting with field(s) #{present_conflicts.map(&:inspect).join(', ')}"
						end
					end
					
					if @hash.key?(configuration.key) && self.class.valid_hash_type?(configuration.type)
						if !@hash.fetch(configuration.key).is_a?(Hash)
							property_errors << "field #{configuration.key.inspect} must be a Hash"
						elsif !@hash.fetch(configuration.key).key?(configuration.type[:field]) || !configuration.type[:types].keys.include?(@hash.fetch(configuration.key).fetch(configuration.type[:field]))
							property_errors << "field #{configuration.key.inspect} must have key #{configuration.type[:field].inspect} with one of values #{configuration.type[:types].keys.map(&:inspect).join(', ')}"
						end
					end
					
					if @hash.key?(configuration.key) && configuration.validate
						deduced_proc =
							if configuration.validate.is_a?(Symbol)
								self.class.method(configuration.validate)
							else
								configuration.validate
							end
						
						deduced_proc.call(@hash[configuration.key], property_errors)
					end
				end,
			]
		end.select do |_, property_errors|
			property_errors.any?
		end.to_h
	
	if errors.none?
		true
	else
		raise(ValidationError, errors.map do |method_name, property_errors|
			"#{method_name}: #{property_errors.join(', ')}"
		end.join('; '))
	end
end