Sha256: a353344d444a83d0166c83fc424fc28f8e40d19803e8d63bb59e6f13a747513b

Contents?: true

Size: 1.07 KB

Versions: 14

Compression:

Stored size: 1.07 KB

Contents

# Parses text to create corresponding mongoid conditions.
# See README for details on syntax.
class Whoops::MongoidSearchParser
  attr_accessor :query
  def initialize(query)
    self.query = query
  end
  
  def conditions
    self.query.split("\n").inject({}) do |conditions, line|
      line.strip!
      next(conditions) if line.empty?
      
      parsed = parse_line(line)
      key = parsed[:method] ? parsed[:key].send(parsed[:method]) : parsed[:key]
      
      conditions[key] = parsed[:value]
      conditions
    end
  end
  
  def parse_line(line)
    key, method, value = line.match(/([^\s]*?)(#[^\s]*)? ([^#]*)/)[1..3]
    
    key = key.sub(/:$/, '').to_sym
    method = method.gsub(/(^#|:$)/, '').to_sym if method
    value = parse_value(value)
    {
      :key => key,
      :method => method,
      :value => value
    }
  end
  
  # Allows user to enter hashes or array
  def parse_value(value)
    value = value.strip
    # value = "!ruby/regexp \"#{value}\"" if value =~ /^\/.*\/$/
    value.gsub!(/!r(\/.*?\/)/, %Q{!ruby/regexp "\\1"})
    return YAML.load(value)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
whoops-0.2.2 app/models/whoops/mongoid_search_parser.rb
whoops-0.2.1 app/models/whoops/mongoid_search_parser.rb
whoops-0.2 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.10 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.9 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.8 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.7 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.6 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.5 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.4 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.3 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.2 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.1 app/models/whoops/mongoid_search_parser.rb
whoops-0.1.0 app/models/whoops/mongoid_search_parser.rb