class SoberSwag::OutputObject::View
DSL for defining a view. Used in `view` blocks within {SoberSwag::OutputObject.define}.
Views are “variants” of {SoberSwag::OutputObject}s that contain different fields.
Attributes
@return [Array<SoberSwag::OutputObject::Fields>] the fields defined in this view.
@return [Symbol] the name of this view
Public Class Methods
Define a new view with the given base fields. @param name [Symbol] name for this view @param base_fields [Array<SoberSwag::OutputObject::Field>] fields already defined @yieldself [SoberSwag::OutputObject::View]
@return [SoberSwag::OutputObject::View]
# File lib/sober_swag/output_object/view.rb, line 17 def self.define(name, base_fields, &block) new(name, base_fields).tap do |view| view.instance_eval(&block) end end
@param name [Sybmol] name for this view. @param base_fields [Array<SoberSwag::OutputObject::Field>] already-defined fields.
# File lib/sober_swag/output_object/view.rb, line 32 def initialize(name, base_fields = []) @name = name @fields = base_fields.dup end
Public Instance Methods
Adds a field do this view. @param field [SoberSwag::OutputObject::Field] @return [nil] nothing interesting
# File lib/sober_swag/output_object/view.rb, line 80 def add_field!(field) @fields << field end
Excludes a field with the given name from this view. @param name [Symbol] field to exclude. @return [nil] nothing interesting
# File lib/sober_swag/output_object/view.rb, line 65 def except!(name) @fields.reject! { |f| f.name == name } end
Serialize an object according to this view. @param object what to serialize @param opts [Hash] arbitrary options @return [Hash] the serialized result
# File lib/sober_swag/output_object/view.rb, line 50 def serialize(obj, opts = {}) serializer.serialize(obj, opts) end
Get the serializer defined by this view. WARNING: Don't add more fields after you call this.
@return [SoberSwag::Serializer::FieldList]
# File lib/sober_swag/output_object/view.rb, line 96 def serializer @serializer ||= SoberSwag::Serializer::FieldList.new(fields).tap { |s| s.identifier(identifier) } end
Pretty show for humans @return [String]
# File lib/sober_swag/output_object/view.rb, line 87 def to_s "<SoberSwag::OutputObject::View(#{identifier})>" end
Get the type of this view. @return [Class] the type, a subclass of {Dry::Struct}
# File lib/sober_swag/output_object/view.rb, line 57 def type serializer.type end
Always raises {NestingError} @raise {NestingError} always
# File lib/sober_swag/output_object/view.rb, line 72 def view(*) raise NestingError, 'no views in views' end