Class: Flexi::Json::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/flexi/json/dataset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Dataset

Returns a new instance of Dataset.



6
7
8
9
10
11
12
13
# File 'lib/flexi/json/dataset.rb', line 6

def initialize(attributes = {})
  @attributes = attributes
  @attributes.each do |key, value|
    validate_key(key)
    set_instance_variable(key, value)
    define_accessor_methods(key)
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/flexi/json/dataset.rb', line 4

def attributes
  @attributes
end

Instance Method Details

#matches?(query, fields = nil, options: nil) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flexi/json/dataset.rb', line 15

def matches?(query, fields = nil, options: nil)
  options ||= Flexi::Json::Configuration.default_match_options

  valid_fields = validateable_fields(query, fields)&.select do |field|
    searchable_fields.include?(field)
  end || searchable_fields

  return false if valid_fields.empty?

  matching_method = options[:match_all] ? :all? : :any?
  valid_fields.public_send(matching_method) do |field|
    search_query = query.is_a?(Hash) ? query[field.to_sym] : query
    attribute_value = attributes[field.to_sym].to_s.downcase
    query_value = search_query.to_s.downcase

    options[:exact_match] ? attribute_value == query_value : attribute_value.include?(query_value)
  end
end

#searchable_fieldsObject



34
35
36
# File 'lib/flexi/json/dataset.rb', line 34

def searchable_fields
  @searchable_fields ||= attributes.keys.map(&:to_s)
end