Sha256: da832d3711d00835bada35b7d46f50ec658e85b559670dd64ee5718a11178001

Contents?: true

Size: 2 KB

Versions: 7

Compression:

Stored size: 2 KB

Contents

#!/usr/bin/env ruby

require 'bundler/setup'
require 'sober_swag'
require 'pry'

Bio = SoberSwag.input_object do
  attribute :description, SoberSwag::Types::String
  attribute :gender, SoberSwag::Types::String.enum('male', 'female') | SoberSwag::Types::String
end

MultiFloorLocation = SoberSwag.input_object do
  attribute :building, SoberSwag::Types::String.enum('science', 'mathematics', 'literature')
  attribute :floor, SoberSwag::Types::String
  attribute :room, SoberSwag::Types::Integer
end

SingleFloorLocation = SoberSwag.input_object do
  attribute :building, SoberSwag::Types::String.enum('philosophy', 'computer science')
  attribute :room, SoberSwag::Types::Integer
end

SortDirections = SoberSwag::Types::CommaArray.of(SoberSwag::Types::String.enum('created_at', 'updated_at', '-created_at', '-updated_at'))

# test
class Whatever < SoberSwag::Reporting::Input::Struct
  attribute :first_name, SoberSwag::Reporting::Input::Text.new
  attribute :last_name, SoberSwag::Reporting::Input::Text.new
  attribute? :father, SoberSwag::Reporting::Input::Null.new | SoberSwag::Reporting::Input::Defer.new(proc { Whatever }), description: 'if the father is in our db, will be present'
  attribute? :mother, SoberSwag::Reporting::Input::Null.new | SoberSwag::Reporting::Input::Defer.new(proc { Whatever }), description: 'if the mother is in our db, will be present'
end

# Kinda neat thing
class Otherwised < Whatever
  attribute :ident, SoberSwag::Reporting::Input::Text.new.with_pattern(/^[A-Za-z0-9]+$/)
end

ArrayOfPeople = Otherwised.or(Whatever).list

##
# Output for a person
class OutputPerson < SoberSwag::Reporting::Output::Struct
  field :first_name, SoberSwag::Reporting::Output::Text.new
  field :last_name, SoberSwag::Reporting::Output::Text.new
  define_view :detail do
    field :initials, SoberSwag::Reporting::Output::Text.new do |obj|
      [obj.first_name, obj.last_name].map { |e| e[0..0] }.map { |e| "#{e}." }.join(' ')
    end
  end
end

Person = Struct.new(:first_name, :last_name)

OutputPerson.view(:base)

Pry.start

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sober_swag-0.25.2 bin/console
sober_swag-0.25.1 bin/console
sober_swag-0.25.0 bin/console
sober_swag-0.24.1 bin/console
sober_swag-0.24.0 bin/console
sober_swag-0.23.0 bin/console
sober_swag-0.22.0 bin/console