Class: SoberSwag::InputObject
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- SoberSwag::InputObject
- Includes:
- Type::Named
- Defined in:
- lib/sober_swag/input_object.rb
Overview
A variant of Dry::Struct that allows you to set a "model name" that is publicly visible. If you do not set one, it will be the Ruby class name, with any '::' replaced with a '.'.
This otherwise behaves exactly like a Dry::Struct. Please see the documentation for that class to see how it works.
Class Method Summary collapse
- .attribute(key, parent = SoberSwag::InputObject, &block) ⇒ Object
- .attribute?(key, parent = SoberSwag::InputObject, &block) ⇒ Boolean
-
.identifier(new_ident = nil) ⇒ Object
The name to use for this type in external documentation.
-
.meta(*args) ⇒ SoberSwag::InputObject
Add metadata keys, like
:description
, to the defined type. -
.param(name) ⇒ Object
Convenience method: you can use
.param
to get a parameter parser of a given type. -
.primitive(*args) ⇒ Object
Convenience method: you can use
.primitive
get a primitive parser for a given type. -
.type_attribute(value, attribute_key: :type) ⇒ Object
Add on an attribute which only ever parses from a constant value.
- .valid_field_def?(parent, block) ⇒ Boolean private
Methods included from Type::Named
Class Method Details
.attribute(key, parent = SoberSwag::InputObject, &block) ⇒ Object .attribute(key, type) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/sober_swag/input_object.rb', line 43 def attribute(key, parent = SoberSwag::InputObject, &block) raise ArgumentError, "parent class #{parent} is not an input object type!" unless valid_field_def?(parent, block) raise ArgumentError, "cannot mix reporting and non-reporting types at attribute #{key}" if parent.is_a?(SoberSwag::Reporting::Input::Interface) super(key, parent, &block) end |
.attribute(key, parent = SoberSwag::InputObject, &block) ⇒ Boolean .attribute(key, type) ⇒ Boolean
99 100 101 102 103 |
# File 'lib/sober_swag/input_object.rb', line 99 def attribute?(key, parent = SoberSwag::InputObject, &block) raise ArgumentError, "parent class #{parent} is not an input object type!" unless valid_field_def?(parent, block) super(key, parent, &block) end |
.identifier(new_ident = nil) ⇒ Object
The name to use for this type in external documentation.
17 18 19 20 21 |
# File 'lib/sober_swag/input_object.rb', line 17 def identifier(new_ident = nil) @identifier = new_ident if new_ident @identifier || name.to_s.gsub('::', '.') end |
.meta(*args) ⇒ SoberSwag::InputObject
Add metadata keys, like :description
, to the defined type.
Note: does NOT mutate the type, returns a new type with the metadata added.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sober_swag/input_object.rb', line 111 def (*args) original = self super(*args).tap do |result| return result unless result.is_a?(Class) result.define_singleton_method(:alias?) { true } result.define_singleton_method(:alias_of) { original } end end |
.param(name) ⇒ Object
Convenience method: you can use .param
to get a parameter parser of a given type.
Said parsers are more loose: for example, param(:Integer)
will parse the string "10"
into 10
, while
primitive(:Integer)
will throw an error.
This method lets you write:
class Foo < SoberSwag::InputObject
attribute :bar, param(:Integer)
end
instead of
class Foo < SoberSwag::InputObject
attribute :bar, SoberSwag::Types::Param::Integer
end
173 174 175 |
# File 'lib/sober_swag/input_object.rb', line 173 def param(name) SoberSwag::Types::Params.const_get(name) end |
.primitive(*args) ⇒ Object
Convenience method: you can use .primitive
get a primitive parser for a given type.
This lets you write:
class Foo < SoberSwag::InputObject
attribute :bar, primitive(:String)
end
instead of
class Foo < SoberSwag::InputObject
attribute :bar, SoberSwag::Types::String
end
142 143 144 145 146 147 148 |
# File 'lib/sober_swag/input_object.rb', line 142 def primitive(*args) if args.length == 1 SoberSwag::Types.const_get(args.first) else super end end |
.type_attribute(value, attribute_key: :type) ⇒ Object
Add on an attribute which only ever parses from a constant value.
By default, this attribute will be called type
, but you can override it with the kwarg.
This is useful in situations where you want to emulate a sum type.
For example, if you want to make an API endpoint that can either accept or reject proposals
ApiInputType = SoberSwag.input_object {
identifier 'AcceptProposal'
type_attribute 'accept'
attribute(:message, primitive(:String))
} | SoberSwag.input_object {
identifier 'RejectProposal'
type_attribute 'reject'
attribute(:message, primitive(:String))
}
Under the hood, this basically looks like:
type_attribute 'archive'
# is equivalent to
attribute(:type, SoberSwag::Types::String.enum('archive'))
80 81 82 |
# File 'lib/sober_swag/input_object.rb', line 80 def type_attribute(value, attribute_key: :type) attribute(attribute_key, SoberSwag::Types::String.enum(value.to_s)) end |
.valid_field_def?(parent, block) ⇒ Boolean (private)
179 180 181 182 183 |
# File 'lib/sober_swag/input_object.rb', line 179 def valid_field_def?(parent, block) return true if block.nil? parent.is_a?(Class) && parent <= SoberSwag::InputObject end |