Class: SoberSwag::OutputObject::View

Inherits:
Serializer::Base show all
Includes:
FieldSyntax
Defined in:
lib/sober_swag/output_object/view.rb

Overview

DSL for defining a view. Used in view blocks within define.

Views are "variants" of SoberSwag::OutputObjects that contain different fields.

Defined Under Namespace

Classes: NestingError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FieldSyntax

#field, #merge, #multi, #primitive

Methods inherited from Serializer::Base

#array, #finalize_lazy_type!, #identifier, #lazy_type, #lazy_type?, #meta, #optional, #via_map

Constructor Details

#initialize(name, base_fields = []) ⇒ View

Returns a new instance of View.

Parameters:



32
33
34
35
# File 'lib/sober_swag/output_object/view.rb', line 32

def initialize(name, base_fields = [])
  @name = name
  @fields = base_fields.dup
end

Instance Attribute Details

#fieldsArray<SoberSwag::OutputObject::Fields> (readonly)

Returns the fields defined in this view.

Returns:

  • (Array<SoberSwag::OutputObject::Fields>)

    the fields defined in this view.



43
44
45
# File 'lib/sober_swag/output_object/view.rb', line 43

def fields
  @fields
end

#nameSymbol (readonly)

Returns the name of this view.

Returns:

  • (Symbol)

    the name of this view



39
40
41
# File 'lib/sober_swag/output_object/view.rb', line 39

def name
  @name
end

Class Method Details

.define(name, base_fields, &block) ⇒ SoberSwag::OutputObject::View

Define a new view with the given base fields.

Parameters:

Returns:



17
18
19
20
21
# 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

Instance Method Details

#add_field!(field) ⇒ nil

Adds a field do this view.

Parameters:

Returns:

  • (nil)

    nothing interesting



80
81
82
# File 'lib/sober_swag/output_object/view.rb', line 80

def add_field!(field)
  @fields << field
end

#except!(name) ⇒ nil

Excludes a field with the given name from this view.

Parameters:

  • name (Symbol)

    field to exclude.

Returns:

  • (nil)

    nothing interesting



65
66
67
# File 'lib/sober_swag/output_object/view.rb', line 65

def except!(name)
  @fields.reject! { |f| f.name == name }
end

#serialize(obj, opts = {}) ⇒ Hash

Serialize an object according to this view.

Parameters:

  • object

    what to serialize

  • opts (Hash) (defaults to: {})

    arbitrary options

Returns:

  • (Hash)

    the serialized result



50
51
52
# File 'lib/sober_swag/output_object/view.rb', line 50

def serialize(obj, opts = {})
  serializer.serialize(obj, opts)
end

#serializerSoberSwag::Serializer::FieldList

Get the serializer defined by this view. WARNING: Don't add more fields after you call this.



96
97
98
99
# 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_sString

Pretty show for humans

Returns:

  • (String)


87
88
89
# File 'lib/sober_swag/output_object/view.rb', line 87

def to_s
  "<SoberSwag::OutputObject::View(#{identifier})>"
end

#typeClass

Get the type of this view.

Returns:

  • (Class)

    the type, a subclass of Dry::Struct



57
58
59
# File 'lib/sober_swag/output_object/view.rb', line 57

def type
  serializer.type
end

#viewObject

Always raises NestingError

Raises:



72
73
74
# File 'lib/sober_swag/output_object/view.rb', line 72

def view(*)
  raise NestingError, 'no views in views'
end