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

Returns a new instance of 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



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

def +(other)
  to_str + other
end

#==(other) ⇒ Object



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

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

#===(other) ⇒ Object



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

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

#as_keyObject



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

def as_key
  [type, view&.to_s, version]
end

#available_validationsObject



102
103
104
105
# File 'lib/media_types/constructable.rb', line 102

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

#collectionObject



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

def collection
  view(COLLECTION_VIEW)
end

#collection?Boolean

Returns:

  • (Boolean)


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

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

#createObject



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

def create
  view(CREATE_VIEW)
end

#create?Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  as_key.hash
end

#indexObject



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

def index
  view(INDEX_VIEW)
end

#index?Boolean

Returns:

  • (Boolean)


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

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

#override_suffix(suffix) ⇒ Object



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

def override_suffix(suffix)
  with(suffix: suffix)
end

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



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

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

#suffixObject



83
84
85
86
87
88
# File 'lib/media_types/constructable.rb', line 83

def suffix
  return opts[:suffix] if opts.key?(:suffix)

  schema = schema_for(self)
  schema.type_attributes.fetch(:suffix, 'json')
end

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



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/media_types/constructable.rb', line 90

def to_str(qualifier = nil)
  qualified(
    qualifier,
    __getobj__.media_type_name_for.call(
      type: opts[:type],
      view: opts[:view],
      version: opts[:version],
      suffix: 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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
# File 'lib/media_types/constructable.rb', line 107

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

Returns:

  • (Boolean)


127
128
129
130
131
132
# File 'lib/media_types/constructable.rb', line 127

def validatable?
  return false if media_type_combinations.nil?
  return false unless media_type_combinations.include? as_key

  __getobj__.validatable?(self)
end

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

Raises:

  • (ArgumentError)


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

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