Class: Explicit::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/explicit/type.rb

Defined Under Namespace

Modules: Modifiers Classes: Agreement, Any, Array, BigDecimal, Boolean, Date, DateRange, DateTimeISO8601, DateTimeISO8601Range, DateTimeUnixEpoch, Enum, File, Float, Hash, Integer, Literal, OneOf, Record, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auth_typeObject

Returns the value of attribute auth_type.



110
111
112
# File 'lib/explicit/type.rb', line 110

def auth_type
  @auth_type
end

#defaultObject

Returns the value of attribute default.



110
111
112
# File 'lib/explicit/type.rb', line 110

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



110
111
112
# File 'lib/explicit/type.rb', line 110

def description
  @description
end

#nilableObject

Returns the value of attribute nilable.



110
111
112
# File 'lib/explicit/type.rb', line 110

def nilable
  @nilable
end

#param_locationObject

Returns the value of attribute param_location.



110
111
112
# File 'lib/explicit/type.rb', line 110

def param_location
  @param_location
end

Class Method Details

.build(type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/explicit/type.rb', line 6

def self.build(type)
  case type
  in :any
    Explicit::Type::Any.new

  in :agreement
    Explicit::Type::Agreement.new

  in [:array, item_type]
    Explicit::Type::Array.new(item_type:)
  in [:array, item_type, options]
    Explicit::Type::Array.new(item_type:, **options)

  in :big_decimal
    Explicit::Type::BigDecimal.new
  in [:big_decimal, options]
    Explicit::Type::BigDecimal.new(**options)

  in :boolean
    Explicit::Type::Boolean.new

  in :date_range
    Explicit::Type::DateRange.new
  in [:date_range, options]
    Explicit::Type::DateRange.new(**options)

  in :date_time_iso8601_range
    Explicit::Type::DateTimeISO8601Range.new()
  in [:date_time_iso8601_range, options]
    Explicit::Type::DateTimeISO8601Range.new(**options)

  in :date_time_iso8601
    Explicit::Type::DateTimeISO8601.new
  in [:date_time_iso8601, options]
    Explicit::Type::DateTimeISO8601.new(**options)

  in :date_time_unix_epoch
    Explicit::Type::DateTimeUnixEpoch.new
  in [:date_time_unix_epoch, options]
    Explicit::Type::DateTimeUnixEpoch.new(**options)

  in :date
    Explicit::Type::Date.new
  in [:date, options]
    Explicit::Type::Date.new(**options)

  in [:hash, key_type, value_type]
    Explicit::Type::Hash.new(key_type:, value_type:)
  in [:hash, key_type, value_type, options]
    Explicit::Type::Hash.new(key_type:, value_type:, **options)

  in [:enum, allowed_values]
    Explicit::Type::Enum.new(allowed_values)

  in :file
    Explicit::Type::File.new
  in [:file, options]
    Explicit::Type::File.new(**options)

  in :float
    Explicit::Type::Float.new
  in [:float, options]
    Explicit::Type::Float.new(**options)

  in :integer
    Explicit::Type::Integer.new
  in [:integer, options]
    Explicit::Type::Integer.new(**options)

  in [:literal, value]
    Explicit::Type::Literal.new(value:)
  in ::String
    Explicit::Type::Literal.new(value: type)

  in [:one_of, *subtypes]
    Explicit::Type::OneOf.new(subtypes:)

  in ::Hash
    Explicit::Type::Record.new(attributes: type)

  in :string
    Explicit::Type::String.new
  in [:string, options]
    Explicit::Type::String.new(**options)

  ## MODIFIERS

  in [:default, default, subtype]
    Explicit::Type::Modifiers::Default.apply(default, subtype)

  in [:description, description, subtype]
    Explicit::Type::Modifiers::Description.apply(description, subtype)

  in [:nilable, type]
    Explicit::Type::Modifiers::Nilable.apply(type)

  in [:_auth_type, auth_type, type]
    Explicit::Type::Modifiers::AuthType.apply(auth_type, type)

  in [:_param_location, param_location, type]
    Explicit::Type::Modifiers::ParamLocation.apply(param_location, type)
  end
end

Instance Method Details

#auth_basic?Boolean

Returns:



124
125
126
# File 'lib/explicit/type.rb', line 124

def auth_basic?
  auth_type == :basic
end

#auth_bearer?Boolean

Returns:



128
129
130
# File 'lib/explicit/type.rb', line 128

def auth_bearer?
  auth_type == :bearer
end

#error_i18n(name, context = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/explicit/type.rb', line 136

def error_i18n(name, context = {})
  key = "explicit.errors.#{name}"

  translation =
    if ::I18n.exists?(key)
      ::I18n.t(key, **context)
    else
      ::I18n.t(key, **context.merge(locale: :en))
    end

  [ :error, translation ]
end

#mcp_schemaObject



163
164
165
# File 'lib/explicit/type.rb', line 163

def mcp_schema
  merge_base_json_schema(json_schema(:mcp))
end

#merge_base_json_schema(attributes) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/explicit/type.rb', line 167

def merge_base_json_schema(attributes)
  topics = attributes.delete(:description_topics)&.compact_blank || []

  formatted_description =
    if description.present? && topics.empty?
      description
    elsif description.present? && topics.any?
      description + "\n\n" + topics.join("\n")
    else
      topics.join("\n")
    end

  default_value =
    if default&.respond_to?(:call)
      nil
    else
      default
    end

  base_attributes = {
    default: default_value,
    description: formatted_description
  }.compact_blank

  base_attributes.merge(attributes)
end

#param_location_body?Boolean

Returns:



120
121
122
# File 'lib/explicit/type.rb', line 120

def param_location_body?
  param_location == :body
end

#param_location_path?Boolean

Returns:



112
113
114
# File 'lib/explicit/type.rb', line 112

def param_location_path?
  param_location == :path
end

#param_location_query?Boolean

Returns:



116
117
118
# File 'lib/explicit/type.rb', line 116

def param_location_query?
  param_location == :query
end

#required?Boolean

Returns:



132
133
134
# File 'lib/explicit/type.rb', line 132

def required?
  !nilable && default.blank?
end

#swagger_i18n(name, context = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/explicit/type.rb', line 149

def swagger_i18n(name, context = {})
  key = "explicit.swagger.#{name}"

  if ::I18n.exists?(key)
    ::I18n.t(key, **context)
  else
    ::I18n.t(key, **context.merge(locale: :en))
  end
end

#swagger_schemaObject



159
160
161
# File 'lib/explicit/type.rb', line 159

def swagger_schema
  merge_base_json_schema(json_schema(:swagger))
end