Sha256: d8f4fe3f4b58fbda9d08e4d52bb0ad5465adeca9ce12cd7463d7f6ea654e8bfb

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'json'
require 'fileutils'
require 'open3'
require 'arli'
require 'arli/commands/base'
require 'arduino/library'
require 'awesome_print'
module Arli
  module Commands
    class Search < Base
      require 'arduino/library/include'

      attr_accessor :search_string,
                    :search_opts,
                    :limit,
                    :database

      class InvalidOptionError < ArgumentError; end

      def initialize(options)
        super(options)
        self.search_string = options[:search]
        self.limit         = options[:limit] || 100

        raise InvalidOptionError, 'Please provide search string with --search' \
          unless search_string

        begin
          self.search_opts = eval("{ #{search_string} }")
        rescue => e
          raise InvalidOptionError "Search string '#{search_string}' is invalid.\n" +
            e.message.red
        end

        unless search_opts.is_a?(::Hash) && search_opts.size > 0
          raise InvalidOptionError, "Search string '#{search_string}' did not eval to Hash.\n"
        end

        self.database = options[:database] ? db_from(option[:database]) : db_default

        search_opts.merge!(limit: limit) if limit
      end

      def run
        ap search(database, **search_opts).map(&:to_hash)
      rescue Exception => e
        error e
        puts e.backtrace.join("\n") if ENV['DEBUG']
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arli-0.3.2 lib/arli/commands/search.rb
arli-0.3.1 lib/arli/commands/search.rb