Class: Saga::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/saga/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



7
8
9
10
11
12
13
# File 'lib/saga/document.rb', line 7

def initialize
  @title        = ''
  @introduction = []
  @authors      = []
  @stories      = ActiveSupport::OrderedHash.new
  @definitions  = ActiveSupport::OrderedHash.new
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



5
6
7
# File 'lib/saga/document.rb', line 5

def authors
  @authors
end

#definitionsObject

Returns the value of attribute definitions.



5
6
7
# File 'lib/saga/document.rb', line 5

def definitions
  @definitions
end

#introductionObject

Returns the value of attribute introduction.



5
6
7
# File 'lib/saga/document.rb', line 5

def introduction
  @introduction
end

#storiesObject

Returns the value of attribute stories.



5
6
7
# File 'lib/saga/document.rb', line 5

def stories
  @stories
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/saga/document.rb', line 5

def title
  @title
end

Instance Method Details

#autofill_idsObject



40
41
42
43
44
45
46
47
# File 'lib/saga/document.rb', line 40

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

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/saga/document.rb', line 36

def empty?
  length == 0
end

#lengthObject



32
33
34
# File 'lib/saga/document.rb', line 32

def length
  stories.inject(0) { |total, (_, stories)| total + stories.length }
end

#unused_ids(limit) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/saga/document.rb', line 22

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

#used_idsObject



15
16
17
18
19
20
# File 'lib/saga/document.rb', line 15

def used_ids
  @stories.values.inject([]) do |ids, stories|
    ids.concat stories.map { |story| story[:id] }
    ids
  end.compact
end