Class: Typesense::IndexSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/typesense-rails.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
250
OPTIONS =
[
  :multi_way_synonyms,
  :one_way_synonyms,
  :synonym_sets,
  :curation_sets,
  :predefined_fields,
  :fields,
  :default_sorting_field,
  :symbols_to_index,
  :token_separators,
  :enable_nested_fields,
  :metadata,
]

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ IndexSettings

Returns a new instance of IndexSettings.



99
100
101
102
# File 'lib/typesense-rails.rb', line 99

def initialize(options, &block)
  @options = options
  instance_exec(&block) if block_given?
end

Instance Method Details

#active_record?(object) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/typesense-rails.rb', line 136

def active_record?(object)
  !mongoid?(object) && !sequel?(object)
end

#add_attribute(*names, &block) ⇒ Object Also known as: add_attributes

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
# File 'lib/typesense-rails.rb', line 118

def add_attribute(*names, &block)
  raise ArgumentError.new("Cannot pass multiple attribute names if block given") if block_given? and names.length > 1
  @additional_attributes ||= {}
  names.each do |name|
    @additional_attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
  end
end

#attribute(*names, &block) ⇒ Object Also known as: attributes

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
# File 'lib/typesense-rails.rb', line 108

def attribute(*names, &block)
  raise ArgumentError.new("Cannot pass multiple attribute names if block given") if block_given? and names.length > 1
  @attributes ||= {}
  names.flatten.each do |name|
    @attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
  end
end

#attributes_to_hash(attributes, object) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/typesense-rails.rb', line 157

def attributes_to_hash(attributes, object)
  if attributes
    Hash[attributes.map { |name, value| [name.to_s, value.call(object)] }]
  else
    {}
  end
end

#encode_attributes(v) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/typesense-rails.rb', line 212

def encode_attributes(v)
  case v
  when String
    v.dup.force_encoding("utf-8")
  when Hash
    v.each { |key, value| v[key] = encode_attributes(value) }
  when Array
    v.map { |x| encode_attributes(x) }
  else
    v
  end
end

#get_attribute_names(object) ⇒ Object



153
154
155
# File 'lib/typesense-rails.rb', line 153

def get_attribute_names(object)
  get_attributes(object).keys
end

#get_attributes(object) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/typesense-rails.rb', line 165

def get_attributes(object)
  # If a serializer is set, we ignore attributes
  # everything should be done via the serializer
  if not @serializer.nil?
    attributes = @serializer.new(object).attributes
  elsif @attributes.nil? || @attributes.length.zero?
    attributes = get_default_attributes(object)
    # no `attribute ...` have been configured, use the default attributes of the model
  elsif active_record?(object)
    # at least 1 `attribute ...` has been configured, therefore use ONLY the one configured
    object.class.unscoped do
      attributes = attributes_to_hash(@attributes, object)
    end
  else
    attributes = attributes_to_hash(@attributes, object)
  end

  attributes.merge!(attributes_to_hash(@additional_attributes, object)) if @additional_attributes

  if @options[:sanitize]
    sanitizer = begin
        ::HTML::FullSanitizer.new
      rescue NameError
        # from rails 4.2
        ::Rails::Html::FullSanitizer.new
      end
    attributes = sanitize_attributes(attributes, sanitizer)
  end

  attributes = encode_attributes(attributes) if @options[:force_utf8_encoding] && Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f > 1.8

  attributes
end

#get_default_attributes(object) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/typesense-rails.rb', line 140

def get_default_attributes(object)
  if mongoid?(object)
    # work-around mongoid 2.4's unscoped method, not accepting a block
    object.attributes
  elsif sequel?(object)
    object.to_hash
  else
    object.class.unscoped do
      object.attributes
    end
  end
end

#get_setting(name) ⇒ Object



225
226
227
# File 'lib/typesense-rails.rb', line 225

def get_setting(name)
  instance_variable_get("@#{name}")
end

#mongoid?(object) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/typesense-rails.rb', line 128

def mongoid?(object)
  defined?(::Mongoid::Document) && object.class.include?(::Mongoid::Document)
end

#sanitize_attributes(v, sanitizer) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/typesense-rails.rb', line 199

def sanitize_attributes(v, sanitizer)
  case v
  when String
    sanitizer.sanitize(v)
  when Hash
    v.each { |key, value| v[key] = sanitize_attributes(value, sanitizer) }
  when Array
    v.map { |x| sanitize_attributes(x, sanitizer) }
  else
    v
  end
end

#sequel?(object) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/typesense-rails.rb', line 132

def sequel?(object)
  defined?(::Sequel) && object.class < ::Sequel::Model
end

#use_serializer(serializer) ⇒ Object



104
105
106
# File 'lib/typesense-rails.rb', line 104

def use_serializer(serializer)
  @serializer = serializer
end