Sha256: ea81a0d87b4465cf41a219e79fdfbeff0bed07673f9c887d560f77f993247250

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'yaml'
require 'forwardable'

module TomatoHarvest
  class List
    extend ::Forwardable

    attr_reader :items

    def_delegators :items, :count, :map

    def self.init_and_load(*args)
      new(*args).load!
    end

    def initialize(path, items = nil)
      @path  = path
      @items = items || []
    end

    def load!
      if File.exists?(@path) && items = YAML.load_file(@path)
        @items = items
      end
      
      self
    end

    def find(id)
      @items.find do |item|
        item.id == id.to_i
      end
    end

    def save!
      dir = File.dirname(@path)
      FileUtils.mkdir_p(dir) unless File.directory?(dir)

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

      self
    end

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

      @items << item
    end

    def remove(id)
      @items.delete_if do |item|
        item.id == id.to_i
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tomatoharvest-0.1.1 lib/tomatoharvest/list.rb
tomatoharvest-0.1.0 lib/tomatoharvest/list.rb