Class: Mangadex::Internal::Definitions::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mangadex/internal/definitions/base.rb

Direct Known Subclasses

ContentRating, Year

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, key:, accepts:, required: false, converts: nil) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
# File 'lib/mangadex/internal/definitions/base.rb', line 9

def initialize(value, key:, accepts:, required: false, converts: nil)
  @value = value
  @key = key
  @accepts = accepts
  @required = required
  @converts = converts
  @errors = Array.new
end

Instance Attribute Details

#acceptsObject (readonly)

Returns the value of attribute accepts.



7
8
9
# File 'lib/mangadex/internal/definitions/base.rb', line 7

def accepts
  @accepts
end

#convertsObject (readonly)

Returns the value of attribute converts.



7
8
9
# File 'lib/mangadex/internal/definitions/base.rb', line 7

def converts
  @converts
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/mangadex/internal/definitions/base.rb', line 7

def errors
  @errors
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/mangadex/internal/definitions/base.rb', line 7

def key
  @key
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mangadex/internal/definitions/base.rb', line 46

def empty?
  converted_value.respond_to?(:empty?) ? converted_value.empty? : converted_value.to_s.strip.empty?
end

#error_messageObject



40
41
42
43
44
# File 'lib/mangadex/internal/definitions/base.rb', line 40

def error_message
  return unless errors.any?

  compile_error_message
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/mangadex/internal/definitions/base.rb', line 33

def valid?
  validate!
  true
rescue ArgumentError
  false
end

#validateObject



18
19
20
21
22
23
24
25
# File 'lib/mangadex/internal/definitions/base.rb', line 18

def validate
  validate_required
  return if !@required && empty?

  validate_accepts

  nil
end

#validate!Object



27
28
29
30
31
# File 'lib/mangadex/internal/definitions/base.rb', line 27

def validate!
  validate

  raise_if_any_errors!
end

#valueObject



50
51
52
# File 'lib/mangadex/internal/definitions/base.rb', line 50

def value
  converted_value
end