TrailerVote/trailervote-media-types

View on GitHub
ruby/lib/trailer_vote/media_types/place.rb

Summary

Maintainability
A
50 mins
Test Coverage
# frozen_string_literal: true

require_relative 'base_text'
require_relative 'types/uuid_v4'
require_relative 'types/iso8601'

module TrailerVote
  module MediaTypes

    ##
    # Media Types for places
    #
    # Each set of credentials has a Place associated to it. A place might be the HQ of an enterprise, or a physical
    # location on the map. You can always access all the data of your place and all the children, as the places in our
    # system are configured to be a tree.
    #
    class Place < BaseText
      use_name 'place'

      validations do
        index_scheme = ::MediaTypes::Scheme.new do
          attribute :places do
            collection :_index, allow_empty: true do
              attribute :href, String
              not_strict
            end

            not_strict
          end
        end

        version 4 do
          attribute :place do
            attribute :name, String
            attribute :parent_place, AllowNil(String)
            attribute :expires_at, AllowNil(String)
            attribute :updated_at, Types::Iso8601

            link :self
            link :products_archive
            link :fragments_archive
            link :parent, allow_nil: true
            link :children
          end

          view 'index' do
            merge index_scheme
          end
        end

        version 3 do
          attribute :place do
            attribute :name, String
            attribute :parent_place, AllowNil(String)
            attribute :expires_at, AllowNil(String)
            attribute :updated_at, Types::Iso8601

            link :self
            link :products_archive
            link :parent, allow_nil: true
            link :children
          end

          view 'index' do
            merge index_scheme
          end
        end

        version 2 do
          version_2_base = ::MediaTypes::Scheme.new do
            attribute :name, String
            attribute :parent_place, AllowNil(String)
            attribute :expires_at, AllowNil(String)
            attribute :updated_at, Types::Iso8601

            link :self
            link :products_archive
            link :parent, allow_nil: true
          end

          attribute :place do
            merge version_2_base
          end

          view 'create' do
            attribute :place do
              attribute :name, String
              attribute :parent_place, String
              attribute :expires_at, AllowNil(Types::Iso8601)
            end
          end

          view 'collection' do
            attribute :places do
              collection :_embedded, version_2_base
              not_strict
            end
          end

          view 'index' do
            merge index_scheme
          end
        end

        version 1 do
          version_1_base = ::MediaTypes::Scheme.new do
            attribute :name, String
            attribute :parent_place, AllowNil(String)
            attribute :expires_at, AllowNil(String)
            attribute :updated_at, Types::Iso8601

            link :self
            link :products
            link :parent, allow_nil: true
          end

          attribute :place do
            merge version_1_base
          end

          view 'collection' do
            attribute :places do
              collection :_embedded, version_1_base
              not_strict
            end
          end

          view 'index' do
            merge index_scheme
          end
        end
      end
    end
  end
end