Class: LocalVault::GroupCatalog

Inherits:
Object
  • Object
show all
Defined in:
lib/localvault/group_catalog.rb

Defined Under Namespace

Classes: Entry, Group, Match

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secrets) ⇒ GroupCatalog

Returns a new instance of GroupCatalog.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/localvault/group_catalog.rb', line 17

def initialize(secrets)
  grouped = {}

  secrets.each do |name, value|
    if value.is_a?(Hash)
      append_namespace(grouped, name, value)
    elsif name.include?("_")
      append_entry(grouped, name.split("_", 2).first, Entry.new(name, value, :prefix))
    end
  end

  @groups = grouped.map do |name, entries|
    kinds = entries.map(&:source_kind).uniq
    kind = kinds.length > 1 ? :mixed : kinds.first
    Group.new(name, kind, entries.sort_by(&:label).freeze)
  end.sort_by { |group| [group.name.downcase, group.name] }.freeze
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



15
16
17
# File 'lib/localvault/group_catalog.rb', line 15

def groups
  @groups
end

Instance Method Details

#resolve(query) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/localvault/group_catalog.rb', line 42

def resolve(query)
  exact = groups.select { |group| group.name == query }
  return Match.new(:exact, query, exact) if exact.one?

  insensitive = groups.select { |group| group.name.casecmp?(query) }
  return Match.new(:unique, query, insensitive) if insensitive.one?
  return Match.new(:ambiguous, query, insensitive) if insensitive.length > 1

  matches = search(query)
  kind = matches.one? ? :unique : (matches.empty? ? :absent : :ambiguous)
  Match.new(kind, query, matches)
end

#search(query = nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/localvault/group_catalog.rb', line 35

def search(query = nil)
  return groups if query.nil? || query.empty?

  needle = query.downcase
  groups.select { |group| group.name.downcase.start_with?(needle) }
end