Sha256: e654ea7dbb3aad0adc8d114653f0b4aed3b96aa613ecb3496ecc63c250ff25eb

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

require 'active_support/ordered_hash'

module Saga
  class Document
    attr_accessor :title, :introduction, :authors, :stories, :definitions
    
    def initialize
      @title        = ''
      @introduction = []
      @authors      = []
      @stories      = ActiveSupport::OrderedHash.new
      @definitions  = ActiveSupport::OrderedHash.new
    end
    
    def used_ids
      @stories.values.inject([]) do |ids, stories|
        ids.concat stories.map { |story| story[:id] }
        ids
      end.compact
    end
    
    def unused_ids(limit)
      position = 1
      used_ids = used_ids()
      (1..limit).map do
        while used_ids.include?(position) do position += 1 end
        used_ids << position
        position
      end
    end
    
    def length
      stories.inject(0) { |total, (_, stories)| total + stories.length }
    end
    
    def empty?
      length == 0
    end
    
    def autofill_ids
      unused_ids = unused_ids(length - used_ids.length)
      stories.each do |_, stories|
        stories.each do |story|
          story[:id] ||= unused_ids.shift
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
saga-0.7.1 lib/saga/document.rb
saga-0.7.0 lib/saga/document.rb
saga-0.6.0 lib/saga/document.rb
saga-0.5.2 lib/saga/document.rb
saga-0.5.1 lib/saga/document.rb