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

fields[R]

@return [Array<SoberSwag::OutputObject::Fields>] the fields defined in this view.

name[R]

@return [Symbol] the name of this view

Public Class Methods

define(name, base_fields, &block) click to toggle source

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
new(name, base_fields = []) click to toggle source

@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

add_field!(field) click to toggle source

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
except!(name) click to toggle source

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(obj, opts = {}) click to toggle source

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
serializer() click to toggle source

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
to_s() click to toggle source

Pretty show for humans @return [String]

# File lib/sober_swag/output_object/view.rb, line 87
def to_s
  "<SoberSwag::OutputObject::View(#{identifier})>"
end
type() click to toggle source

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
view(*) click to toggle source

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