lib/keepassx/database.rb in ruby-keepassx-0.2.0beta11 vs lib/keepassx/database.rb in ruby-keepassx-0.2.0

- old
+ new

@@ -48,10 +48,15 @@ end end + # Get raw encoded database. + # + # @param password [String] Password the database will be encoded with. + # @param key_file [String] Path to key file. + # @return [String] def dump password = nil, key_file = nil # FIXME: Figure out what this is needed for # my $e = ($self->find_entries({title => 'Meta-Info', username => 'SYSTEM', comment => 'KPX_GROUP_TREE_STATE', url => '$'}))[0] || $self->add_entry({ # comment => 'KPX_GROUP_TREE_STATE', # title => 'Meta-Info', @@ -71,12 +76,17 @@ header.encode << @encrypted_payload.to_s end - # TODO: Switch to rails style, i.e. save(:password => 'pass') + # Save database to file storage + # + # @param password [String] Password the database will be encoded with. + # @param key_file [String] Path to key file. + # @return [Fixnum] def save password = nil, key_file = nil + # TODO: Switch to rails style, i.e. save(:password => 'pass') fail TypeError, 'File path is not set' if path.nil? File.write path, dump(password, key_file) # FIXME: Implement exceptions rescue IOError => e @@ -87,10 +97,14 @@ fail end # 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] def get item_type, opts = {} case item_type when :entry item_list = @entries @@ -108,11 +122,11 @@ opts = {:title => opts.to_s} if opts.is_a? String or opts.is_a? Symbol match_number = opts.length items = [] opts.each do |k, v| - items += Array(item_list.select { |e| e.send(k).eql?(v) }) + items += Array item_list.select { |e| e.send(k).eql?(v) } end buffer = Hash.new 0 items.each do |e| buffer[e] += 1 @@ -136,11 +150,13 @@ end end - # Get first matching entry + # Get first matching entry. + # + # @return [Keepassx::Entry] def entry opts = {} entries = get :entry, opts if entries.empty? nil else @@ -148,17 +164,21 @@ end end - # Get all matching entries + # Get all matching entries. + # + # @return [Array<Keepassx::Entry>] def entries opts = {} get :entry, opts end - # Get first matching group + # Get first matching group. + # + # @return [Keepassx::Group] def group opts = {} groups = get :group, opts if groups.empty? nil else @@ -166,16 +186,23 @@ end end - # Get all matching groups + # Get all matching groups. + # + # @param opts [Hash] + # @return [Array<Keepassx::Group>] def groups opts = {} get :group, opts end + # Add new item to database. + # + # @param item [Symbol, Keepassx::Group, Keepassx::Entry] New item. + # @return [Keepassx::Group, Keepassx::Entry] def add item, opts = {} if item.is_a? Symbol if item.eql? :group return add_group opts @@ -195,10 +222,14 @@ fail "Could not add '#{item.inspect}'" end end + # Add new group to database. + # + # @param opts [Hash] Options that will be passed to Keepassx::Group#new. + # @return [Keepassx::Group] def add_group opts if opts.is_a? Hash opts = deep_copy opts opts[:id] = next_group_id unless opts.has_key? :id @@ -230,11 +261,16 @@ end end + # Add new entry to database. + # + # @param opts [Hash] Options that will be passed to Keepassx::Entry#new. + # @return [Keepassx::Entry] def add_entry opts + # FIXME: Add warnings and detailed description if opts.is_a? Hash opts = deep_copy opts # FIXME: Remove this feature as it has unpredictable behavior when groups with duplicate title are present if opts[:group].is_a? Symbol @@ -256,15 +292,15 @@ fail TypeError, "Expected Hash or Keepassx::Entry, got #{opts.class}" end end - # Delete item from database + # Delete item from database. # - # @param [Keepassx::Group, Keepassx::Entry, Symbol] item Item to delete. - # @param [Hash] opts If first parameter is a Symbol, then this is used to - # determine which item to delete. + # @param item [Keepassx::Group, Keepassx::Entry, Symbol] Item to delete. + # @param opts [Hash] If first parameter is a Symbol, then this will be + # used to determine which item to delete. def delete item, opts = {} if item.is_a? Keepassx::Group delete_group item elsif item.is_a? Keepassx::Entry @@ -283,15 +319,15 @@ end end - # Unlock database + # Unlock database. # - # @param [String] password Datbase password - # @param [String] key_file Key file path - # @return [Boolean] Whether or not password validation successfull + # @param password [String] Datbase password. + # @param key_file [String] Key file path. + # @return [Boolean] Whether or not password validation successfull. def unlock password, key_file = nil return true unless locked? self.password = password unless password.nil? @@ -308,25 +344,26 @@ rescue Keepassx::MalformedDataError fail end - # FIXME: Seqrch by any atribute by pattern - # Searn entry by title + + # Search entry by title. # - # @param [String] pattern Entry's title to search for + # @param pattern [String] Entry's title to search for. # @return [Keepassx::Entry] def search pattern + # FIXME: Seqrch by any atribute by pattern backup = group 'Backup' entries.select do |e| e.group != backup && e.title =~ /#{pattern}/i end end - # Check database validity + # Check database validity. # # @return [Boolean] def valid? header.valid? end @@ -338,13 +375,13 @@ def locked? @locked end - # Get Group/Entry index in storage + # Get Group/Entry index in storage. # - # @return [Integer] + # @return [Fixnum] def index v if v.is_a? Keepassx::Group groups.find_index v elsif v.is_a? Keepassx::Entry @@ -355,13 +392,13 @@ end end - # Get Enries and Groups total number + # Get Enries and Groups total number. # - # @return [Integer] + # @return [Fixnum] def length length = 0 [@groups, @entries].each do |items| items.each do |item| length += item.length @@ -370,20 +407,20 @@ length end - # Get actual payload checksum + # Get actual payload checksum. # # @return [String] def checksum Digest::SHA256.digest payload end - # Get next group ID number + # Get next group ID number. # - # @return [Integer] + # @return [Fixnum] def next_group_id if groups.empty? # Start each time from 1 to make sure groups get the same id's for the # same input data 1