class RubeePass::Group

Attributes

entries[RW]
group[RW]
groups[RW]
keepass[RW]
name[RW]
path[RW]
uuid[RW]

Public Class Methods

from_xml(keepass, parent, xml) click to toggle source
# File lib/rubeepass/group.rb, line 95
def self.from_xml(keepass, parent, xml)
    name = "/"
    name = xml.elements["Name"].text || "" if (parent)

    notes = ""
    notes = xml.elements["Notes"].text || "" if (parent)

    uuid = ""
    uuid = xml.elements["UUID"].text || "" if (parent)

    group = RubeePass::Group.new(
        parent,
        keepass,
        name,
        notes,
        uuid
    )

    if (xml.elements["Entry"])
        xml.elements.each("Entry") do |entry_xml|
            entry = RubeePass::Entry.from_xml(
                keepass,
                group,
                entry_xml
            )
            group.entries[entry.uuid] = entry
        end
    end

    if (xml.elements["Group"])
        xml.elements.each("Group") do |group_xml|
            child = RubeePass::Group.from_xml(
                keepass,
                group,
                group_xml
            )
            group.groups[child.uuid] = child
        end
    end

    return group
end
new( group, keepass, name, notes, uuid ) click to toggle source
# File lib/rubeepass/group.rb, line 214
def initialize(
    group,
    keepass,
    name,
    notes,
    uuid
)
    @entries = Hash.new
    @group = group
    @groups = Hash.new
    @keepass = keepass
    @name = name
    @notes = notes
    @uuid = uuid

    @path = @name
    @path = "#{@group.path}/#{@name}" if (@group)
    @path.gsub!(%r{^//}, "/")
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/rubeepass/group.rb, line 19
def <=>(other)
    if (self.name.downcase == other.name.downcase)
        return (self.uuid <=> other.uuid)
    end
    return (self.name.downcase <=> other.name.downcase)
end
==(other) click to toggle source
# File lib/rubeepass/group.rb, line 15
def ==(other)
    return (self.uuid == other.uuid)
end
details(level = 0, show_passwd = false) click to toggle source
# File lib/rubeepass/group.rb, line 26
def details(level = 0, show_passwd = false)
    out = Array.new
    lvl = "  " * level

    group_details = [hilight_header(@path)] if (level == 0)
    group_details = [hilight_header(@name)] if (level != 0)

    group_details.each do |line|
        out.push("#{lvl}#{line}")
    end

    @groups.values.each do |group|
        out.push(group.details(level + 1, show_passwd))
    end

    div = "-" * (70 - lvl.length - 2)
    out.push("#{lvl}  #{div}") if (!@entries.empty?)
    @entries.values.each do |entry|
        out.push(entry.details(level + 1, show_passwd))
        out.push("#{lvl}  #{div}")
    end

    return out.join("\n")
end
entries_by_title(title, case_insensitive = false) click to toggle source
# File lib/rubeepass/group.rb, line 63
def entries_by_title(title, case_insensitive = false)
    return @entries.values.select do |entry|
        (entry.title == title) ||
        (
            case_insensitive &&
            (entry.title.downcase == title.downcase)
        )
    end
end
entry_by_uuid(uuid) click to toggle source
# File lib/rubeepass/group.rb, line 51
def entry_by_uuid(uuid)
    return @entries[uuid]
end
entry_titles() click to toggle source
# File lib/rubeepass/group.rb, line 55
def entry_titles
    return @entries.values.map do |entry|
        entry.title
    end.sort do |a, b|
        a.downcase <=> b.downcase
    end
end
find_group(path, case_insensitive = false) click to toggle source
# File lib/rubeepass/group.rb, line 73
def find_group(path, case_insensitive = false)
    return nil if (@keepass.nil?)

    path = @keepass.absolute_path(path, @path)
    cwd = @keepass.db

    path.split("/").each do |group|
        next if (group.empty?)
        if (cwd.has_group_like?(group))
            cwd = cwd.groups_by_name(group, case_insensitive)[0]
        else
            return nil
        end
    end

    return cwd
end
find_group_like(path) click to toggle source
# File lib/rubeepass/group.rb, line 91
def find_group_like(path)
    return find_group(path, true)
end
fuzzy_find(search) click to toggle source
# File lib/rubeepass/group.rb, line 138
def fuzzy_find(search)
    return [Array.new, Array.new] if (@keepass.nil?)

    search = @path if (search.nil? || search.empty?)
    search = @keepass.absolute_path(search, @path)
    path, _, target = search.rpartition("/")

    new_cwd = find_group(path)
    return [Array.new, Array.new] if (new_cwd.nil?)

    if (new_cwd.has_group_like?(target))
        new_cwd = new_cwd.groups_by_name(target, true)[0]
        target = ""
    end

    group_completions = new_cwd.group_names
    entry_completions = new_cwd.entry_titles

    if (target.empty?)
        return [group_completions, entry_completions]
    end

    group_completions.keep_if do |group|
        group.downcase.start_with?(target.downcase)
    end
    entry_completions.keep_if do |entry|
        entry.downcase.start_with?(target.downcase)
    end

    return [group_completions, entry_completions]
end
group_by_uuid(uuid) click to toggle source
# File lib/rubeepass/group.rb, line 170
def group_by_uuid(uuid)
    return @groups[uuid]
end
group_names() click to toggle source
# File lib/rubeepass/group.rb, line 174
def group_names
    return @groups.values.map do |group|
        group.name
    end.sort do |a, b|
        a.downcase <=> b.downcase
    end
end
groups_by_name(name, case_insensitive = false) click to toggle source
# File lib/rubeepass/group.rb, line 182
def groups_by_name(name, case_insensitive = false)
    return @groups.values.select do |group|
        (group.name == name) ||
        (
            case_insensitive &&
            (group.name.downcase == name.downcase)
        )
    end
end
has_entry?(entry) click to toggle source
# File lib/rubeepass/group.rb, line 192
def has_entry?(entry)
    return !entries_by_title(entry).empty?
end
has_entry_like?(entry) click to toggle source
# File lib/rubeepass/group.rb, line 196
def has_entry_like?(entry)
    return !entries_by_title(entry, true).empty?
end
has_group?(group) click to toggle source
# File lib/rubeepass/group.rb, line 200
def has_group?(group)
    return !groups_by_name(group).empty?
end
has_group_like?(group) click to toggle source
# File lib/rubeepass/group.rb, line 204
def has_group_like?(group)
    return !groups_by_name(group, true).empty?
end
to_s() click to toggle source
# File lib/rubeepass/group.rb, line 234
def to_s
    return details
end

Private Instance Methods

hilight_header(header) click to toggle source
# File lib/rubeepass/group.rb, line 208
def hilight_header(header)
    return header if (!RubeePass.hilight?)
    return header.cyan
end