-1

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
IotaSpencer
  • 77
  • 10
  • `File.write(@file, @data.to_yaml)` is how to write the file. You have global variables like `$notedb` and `$netnotedb`. If you are using globals to work around variable scoping issues, don't, as that's code smell. – the Tin Man Aug 26 '14 at 23:43
  • Well that's put in where I think it should be put in, is there something else that would make this work? I'm not really that good with ruby and yaml writing – IotaSpencer Aug 27 '14 at 00:22
  • 1
    You're being selfish by telling us "it's working" but not posting the answer. If you solve your problem, you're expected to post the solution and mark it accepted to "close" this question. Don't put your own tags like `` into the title. – user229044 Aug 27 '14 at 00:53
  • I apologize, and I can't accept my own answer (at least under 2 days), so if you would, I'd appreciate it. – IotaSpencer Aug 27 '14 at 01:31
  • We can't accept it, only you can. – the Tin Man Aug 27 '14 at 18:16

1 Answers1

0
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.

IotaSpencer
  • 77
  • 10
  • `YAML.dump(@file) + @data.to_yaml`? Ugh. You're creating two separate YAML documents in one file; Don't do that unless you understand why you'd want them. Instead, combine the variables into a hash `{file: @file, data: @data}.to_yaml` or an array `[@file, @data].to_yaml`, then write it. There are subtle differences between what is being output between those and what you're doing. – the Tin Man Aug 27 '14 at 18:33