Sha256: 4d6b8df610ff5e752b54890907d0a940cd7a6a05624cdaf831b5f73d9fde880f

Contents?: true

Size: 973 Bytes

Versions: 6

Compression:

Stored size: 973 Bytes

Contents

class AttrSearchable::HashParser
  def initialize(model)
    @model = model
  end

  def parse(hash)
    res = hash.collect do |key, value|
      case key
        when :and
          value.collect { |val| parse val }.inject(:and)
        when :or
          value.collect { |val| parse val }.inject(:or)
        when :not
          parse(value).not
        when :query
          AttrSearchable::Parser.parse value, @model
        else
          parse_attribute key, value
      end
    end

    res.inject :and
  end

  private

  def parse_attribute(key, value)
    collection = AttrSearchableGrammar::Attributes::Collection.new(@model, key.to_s)

    if value.is_a?(Hash)
      raise(AttrSearchable::ParseError, "Unknown operator #{value.keys.first}") unless [:matches, :eq, :not_eq, :gt, :gteq, :lt, :lteq].include?(value.keys.first)

      collection.send value.keys.first, value.values.first.to_s
    else
      collection.send :matches, value.to_s
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attr_searchable-0.0.7 lib/attr_searchable/hash_parser.rb
attr_searchable-0.0.6 lib/attr_searchable/hash_parser.rb
attr_searchable-0.0.5 lib/attr_searchable/hash_parser.rb
attr_searchable-0.0.4 lib/attr_searchable/hash_parser.rb
attr_searchable-0.0.3 lib/attr_searchable/hash_parser.rb
attr_searchable-0.0.2 lib/attr_searchable/hash_parser.rb