Class: Tapioca::Dsl::Compilers::Operandi

Inherits:
Compiler
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/compilers/operandi.rb

Overview

Tapioca DSL compiler for Operandi

Generates RBI signatures for methods automatically defined by the arg/argument and output DSL macros in operandi.

For each argument and output, three methods are generated:

  • Getter: def name - returns the value
  • Predicate: def name? - returns boolean
  • Setter: def name= (private) - sets the value

Additionally, typed inner classes are generated:

  • Arguments - T::Struct representing all service arguments
  • Outputs - T::Struct representing all service outputs

Examples:

Service definition

class CreateUser < Operandi::Base
  arg :name, type: String
  arg :email, type: String, optional: true
  arg :role, type: [Symbol, String]

  output :user, type: User
end

Generated RBI

class CreateUser
  class Arguments < T::Struct
    prop :name, ::String
    prop :email, T.nilable(::String), default: nil
    prop :role, T.any(::Symbol, ::String)
  end

  class Outputs < T::Struct
    prop :user, ::User
  end

  sig { returns(Arguments) }
  def arg; end

  sig { returns(Outputs) }
  def output; end

  sig { returns(String) }
  def name; end

  sig { returns(T::Boolean) }
  def name?; end

  sig { returns(T.nilable(String)) }
  def email; end

  sig { returns(T::Boolean) }
  def email?; end

  sig { returns(T.any(Symbol, String)) }
  def role; end

  sig { returns(T::Boolean) }
  def role?; end

  sig { returns(User) }
  def user; end

  sig { returns(T::Boolean) }
  def user?; end

  private

  sig { params(value: String).returns(String) }
  def name=(value); end

  # ... other setters
end

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(::Operandi::Base) } }
CONFIG_TYPE =
"T::Hash[T.any(::String, ::Symbol), T.untyped]"
SERVICE_OR_CONFIG_TYPE =
"T.any(::Operandi::Base, #{CONFIG_TYPE})".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



91
92
93
94
95
# File 'lib/tapioca/dsl/compilers/operandi.rb', line 91

def gather_constants
  all_classes.select do |klass|
    klass < ::Operandi::Base && klass.name && klass != ::Operandi::Base
  end
end

Instance Method Details

#decorateObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tapioca/dsl/compilers/operandi.rb', line 99

def decorate
  root.create_path(constant) do |klass|
    # Generate class methods (.run, .run!, .with)
    generate_class_methods(klass)

    # Generate typed inner classes for arguments and outputs
    generate_arguments_type(klass)
    generate_outputs_type(klass)

    # Generate argument methods
    constant.arguments.each_value do |field|
      generate_field_methods(klass, field)
    end

    # Generate output methods
    constant.outputs.each_value do |field|
      generate_field_methods(klass, field)
    end
  end
end