Module: PrettySearch

Defined in:
lib/pretty_search.rb,
lib/pretty_search/version.rb,
lib/pretty_search/collection.rb,
lib/pretty_search/cli_options.rb,
lib/pretty_search/collection/memory_collection.rb,
lib/pretty_search/collection/indexed_collection.rb

Defined Under Namespace

Classes: Collection, Document, IndexedCollection, MemoryCollection, MissingParameter, Query, SimpleQuery

Constant Summary collapse

VERSION =
'0.1.0'.freeze
HELP_TEXT =
<<-EOF.freeze
pretty_search [OPTION] ... QUERY

Example:
pretty_search --data ./fixtures/users.json name='Raylan Givens'

-h, --help:
 show help

--data FILE, -d FILE:
 the file to search on

--first, -f:
 returns when first match is found. Omit this flag to return all matches.
EOF

Class Method Summary collapse

Class Method Details

.parse_cli_optsHash

Returns like:

help: [bool],
data: [string],

.

Returns:

  • (Hash)

    like:

    help: [bool],
    data: [string],
    



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pretty_search/cli_options.rb', line 11

def parse_cli_opts
  abort 'Ruby 2.0.0 or newer is required' unless defined?(GetoptLong)
  cli_opts = GetoptLong.new(
    ['--help',  '-h', GetoptLong::NO_ARGUMENT],
    ['--first', '-f', GetoptLong::NO_ARGUMENT],
    ['--data',  '-d', GetoptLong::REQUIRED_ARGUMENT]
  )
  options = {}
  cli_opts.each do |option, args|
    # args is "" for options without an argument
    options[option[2..-1].to_sym] = args == '' ? true : args
  end
  options
end

.run(query, data: nil, **options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pretty_search.rb', line 14

def self.run(query, data: nil, **options)
  if data.nil?
    raise MissingParameter, 'Data file is required, please pass in as --data'
  end
  collection = PrettySearch::Collection.load(data, options)
  found = collection.search(query)
  if found.empty?
    'No records found.'
  else
    found.join("\n")
  end
end