Sha256: d67e066664a9581cfed22a0d3de5e7313c537b350e12cf3e7fd977d41bdf7676

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 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 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

1 entries across 1 versions & 1 rubygems

Version Path
attr_searchable-0.0.1 lib/attr_searchable/hash_parser.rb