Class: RubeePass::Group

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubeepass/group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, keepass, name, notes, uuid) ⇒ Group

Returns a new instance of Group.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rubeepass/group.rb', line 171

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

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



7
8
9
# File 'lib/rubeepass/group.rb', line 7

def entries
  @entries
end

#groupObject

Returns the value of attribute group.



8
9
10
# File 'lib/rubeepass/group.rb', line 8

def group
  @group
end

#groupsObject

Returns the value of attribute groups.



9
10
11
# File 'lib/rubeepass/group.rb', line 9

def groups
  @groups
end

#keepassObject

Returns the value of attribute keepass.



10
11
12
# File 'lib/rubeepass/group.rb', line 10

def keepass
  @keepass
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/rubeepass/group.rb', line 11

def name
  @name
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/rubeepass/group.rb', line 12

def path
  @path
end

#uuidObject

Returns the value of attribute uuid.



13
14
15
# File 'lib/rubeepass/group.rb', line 13

def uuid
  @uuid
end

Class Method Details

.from_xml(keepass, parent, xml) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rubeepass/group.rb', line 72

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.title] = 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.name] = child
        end
    end

    return group
end

Instance Method Details

#<=>(other) ⇒ Object



19
20
21
# File 'lib/rubeepass/group.rb', line 19

def <=>(other)
    return (self.name.downcase <=> other.name.downcase)
end

#==(other) ⇒ Object



15
16
17
# File 'lib/rubeepass/group.rb', line 15

def ==(other)
    return (self.uuid == other.uuid)
end

#details(level = 0, show_passwd = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubeepass/group.rb', line 23

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

#entry_titlesObject



48
49
50
51
52
# File 'lib/rubeepass/group.rb', line 48

def entry_titles
    return @entries.keys.sort do |a, b|
        a.downcase <=> b.downcase
    end
end

#find_group(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubeepass/group.rb', line 54

def find_group(path)
    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?(group))
            cwd = cwd.groups[group]
        else
            return nil
        end
    end

    return cwd
end

#fuzzy_find(search) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubeepass/group.rb', line 115

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, found, target = search.rpartition("/")

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

    if (new_cwd.has_group?(target))
        new_cwd = new_cwd.groups[target]
        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_namesObject



147
148
149
150
151
# File 'lib/rubeepass/group.rb', line 147

def group_names
    return @groups.keys.sort do |a, b|
        a.downcase <=> b.downcase
    end
end

#has_entry?(title) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
# File 'lib/rubeepass/group.rb', line 153

def has_entry?(title)
    return entry_titles.any? do |entry|
        entry.downcase == title.downcase
    end
end

#has_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/rubeepass/group.rb', line 159

def has_group?(name)
    return group_names.any? do |group|
        group.downcase == name.downcase
    end
end

#to_sObject



191
192
193
# File 'lib/rubeepass/group.rb', line 191

def to_s
    return details
end