Class: Typesensual::Schema

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Schema

Returns a new instance of Schema.



20
21
22
# File 'lib/typesensual/schema.rb', line 20

def initialize(&block)
  instance_eval(&block) unless block.nil?
end

Instance Method Details

#default_sorting_field(field_name) ⇒ Object



44
45
46
# File 'lib/typesensual/schema.rb', line 44

def default_sorting_field(field_name)
  @default_sorting_field = field_name.to_s
end

#enable_nested_fields(value = true) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



48
49
50
# File 'lib/typesensual/schema.rb', line 48

def enable_nested_fields(value = true) # rubocop:disable Style/OptionalBooleanParameter
  @enable_nested_fields = value
end

#field(name, type: 'auto', locale: nil, facet: nil, index: nil, optional: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/typesensual/schema.rb', line 24

def field(name, type: 'auto', locale: nil, facet: nil, index: nil, optional: nil)
  @fields ||= []
  @fields << Field.new(
    name: name,
    type: type,
    locale: locale,
    facet: facet,
    index: index,
    optional: optional
  )
end

#initialize_copy(original) ⇒ Object

Duplicate fields from the original



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/typesensual/schema.rb', line 8

def initialize_copy(original)
  %w[
    @fields
    @token_separators
    @symbols_to_index
    @default_sorting_field
    @enable_nested_fields
  ].each do |var|
    instance_variable_set(var, original.instance_variable_get(var).dup)
  end
end

#symbols_to_index(*symbols) ⇒ Object



40
41
42
# File 'lib/typesensual/schema.rb', line 40

def symbols_to_index(*symbols)
  @symbols_to_index = symbols
end

#to_hObject



52
53
54
55
56
57
58
59
60
# File 'lib/typesensual/schema.rb', line 52

def to_h
  {
    'fields' => @fields&.map(&:to_h),
    'token_separators' => @token_separators,
    'symbols_to_index' => @symbols_to_index,
    'default_sorting_field' => @default_sorting_field,
    'enable_nested_fields' => @enable_nested_fields
  }.compact!
end

#token_separators(*separators) ⇒ Object



36
37
38
# File 'lib/typesensual/schema.rb', line 36

def token_separators(*separators)
  @token_separators = separators
end