Class: Icarus::Mod::Tools::Baseinfo
- Inherits:
-
Object
- Object
- Icarus::Mod::Tools::Baseinfo
show all
- Defined in:
- lib/icarus/mod/tools/baseinfo.rb
Overview
Base class for Modinfo and Toolinfo
Constant Summary
collapse
- HASHKEYS =
%i[name author version compatibility description files imageURL readmeURL].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data, id: nil, created: nil, updated: nil) ⇒ Baseinfo
Returns a new instance of Baseinfo.
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 12
def initialize(data, id: nil, created: nil, updated: nil)
@id = id
@created_at = created
@updated_at = updated
@errors = []
@warnings = []
@validated = false
read(data)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args, &_block) ⇒ Object
104
105
106
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 104
def method_missing(method_name, *_args, &_block)
@data[method_name.to_sym] if @data.key?(method_name.to_sym)
end
|
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
8
9
10
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8
def created_at
@created_at
end
|
#data ⇒ Object
Returns the value of attribute data.
8
9
10
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8
def data
@data
end
|
#id ⇒ Object
Returns the value of attribute id.
8
9
10
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8
def id
@id
end
|
#updated_at ⇒ Object
Returns the value of attribute updated_at.
8
9
10
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8
def updated_at
@updated_at
end
|
Instance Method Details
#author_id ⇒ Object
23
24
25
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 23
def author_id
author.downcase.gsub(/\s+/, "_")
end
|
#errors ⇒ Object
31
32
33
34
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 31
def errors
validate
@errors.compact.uniq
end
|
#errors? ⇒ Boolean
36
37
38
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 36
def errors?
errors.any?
end
|
#file_types ⇒ Object
96
97
98
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 96
def file_types
files&.keys || []
end
|
#file_urls ⇒ Object
100
101
102
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 100
def file_urls
files&.values || []
end
|
#read(data) ⇒ Object
27
28
29
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 27
def read(data)
@data = data.is_a?(String) ? JSON.parse(data, symbolize_names: true) : data
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
108
109
110
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 108
def respond_to_missing?(method_name, include_private = false)
@data.key?(method_name.to_sym) || super
end
|
#status ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 49
def status
validate
{
errors:,
warnings:
}
end
|
#to_h ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 66
def to_h
db_hash = HASHKEYS.each_with_object({}) { |key, hash| hash[key] = @data[key] }
db_hash[:version] = "1.0" if version.nil?
db_hash
end
|
#to_json(*args) ⇒ Object
62
63
64
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 62
def to_json(*args)
JSON.generate(@data, *args)
end
|
#uniq_name ⇒ Object
58
59
60
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 58
def uniq_name
"#{author.strip}/#{name.strip}"
end
|
#valid? ⇒ Boolean
90
91
92
93
94
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 90
def valid?
validate
!errors?
end
|
#validate ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 74
def validate
return true if @validated
validate_version
%w[name author description].each do |key|
@errors << "#{key.capitalize} cannot be blank" unless validate_string(@data[key.to_sym])
end
%w[imageURL readmeURL].each do |key|
@errors << "Invalid URL #{key.capitalize}: #{@data[key.to_sym] || "blank"}" unless validate_url(@data[key.to_sym])
end
@validated = true
end
|
#warnings ⇒ Object
40
41
42
43
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 40
def warnings
validate
@warnings.compact.uniq
end
|
#warnings? ⇒ Boolean
45
46
47
|
# File 'lib/icarus/mod/tools/baseinfo.rb', line 45
def warnings?
warnings.any?
end
|