Class: RediSearcher::Schema::Field

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

Constant Summary collapse

OPTIONS_FLAGS =
{
  text: [:no_stem],
  all: [:sortable, :no_index]
}
OPTIONS_PARAMS =
{
  text: [:phonetic, :weight],
  tag: [:separator]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, **options) ⇒ Field

Returns a new instance of Field.



16
17
18
19
20
21
# File 'lib/redi_searcher/schema/field.rb', line 16

def initialize(name, type, **options)
  default_options = type == :tag ? {separator: ','} : {}
  @name = name
  @type = type
  @options = default_options.deep_merge(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/redi_searcher/schema/field.rb', line 14

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/redi_searcher/schema/field.rb', line 14

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/redi_searcher/schema/field.rb', line 14

def type
  @type
end

Instance Method Details

#flags_for_type(type, **options) ⇒ Object



27
28
29
30
31
# File 'lib/redi_searcher/schema/field.rb', line 27

def flags_for_type(type, **options)
  self.class::OPTIONS_FLAGS[type].to_a.map do |key|
    key.to_s.upcase if options[key]
  end.compact
end

#params_for_type(type, **options) ⇒ Object



33
34
35
36
37
# File 'lib/redi_searcher/schema/field.rb', line 33

def params_for_type(type, **options)
  self.class::OPTIONS_PARAMS[type].to_a.map do |key|
    [key.to_s.upcase, *options[key]] unless options[key].nil?
  end.compact
end

#serializeObject



23
24
25
# File 'lib/redi_searcher/schema/field.rb', line 23

def serialize
  [name.to_s, type.to_s.upcase, flags_for_type(type, options), params_for_type(type, options), flags_for_type(:all, options)].flatten
end