Class: PromptEngine::Parameter

Inherits:
ApplicationRecord show all
Defined in:
app/models/prompt_engine/parameter.rb

Constant Summary collapse

TYPES =

Parameter types that can be used

%w[string integer decimal boolean datetime date array json].freeze

Instance Method Summary collapse

Instance Method Details

#cast_value(value) ⇒ Object

Convert the parameter value based on its type



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/prompt_engine/parameter.rb', line 23

def cast_value(value)
  return default_value if value.blank? && !required?

  case parameter_type
  when "integer"
    value.to_i
  when "decimal"
    value.to_f
  when "boolean"
    ActiveModel::Type::Boolean.new.cast(value)
  when "datetime"
    DateTime.parse(value.to_s) rescue nil
  when "date"
    Date.parse(value.to_s) rescue nil
  when "array"
    value.is_a?(Array) ? value : value.to_s.split(",").map(&:strip)
  when "json"
    value.is_a?(String) ? JSON.parse(value) : value rescue {}
  else
    value.to_s
  end
end

#form_input_optionsObject

Generate form input attributes for this parameter



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/prompt_engine/parameter.rb', line 81

def form_input_options
  options = {
    label: name.humanize,
    required: required?,
    placeholder: example_value,
    hint: description
  }

  case parameter_type
  when "integer", "decimal"
    options[:type] = "number"
    options[:step] = parameter_type == "decimal" ? "0.01" : "1"
    options[:min] = validation_rules["min"] if validation_rules&.dig("min")
    options[:max] = validation_rules["max"] if validation_rules&.dig("max")
  when "boolean"
    options[:type] = "checkbox"
  when "datetime"
    options[:type] = "datetime-local"
  when "date"
    options[:type] = "date"
  when "array"
    options[:type] = "text"
    options[:hint] = "#{description} (comma-separated values)"
  when "json"
    options[:type] = "textarea"
    options[:hint] = "#{description} (JSON format)"
  else
    options[:type] = "text"
    options[:minlength] = validation_rules["min_length"] if validation_rules&.dig("min_length")
    options[:maxlength] = validation_rules["max_length"] if validation_rules&.dig("max_length")
    options[:pattern] = validation_rules["pattern"] if validation_rules&.dig("pattern")
  end

  options[:value] = default_value if default_value.present?

  options
end

#validate_value(value) ⇒ Object

Validate a value against this parameter’s rules



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/prompt_engine/parameter.rb', line 47

def validate_value(value)
  errors = []

  if required? && value.blank?
    errors << "#{name} is required"
  end

  if validation_rules.present?
    # Apply custom validation rules
    if validation_rules["min_length"] && value.to_s.length < validation_rules["min_length"]
      errors << "#{name} must be at least #{validation_rules['min_length']} characters"
    end

    if validation_rules["max_length"] && value.to_s.length > validation_rules["max_length"]
      errors << "#{name} must be at most #{validation_rules['max_length']} characters"
    end

    if validation_rules["pattern"] && !value.to_s.match?(Regexp.new(validation_rules["pattern"]))
      errors << "#{name} must match pattern: #{validation_rules['pattern']}"
    end

    if validation_rules["min"] && cast_value(value) < validation_rules["min"]
      errors << "#{name} must be at least #{validation_rules['min']}"
    end

    if validation_rules["max"] && cast_value(value) > validation_rules["max"]
      errors << "#{name} must be at most #{validation_rules['max']}"
    end
  end

  errors
end