Class: Icarus::Mod::Tools::Baseinfo

Inherits:
Object
  • Object
show all
Defined in:
lib/icarus/mod/tools/baseinfo.rb

Overview

Base class for Modinfo and Toolinfo

Direct Known Subclasses

Modinfo, 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_atObject (readonly)

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

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8

def id
  @id
end

#updated_atObject (readonly)

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_idObject



23
24
25
# File 'lib/icarus/mod/tools/baseinfo.rb', line 23

def author_id
  author.downcase.gsub(/\s+/, "_")
end

#errorsObject



31
32
33
34
# File 'lib/icarus/mod/tools/baseinfo.rb', line 31

def errors
  validate
  @errors.compact.uniq
end

#errors?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/icarus/mod/tools/baseinfo.rb', line 36

def errors?
  errors.any?
end

#file_typesObject



96
97
98
# File 'lib/icarus/mod/tools/baseinfo.rb', line 96

def file_types
  files&.keys || []
end

#file_urlsObject



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

Returns:

  • (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

#statusObject



49
50
51
52
53
54
55
56
# File 'lib/icarus/mod/tools/baseinfo.rb', line 49

def status
  validate

  {
    errors:,
    warnings:
  }
end

#to_hObject



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_nameObject



58
59
60
# File 'lib/icarus/mod/tools/baseinfo.rb', line 58

def uniq_name
  "#{author.strip}/#{name.strip}"
end

#valid?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/icarus/mod/tools/baseinfo.rb', line 90

def valid?
  validate

  !errors?
end

#validateObject



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

#warningsObject



40
41
42
43
# File 'lib/icarus/mod/tools/baseinfo.rb', line 40

def warnings
  validate
  @warnings.compact.uniq
end

#warnings?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/icarus/mod/tools/baseinfo.rb', line 45

def warnings?
  warnings.any?
end