Class: Saga::Document
- Inherits:
-
Object
- Object
- Saga::Document
- Defined in:
- lib/saga/document.rb
Instance Attribute Summary collapse
-
#authors ⇒ Object
Returns the value of attribute authors.
-
#definitions ⇒ Object
Returns the value of attribute definitions.
-
#introduction ⇒ Object
Returns the value of attribute introduction.
-
#stories ⇒ Object
Returns the value of attribute stories.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #autofill_ids ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Document
constructor
A new instance of Document.
- #length ⇒ Object
- #unused_ids(limit) ⇒ Object
- #used_ids ⇒ Object
Constructor Details
#initialize ⇒ Document
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
#authors ⇒ Object
Returns the value of attribute authors.
5 6 7 |
# File 'lib/saga/document.rb', line 5 def @authors end |
#definitions ⇒ Object
Returns the value of attribute definitions.
5 6 7 |
# File 'lib/saga/document.rb', line 5 def definitions @definitions end |
#introduction ⇒ Object
Returns the value of attribute introduction.
5 6 7 |
# File 'lib/saga/document.rb', line 5 def introduction @introduction end |
#stories ⇒ Object
Returns the value of attribute stories.
5 6 7 |
# File 'lib/saga/document.rb', line 5 def stories @stories end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/saga/document.rb', line 5 def title @title end |
Instance Method Details
#autofill_ids ⇒ Object
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
36 37 38 |
# File 'lib/saga/document.rb', line 36 def empty? length == 0 end |
#length ⇒ Object
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_ids ⇒ Object
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 |