Class: Flexi::Json::Dataset
- Inherits:
-
Object
- Object
- Flexi::Json::Dataset
- Defined in:
- lib/flexi/json/dataset.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Dataset
constructor
A new instance of Dataset.
- #matches?(query, fields = nil, options: nil) ⇒ Boolean
- #searchable_fields ⇒ Object
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
#attributes ⇒ Object (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
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) ||= Flexi::Json::Configuration. valid_fields = validateable_fields(query, fields)&.select do |field| searchable_fields.include?(field) end || searchable_fields return false if valid_fields.empty? matching_method = [: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 [:exact_match] ? attribute_value == query_value : attribute_value.include?(query_value) end end |
#searchable_fields ⇒ Object
34 35 36 |
# File 'lib/flexi/json/dataset.rb', line 34 def searchable_fields @searchable_fields ||= attributes.keys.map(&:to_s) end |