Sha256: 6a38d392574fece8f2b7fa2ed6ed14ecf992bcfe0c83c1c25fbf79c3bd817201

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module Meibo
  class DataSet
    attr_reader :roster

    def initialize(data, roster:)
      @data = data
      @data_hash = data.to_h {|datum| [datum.sourced_id, datum] }
      @roster = roster
    end

    def <<(new_data)
      raise DataNotFoundError, "sourcedIdがありません" unless new_data.sourced_id
      raise SourcedIdDuplicatedError, 'sourcedIdが重複しています' if @data_hash.key?(new_data.sourced_id)

      @data << new_data
      @data_hash[new_data.sourced_id] = new_data
    end

    def check_semantically_consistent
      unless @data.size == @data_hash.size
        raise SourcedIdDuplicatedError, 'sourcedIdが重複しています'
      end

      unless @data_hash[nil].nil?
        raise DataNotFoundError, "sourcedIdがありません"
      end
    end

    def each(...)
      @data.each(...)
    end

    def empty?
      @data.empty?
    end

    def find(sourced_id)
      @data_hash.fetch(sourced_id)
    rescue KeyError
      raise DataNotFoundError, "sourcedId: #{sourced_id} が見つかりません"
    end

    def to_a
      @data
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
meibo-0.3.0 lib/meibo/data_set.rb
meibo-0.2.0 lib/meibo/data_set.rb