Module: MediaTypes::Dsl::ClassMethods

Defined in:
lib/media_types/dsl.rb

Instance Method Summary collapse

Instance Method Details

#available_validationsObject



79
80
81
82
83
84
# File 'lib/media_types/dsl.rb', line 79

def available_validations
  self.media_type_combinations.map do |a|
    _, view, version = a
    view(view).version(version)
  end
end

#identifierObject



75
76
77
# File 'lib/media_types/dsl.rb', line 75

def identifier
  to_constructable.to_s
end

#identifier_formatObject



69
70
71
72
73
# File 'lib/media_types/dsl.rb', line 69

def identifier_format
  self.media_type_name_for = Proc.new do |type:, view:, version:, suffix:|
    yield(type: type, view: view, version: version, suffix: suffix)
  end
end

#registerObject



55
56
57
58
59
60
# File 'lib/media_types/dsl.rb', line 55

def register
  registrations.to_a.map do |registerable|
    MediaTypes.register(registerable)
    registerable
  end
end

#schema_for(constructable) ⇒ Object



86
87
88
# File 'lib/media_types/dsl.rb', line 86

def schema_for(constructable)
  validations.find(constructable)
end

#to_constructableObject



25
26
27
28
29
# File 'lib/media_types/dsl.rb', line 25

def to_constructable
  media_type_constructable.dup.tap do |constructable|
    constructable.__setobj__(self)
  end
end

#valid?(output, **opts) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/media_types/dsl.rb', line 31

def valid?(output, **opts)
  to_constructable.valid?(output, **opts)
end

#valid_unsafe?(output, media_type = to_constructable, **opts) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/media_types/dsl.rb', line 35

def valid_unsafe?(output, media_type = to_constructable, **opts)
  validations.find(media_type).valid?(output, backtrace: ['.'], **opts)
end

#validatable?(media_type = to_constructable) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/media_types/dsl.rb', line 47

def validatable?(media_type = to_constructable)
  return false unless validations

  resolved = validations.find(media_type, -> { nil })

  !resolved.nil?
end

#validate!(output, **opts) ⇒ Object



39
40
41
# File 'lib/media_types/dsl.rb', line 39

def validate!(output, **opts)
  to_constructable.validate!(output, **opts)
end

#validate_unsafe!(output, media_type = to_constructable, **opts) ⇒ Object



43
44
45
# File 'lib/media_types/dsl.rb', line 43

def validate_unsafe!(output, media_type = to_constructable, **opts)
  validations.find(media_type).validate(output, backtrace: ['.'], **opts)
end

#version(v) ⇒ Object



65
66
67
# File 'lib/media_types/dsl.rb', line 65

def version(v)
  to_constructable.version(v)
end

#view(v) ⇒ Object



62
63
64
# File 'lib/media_types/dsl.rb', line 62

def view(v)
  to_constructable.view(v)
end