Sha256: 1402b9b8e7e88612894cad67cb48776a5a7dacdc85aabdfce7251b714628cf5a

Contents?: true

Size: 969 Bytes

Versions: 28

Compression:

Stored size: 969 Bytes

Contents

module CustomAttributes
  class SearchQueryField
    attr_reader :query, :fuzziness, :operator

    def initialize(field, defaults)
      @defaults = defaults
      @field = field
    end

    def to_query_hash
      query_hash = {}

      return query_hash if query == '*'

      query_hash = query_hash.merge({query: query}) unless query.nil?
      query_hash = query_hash.merge({fuzziness: fuzziness}) unless fuzziness.nil? || fuzziness.zero?
      query_hash = query_hash.merge({operator: operator}) unless operator.nil?

      query_hash
    end

    def query
      field_or_default(:query)
    end

    def fuzziness
      field_or_default(:fuzziness)
    end

    def operator
      field_or_default(:operator)
    end

    def field
      @field ||= {}
    end

    def defaults
      @defaults ||= { query: '*' }
    end

    private 

    def field_or_default(attribute)
      field[attribute].nil? ? defaults[attribute] : field[attribute]
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
create_custom_attributes-0.5.8 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.7 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.6 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.5 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.4 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.3 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.2 lib/custom_attributes/search_query_field.rb
create_custom_attributes-0.5.0 lib/custom_attributes/search_query_field.rb