Class: TypesenseModel::Schema

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_name = nil) ⇒ Schema

Returns a new instance of Schema.



7
8
9
10
11
# File 'lib/typesense_model/schema.rb', line 7

def initialize(collection_name = nil)
  @fields = []
  @collection_name = collection_name
  @default_sorting_field = nil
end

Instance Attribute Details

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



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

def collection_name
  @collection_name
end

#default_sorting_fieldObject (readonly)

Returns the value of attribute default_sorting_field.



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

def default_sorting_field
  @default_sorting_field
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

Instance Method Details

#field(name, type, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/typesense_model/schema.rb', line 13

def field(name, type, options = {})
  @fields << {
    name: name.to_s,
    type: type.to_s,
    facet: options[:facet] || false,
    optional: options[:optional] || false,
    index: options[:index].nil? ? true : options[:index],
    sort: options[:sort] || false
  }

  # Set as default sorting field if specified
  @default_sorting_field = name.to_s if options[:default_sort]
end

#to_hashObject



27
28
29
30
31
32
33
# File 'lib/typesense_model/schema.rb', line 27

def to_hash
  {
    name: @collection_name,
    fields: @fields,
    default_sorting_field: @default_sorting_field
  }.compact
end