Sha256: c5cab29a5363a0b1d042eb60ff4f2450083f5a0322f63e7d10bcf8caf8d09444

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'yaml'

module TomatoHarvest
  class List
    PATH = File.expand_path("#{ENV['$HOME']}/.toma")

    attr_reader :items

    alias :all :items

    def self.add(item)
      new.tap do |list|
        list.add(item)
        list.save
      end
    end

    def self.all
      new.all
    end

    def self.find(id)
      new.find(id)
    end

    def initialize
      if File.exists?(PATH)
        @items = load_list
      else
        @items = []
      end
    end

    def find(id)
      # TODO speed this up with an algo
      all.find do |item|
        item.id == id.to_i
      end
    end

    def save
      yaml = YAML::dump(@items)
      File.open(PATH, "w+") do |f|
        f.write(yaml)
      end
    end

    def add(item)
      if last_item = @items.last
        id = last_item.id
      else
        id = 0
      end
      item.id = id + 1

      @items << item
    end

    private

    def load_list
      string = ""

      # better way to do this?
      File.open(PATH, "r") do |f|
        while line = f.gets
          string += line
        end
      end

      YAML::load(string)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tomatoharvest-0.0.1 lib/tomatoharvest/list.rb