class Forematter::Commands::Search

Public Instance Methods

run() click to toggle source
# File lib/forematter/commands/search.rb, line 18
def run
  files_with(field).sort_by(&:filename).each do |file|
    field_val = file[field].to_ruby
    if field_val.is_a? Array
      next unless field_val.any? { |v| pattern =~ v }
    else
      next unless pattern =~ field_val
    end
    write(file.filename, field_val)
  end
end

Protected Instance Methods

parse_pattern() click to toggle source
# File lib/forematter/commands/search.rb, line 46
def parse_pattern
  opts = options[:'ignore-case'] ? Regexp::IGNORECASE : 0

  if %r{^/(.*)/([im]{0,2})$} =~ value
    val = Regexp.last_match[1]
    opts |= Regexp::IGNORECASE if Regexp.last_match[2].include? 'i'
    opts |= Regexp::MULTILINE  if Regexp.last_match[2].include? 'm'
  else
    val = Regexp.escape(value)
  end

  Regexp.new(val, opts)
end
pattern() click to toggle source
# File lib/forematter/commands/search.rb, line 42
def pattern
  @pattern ||= parse_pattern
end
write(filename, val) click to toggle source
# File lib/forematter/commands/search.rb, line 32
def write(filename, val)
  if options[:print0]
    print "#{filename}\0"
  elsif options[:'files-with-matches']
    puts filename
  else
    puts "#{filename}: #{val.to_json}"
  end
end