Class: LlmScraper::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_scraper/schema.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



25
26
27
# File 'lib/llm_scraper/schema.rb', line 25

def initialize
  @fields = {}
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/llm_scraper/schema.rb', line 5

def fields
  @fields
end

Class Method Details

.define(&block) ⇒ Schema

Parameters:

  • block (Proc)

    DSL block

Returns:



9
10
11
12
13
# File 'lib/llm_scraper/schema.rb', line 9

def self.define(&block)
  schema = new
  schema.instance_eval(&block)
  schema
end

.from_hash(hash) ⇒ Schema

Parameters:

  • hash (Hash)

    { field_name => { type:, description:, … } }

Returns:



17
18
19
20
21
22
23
# File 'lib/llm_scraper/schema.rb', line 17

def self.from_hash(hash)
  schema = new
  hash.each do |name, opts|
    schema.field(name.to_sym, **opts.transform_keys(&:to_sym))
  end
  schema
end

Instance Method Details

#field(name, type:, **options) ⇒ Object

Parameters:

  • name (Symbol)
  • type (Symbol)

    :string | :number | :boolean | :array | :object

  • options (Hash)


32
33
34
# File 'lib/llm_scraper/schema.rb', line 32

def field(name, type:, **options)
  @fields[name.to_sym] = Field.new(name: name.to_sym, type: type, **options)
end