Class: Geminize::Models::CodeExecution::ExecutableCode

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/models/code_execution/executable_code.rb

Overview

Represents executable code generated by the model

Constant Summary collapse

VALID_LANGUAGES =

Valid language options for executable code

["PYTHON"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language, code) ⇒ ExecutableCode

Initialize a new executable code object

Raises:



21
22
23
24
25
# File 'lib/geminize/models/code_execution/executable_code.rb', line 21

def initialize(language, code)
  @language = language
  @code = code
  validate!
end

Instance Attribute Details

#codeString (readonly)



15
16
17
# File 'lib/geminize/models/code_execution/executable_code.rb', line 15

def code
  @code
end

#languageString (readonly)



12
13
14
# File 'lib/geminize/models/code_execution/executable_code.rb', line 12

def language
  @language
end

Instance Method Details

#to_hHash

Alias for to_hash



66
67
68
# File 'lib/geminize/models/code_execution/executable_code.rb', line 66

def to_h
  to_hash
end

#to_hashHash

Convert the executable code to a hash



57
58
59
60
61
62
# File 'lib/geminize/models/code_execution/executable_code.rb', line 57

def to_hash
  {
    language: @language,
    code: @code
  }
end

#validate!Boolean

Validate the executable code

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/geminize/models/code_execution/executable_code.rb', line 30

def validate!
  unless @language.is_a?(String)
    raise Geminize::ValidationError.new(
      "Language must be a string, got #{@language.class}",
      "INVALID_ARGUMENT"
    )
  end

  unless VALID_LANGUAGES.include?(@language)
    raise Geminize::ValidationError.new(
      "Invalid language: #{@language}. Must be one of: #{VALID_LANGUAGES.join(", ")}",
      "INVALID_ARGUMENT"
    )
  end

  unless @code.is_a?(String)
    raise Geminize::ValidationError.new(
      "Code must be a string, got #{@code.class}",
      "INVALID_ARGUMENT"
    )
  end

  true
end