Module: Structify::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/structify/model.rb

Overview

The Model module provides a DSL for defining LLM extraction schemas in your Rails models. It allows you to define fields, versioning, and validation for LLM-based data extraction.

Examples:

class Article < ApplicationRecord
  include Structify::Model

  schema_definition do
    title "Article Extraction"
    description "Extract article metadata"
    version 1

    field :title, :string, required: true
    field :summary, :text, description: "A brief summary of the article"
    field :category, :string, enum: ["tech", "business", "science"]
  end
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#saved_change_to_extracted_data?Boolean

Check if the extracted data has been changed since the record was last saved Respects the default_container_attribute configuration

This provides a consistent method to check for changes regardless of the container attribute name that was configured.

Returns:

  • (Boolean)

    Whether the extracted data has changed



59
60
61
62
63
# File 'lib/structify/model.rb', line 59

def saved_change_to_extracted_data?
  container_attribute = self.class.attr_json_config.default_container_attribute
  respond_to?("saved_change_to_#{container_attribute}?") && 
    self.send("saved_change_to_#{container_attribute}?")
end

#stored_versionObject

Get the stored version of this record



46
47
48
49
50
# File 'lib/structify/model.rb', line 46

def stored_version
  container_attribute = self.class.attr_json_config.default_container_attribute
  record_data = self.send(container_attribute) || {}
  record_data["version"] || 1
end

#version_compatible_with?(required_version) ⇒ Boolean

Instance methods

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/structify/model.rb', line 38

def version_compatible_with?(required_version)
  container_attribute = self.class.attr_json_config.default_container_attribute
  record_data = self.send(container_attribute) || {}
  record_version = record_data["version"] || 1
  record_version >= required_version
end

#version_in_range?(version, range) ⇒ Boolean

Check if a version is within a given range/array of versions This is used in field accessors to check version compatibility

Parameters:

  • version (Integer)

    The version to check

  • range (Range, Array, Integer)

    The range, array, or single version to check against

Returns:

  • (Boolean)

    Whether the version is within the range



71
72
73
74
75
76
77
78
79
80
# File 'lib/structify/model.rb', line 71

def version_in_range?(version, range)
  case range
  when Range
    range.cover?(version)
  when Array
    range.include?(version)
  else
    version == range
  end
end