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.



9
10
11
12
# File 'lib/media_types/constructable.rb', line 9

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

Instance Method Details

#+(other) ⇒ Object



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

def +(other)
  to_str + other
end

#==(other) ⇒ Object



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

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

#===(other) ⇒ Object



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

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

#collectionObject



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

def collection
  view(COLLECTION_VIEW)
end

#collection?Boolean

Returns:

  • (Boolean)


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

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

#createObject



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

def create
  view(CREATE_VIEW)
end

#create?Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  to_str.hash
end

#indexObject



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

def index
  view(INDEX_VIEW)
end

#index?Boolean

Returns:

  • (Boolean)


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

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

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



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

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

#suffix(suffix = NO_ARG) ⇒ Object



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

def suffix(suffix = NO_ARG)
  return opts[:suffix] if suffix == NO_ARG
  Constructable.new(__getobj__, **with(suffix: suffix))
end

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/media_types/constructable.rb', line 78

def to_str(qualifier = nil)
  # TODO: remove warning by slicing out these arguments if they don't appear in the format

  qualified(qualifier, @to_str ||= format(
    opts.fetch(:format),
    version: opts.fetch(:version),
    suffix: opts.fetch(:suffix) { :json },
    type: opts.fetch(:type),
    view: format_view(opts[:view])
  ))
end

#type(name = NO_ARG) ⇒ Object



14
15
16
17
# File 'lib/media_types/constructable.rb', line 14

def type(name = NO_ARG)
  return opts[:type] if name == NO_ARG
  Constructable.new(__getobj__, **with(type: name))
end

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

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/media_types/constructable.rb', line 89

def valid?(output, **validation_opts)
  __getobj__.valid?(
    output,
    self,
    **validation_opts
  )
end

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



97
98
99
100
101
102
103
# File 'lib/media_types/constructable.rb', line 97

def validate!(output, **validation_opts)
  __getobj__.validate!(
    output,
    self,
    **validation_opts
  )
end

#version(version = NO_ARG) ⇒ Object



19
20
21
22
# File 'lib/media_types/constructable.rb', line 19

def version(version = NO_ARG)
  return opts[:version] if version == NO_ARG
  Constructable.new(__getobj__, **with(version: version))
end

#view(view = NO_ARG) ⇒ Object



24
25
26
27
# File 'lib/media_types/constructable.rb', line 24

def view(view = NO_ARG)
  return opts[:view] if view == NO_ARG
  Constructable.new(__getobj__, **with(view: view))
end