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.



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

def initialize
  @title        = ''
  @introduction = []
  @authors      = []
  @stories      = {}
  @definitions  = {}
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



3
4
5
# File 'lib/saga/document.rb', line 3

def authors
  @authors
end

#definitionsObject

Returns the value of attribute definitions.



3
4
5
# File 'lib/saga/document.rb', line 3

def definitions
  @definitions
end

#introductionObject

Returns the value of attribute introduction.



3
4
5
# File 'lib/saga/document.rb', line 3

def introduction
  @introduction
end

#storiesObject

Returns the value of attribute stories.



3
4
5
# File 'lib/saga/document.rb', line 3

def stories
  @stories
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/saga/document.rb', line 3

def title
  @title
end

Instance Method Details

#_autofill_ids(stories, unused_ids) ⇒ Object



71
72
73
74
75
76
# File 'lib/saga/document.rb', line 71

def _autofill_ids(stories, unused_ids)
  stories.each do |story|
    story[:id] ||= unused_ids.shift
    _autofill_ids(story[:stories], unused_ids) if story[:stories]
  end
end

#_bindingObject



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

def _binding
  binding
end

#autofill_idsObject



78
79
80
81
82
83
# File 'lib/saga/document.rb', line 78

def autofill_ids
  unused_ids = unused_ids(length - used_ids.length)
  stories.each do |_section, data|
    _autofill_ids(data, unused_ids)
  end
end

#copy_story(story) ⇒ Object



13
14
15
16
17
18
# File 'lib/saga/document.rb', line 13

def copy_story(story)
  copied = {}
  %i[id iteration status estimate description].each do |attribute|
    copied[attribute] = story[attribute] if story[attribute]
  end; copied
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/saga/document.rb', line 67

def empty?
  length == 0
end

#flatten_stories(stories) ⇒ Object



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

def flatten_stories(stories)
  stories_as_flat_list = []
  stories.flatten.each do |story|
    if story[:stories]
      stories_as_flat_list << copy_story(story)
      stories_as_flat_list.concat(story[:stories])
    else
      stories_as_flat_list << story
    end
  end; stories_as_flat_list
end

#lengthObject



63
64
65
# File 'lib/saga/document.rb', line 63

def length
  stories_as_flat_list.length
end

#stories_as_flat_listObject



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

def stories_as_flat_list
  flatten_stories(stories.values)
end

#unused_ids(limit) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/saga/document.rb', line 53

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



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

def used_ids
  @stories.values.each_with_object([]) do |stories, ids|
    stories.each do |story|
      ids << story[:id]
      next unless story[:stories]

      story[:stories].each do |nested|
        ids << nested[:id]
      end
    end
  end.compact
end