module Keepassx::Database::Finder
Public Instance Methods
find_entries(opts = {}, &block)
click to toggle source
Get all matching entries.
@return [Array<Keepassx::Entry>]
# File lib/keepassx/database/finder.rb, line 37 def find_entries(opts = {}, &block) find :entry, opts, &block end
find_entry(opts = {}, &block)
click to toggle source
Get the first matching entry.
@return [Keepassx::Entry]
# File lib/keepassx/database/finder.rb, line 10 def find_entry(opts = {}, &block) entries = find_entries(opts, &block) filter_list(entries) end
find_group(opts = {}, &block)
click to toggle source
Get the first matching group.
@return [Keepassx::Group]
# File lib/keepassx/database/finder.rb, line 19 def find_group(opts = {}, &block) groups = find_groups(opts, &block) filter_list(groups) end
find_groups(opts = {}, &block)
click to toggle source
Get all matching groups.
@param opts [Hash] @return [Array<Keepassx::Group>]
# File lib/keepassx/database/finder.rb, line 29 def find_groups(opts = {}, &block) find :group, opts, &block end
search(pattern)
click to toggle source
# File lib/keepassx/database/finder.rb, line 42 def search(pattern) backup = groups.find { |g| g.name == 'Backup' } backup_group_id = backup&.id entries.select { |e| e.group_id != backup_group_id && e.name =~ /#{pattern}/i } end
Private Instance Methods
deep_search(item_list, opts = {})
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/keepassx/database/finder.rb, line 70 def deep_search(item_list, opts = {}) opts = { name: opts.to_s } if opts.is_a?(String) || opts.is_a?(Symbol) match_number = opts.length items = [] opts.each do |k, v| items += Array(item_list.select { |e| e.send(k) == v }) end buffer = Hash.new 0 items.each do |e| buffer[e] += 1 end # Select only items which matches all conditions items = [] buffer.each do |k, v| items << k if v == match_number end items end
filter_list(list)
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/keepassx/database/finder.rb, line 96 def filter_list(list) list.empty? ? nil : list.first end
find(item_type, opts = {}) { |i| ... }
click to toggle source
Search for items, using AND statement for the search conditions
@param item_type [Symbol] Can be :entry or :group. @param opts [Hash] Search options. @return [Keepassx::Group, Keepassx::Entry]
# File lib/keepassx/database/finder.rb, line 57 def find(item_type, opts = {}) item_list = item_type == :entry ? @entries : @groups items = opts.empty? ? item_list : deep_search(item_list, opts) return items unless block_given? items.each do |i| yield i end end