Class: MediaTypes::Constructable

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/media_types/constructable.rb

Defined Under Namespace

Classes: NoArgumentGiven

Instance Method Summary collapse

Constructor Details

#initialize(klazz, **opts) ⇒ Constructable



11
12
13
14
# File 'lib/media_types/constructable.rb', line 11

def initialize(klazz, **opts)
  super klazz
  self.opts = opts
end

Instance Method Details

#+(other) ⇒ Object



68
69
70
# File 'lib/media_types/constructable.rb', line 68

def +(other)
  to_str + other
end

#==(other) ⇒ Object



64
65
66
# File 'lib/media_types/constructable.rb', line 64

def ==(other)
  to_str.send(:==, other)
end

#===(other) ⇒ Object



60
61
62
# File 'lib/media_types/constructable.rb', line 60

def ===(other)
  to_str.send(:===, other)
end

#as_keyObject



76
77
78
# File 'lib/media_types/constructable.rb', line 76

def as_key
  [type, view, version, suffix]
end

#available_validationsObject



96
97
98
99
# File 'lib/media_types/constructable.rb', line 96

def available_validations
  return [] if !validatable?
  [self]
end

#collectionObject



36
37
38
# File 'lib/media_types/constructable.rb', line 36

def collection
  view(COLLECTION_VIEW)
end

#collection?Boolean



40
41
42
# File 'lib/media_types/constructable.rb', line 40

def collection?
  opts[:view] == COLLECTION_VIEW
end

#createObject



44
45
46
# File 'lib/media_types/constructable.rb', line 44

def create
  view(CREATE_VIEW)
end

#create?Boolean



48
49
50
# File 'lib/media_types/constructable.rb', line 48

def create?
  opts[:view] == CREATE_VIEW
end

#hashObject



80
81
82
# File 'lib/media_types/constructable.rb', line 80

def hash
  as_key.hash
end

#indexObject



52
53
54
# File 'lib/media_types/constructable.rb', line 52

def index
  view(INDEX_VIEW)
end

#index?Boolean



56
57
58
# File 'lib/media_types/constructable.rb', line 56

def index?
  opts[:view] == INDEX_VIEW
end

#split(pattern = nil, *limit) ⇒ Object



72
73
74
# File 'lib/media_types/constructable.rb', line 72

def split(pattern = nil, *limit)
  to_str.split(pattern, *limit)
end

#suffix(suffix = NO_ARG) ⇒ Object



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

def suffix(suffix = NO_ARG)
  return opts[:suffix] if suffix == NO_ARG
  with(suffix: suffix)
end

#to_str(qualifier = nil) ⇒ Object Also known as: inspect, to_s, identifier



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/media_types/constructable.rb', line 84

def to_str(qualifier = nil)
  qualified(
    qualifier,
    __getobj__.media_type_name_for.call(
      type: opts[:type],
      view: opts[:view],
      version: opts[:version],
      suffix: opts[:suffix],
    )
  )
end

#type(name = NO_ARG) ⇒ Object



16
17
18
19
# File 'lib/media_types/constructable.rb', line 16

def type(name = NO_ARG)
  return opts[:type] if name == NO_ARG
  with(type: name)
end

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

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
# File 'lib/media_types/constructable.rb', line 101

def valid?(output, **validation_opts)
  raise ArgumentError, "Unable to validate #{to_s} type without a corresponding validation. Please mark objects that should be empty with 'empty'." unless validatable?

  __getobj__.valid_unsafe?(
    output,
    self,
    **validation_opts
  )
end

#validatable?Boolean



121
122
123
124
125
# File 'lib/media_types/constructable.rb', line 121

def validatable?
  return false unless media_type_combinations.include? as_key
  
  __getobj__.validatable?(self)
end

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

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
# File 'lib/media_types/constructable.rb', line 111

def validate!(output, **validation_opts)
  raise ArgumentError, "Unable to validate #{to_s} type without a corresponding validation. Please mark objects that should be empty with 'empty'." unless validatable?

  __getobj__.validate_unsafe!(
    output,
    self,
    **validation_opts
  )
end

#version(version = NO_ARG) ⇒ Object



21
22
23
24
# File 'lib/media_types/constructable.rb', line 21

def version(version = NO_ARG)
  return opts[:version] if version == NO_ARG
  with(version: version)
end

#view(view = NO_ARG) ⇒ Object



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

def view(view = NO_ARG)
  return opts[:view] if view == NO_ARG
  with(view: view)
end