Sha256: 57186f35d8933060c4edfb5e141bcae2e9ed8bba5a8d8844008cd793700d24a3

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module SoberSwag
  class OutputObject
    ##
    # DSL for defining a view.
    # Used in `view` blocks within {SoberSwag::OutputObject.define}.
    class View < SoberSwag::Serializer::Base
      def self.define(name, base_fields, &block)
        new(name, base_fields).tap do |view|
          view.instance_eval(&block)
        end
      end

      class NestingError < Error; end

      include FieldSyntax

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

      attr_reader :name, :fields

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

      def type
        serializer.type
      end

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

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

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

      ##
      # Get the serializer defined by this view.
      # WARNING: Don't add more fields after you call this.
      def serializer
        @serializer ||=
          SoberSwag::Serializer::FieldList.new(fields).tap { |s| s.identifier(identifier) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sober_swag-0.9.0 lib/sober_swag/output_object/view.rb