Search code examples
rubyyamlirccinch

How would I go about fixing this?


I'm using the following to try and write to a yaml file

class NoteDB
    attr_reader :data

    def initialize(file)
        @file = file
        if File.exists?(@file)
            @data = YAML.load_file(@file)
        else
            puts "Warning: note db #{@file} does not exist. Skipping loading."
            @data = Hash.new
        end
    end
    def data=(newdata)
        @data = newdata
        #f = File.open(@file, 'w')
        #YAML.dump(@data, f)
        File.write(@file, @data.to_yaml)
        #f.close
    end
end

This next bits are the setting methods for each command

class NotePlugin 
    include Cinch::Plugin
    def set_netnote(m, network, note)
        return unless m.channel == "#channel"
        data = $netnotedb.data
        network.downcase!
        data[network] = note
        m.reply "Note for #{network} has been set."
        $netnotedb.data = data
    end
    def add_cat(m, category)
        return unless m.channel == "#channel"
        category.downcase!
        data = $notedb.data
        if data.has_key? category
            m.reply "#{Format(:bold, "Error:")} Category \"#{category}\" exists."
        else
            data[category] = Array.new
            $notedb.data = data
            m.reply "Category \"#{category}\" added."
        end
    end
    def del_cat(m, category)
        return unless m.channel == "#channel"
        category.downcase!
        data = $notedb.data
        if data.has_key? category
            data.delete category
            $notedb.data = data
            m.reply "Category \"#{category}\" removed."
        else
            m.reply "#{Format(:bold, "Error:")} Category \"#{category}\" does not exist."
        end
    end
    def add_item(m, cat, item)
        return unless m.channel == "#channel"
        cat.downcase!
        data = $notedb.data
        if data.has_key? cat
            data[cat] << item
            $notedb.data = data
            m.reply "Item added."
        else
            m.reply "#{Format(:bold, "Error:")} Category \"#{cat}\" does not exist."
        end
    end
    def del_item(m, cat, index)
        return unless m.channel == "#channel"
        cat.downcase!
        index = index.to_i - 1
        data = $notedb.data
        if data.has_key? cat
            if data[cat][index].nil?
                m.reply "#{Format(:bold, "Error:")} Item ##{index + 1} not found."
            else
                data[cat].delete data[cat][index]
                $notedb.data = data
                m.reply "Deleted item ##{index + 1}."
            end
        else
            m.reply "#{Format(:bold, "Error:")} Category \"#{cat}\" does not exist."
        end
    end
end

The plugin will either error with []= being undefined, or will exit without no error or action whatsoever.

Someone also said to use the block type of write, and I tried that but I still can't seem to get it to work.

get_netnote works, the rest do not, and I can't seem to figure out why.


Solution

  • class NoteDB
        attr_reader :data
        def initialize(file)
            @file = file
            if File.exists?(@file)
                @data = YAML.load_file(@file)
            else
                puts "Warning: note db #{@file} does not exist. Skipping loading."
                @data = Hash.new
            end
        end
        def data=(newdata)
            @data = newdata
            #f = File.open(@file, 'w')
            #File.write(@file, @data.to_yaml)
            File.open(@file,'w') do |f|
                f.write YAML.dump(@file) + @data.to_yaml
            end
        end
    end
    

    f.write YAML.dump(@file) + @data.to_yaml was the kicker in this, where @file is the decorator/variable to your yaml file, dump your file, add to it with @data into yaml, and write it.