Module: Operandi::Dsl::ArgumentsDsl::ClassMethods

Defined in:
lib/operandi/dsl/arguments_dsl.rb

Instance Method Summary collapse

Instance Method Details

#arg(name, opts = {}) ⇒ Object

Define an argument for the service

Examples:

Define a required string argument

arg :name, type: String

Define an optional argument with default

arg :age, type: Integer, optional: true, default: 25

Define an argument with multiple allowed types

arg :id, type: [String, Integer]

Define an argument with proc default

arg :timestamp, type: Time, default: -> { Time.now }

Define a context argument passed to child services

arg :current_user, type: User, context: true

Parameters:

  • name (Symbol)

    the argument name

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

    options for configuring the argument

Options Hash (opts):

  • :type (Class, Array<Class>)

    Type(s) to validate against (e.g., String, Integer, [String, Symbol])

  • :optional (Boolean) — default: false

    Whether nil values are allowed

  • :default (Object, Proc)

    Default value or proc to evaluate in instance context

  • :context (Boolean) — default: false

    Whether to pass this argument to child services



39
40
41
42
43
44
45
46
47
# File 'lib/operandi/dsl/arguments_dsl.rb', line 39

def arg(name, opts = {})
  Validation.validate_symbol_name!(name, :argument, self)
  Validation.validate_reserved_name!(name, :argument, self)
  Validation.validate_name_conflicts!(name, :argument, self)
  Validation.validate_type_required!(name, :argument, self, opts)

  own_arguments[name] = Settings::Field.new(name, self, opts.merge(field_type: FieldTypes::ARGUMENT))
  @arguments = nil # Clear memoized arguments since we're modifying them
end

#argumentsHash

Get all arguments including inherited ones

Returns:

  • (Hash)

    all arguments defined for this service



60
61
62
# File 'lib/operandi/dsl/arguments_dsl.rb', line 60

def arguments
  @arguments ||= build_arguments
end

#own_argumentsHash

Get only arguments defined in this class

Returns:

  • (Hash)

    arguments defined in this class only



67
68
69
# File 'lib/operandi/dsl/arguments_dsl.rb', line 67

def own_arguments
  @own_arguments ||= {}
end

#remove_arg(name) ⇒ Object

Remove an argument from the service

Parameters:

  • name (Symbol)

    the argument name to remove



52
53
54
55
# File 'lib/operandi/dsl/arguments_dsl.rb', line 52

def remove_arg(name)
  own_arguments.delete(name)
  @arguments = nil # Clear memoized arguments since we're modifying them
end