Class: AnimateIt::Chapters
- Inherits:
-
Object
- Object
- AnimateIt::Chapters
- Includes:
- Enumerable
- Defined in:
- lib/animate_it/chapters.rb
Instance Method Summary collapse
- #add(name, beat:, label:, metadata: {}) ⇒ Object
- #as_json ⇒ Object
- #each ⇒ Object
- #fetch(name) ⇒ Object
-
#initialize(composition) ⇒ Chapters
constructor
A new instance of Chapters.
- #validate! ⇒ Object
Constructor Details
#initialize(composition) ⇒ Chapters
Returns a new instance of Chapters.
25 26 27 28 |
# File 'lib/animate_it/chapters.rb', line 25 def initialize(composition) @composition = composition @chapters = [] end |
Instance Method Details
#add(name, beat:, label:, metadata: {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/animate_it/chapters.rb', line 30 def add(name, beat:, label:, metadata: {}) key = name.to_sym raise ArgumentError, "AnimateIt chapter names must be unique: #{name.inspect}" if @chapters.any? { |chapter| chapter.name == key } raise ArgumentError, "AnimateIt chapter labels must not be blank" if label.to_s.strip.empty? raise ArgumentError, "AnimateIt chapter metadata must be a hash" unless .is_a?(Hash) JSON.generate() beat_record = @composition.beats.fetch(beat) chapter = Chapter.new( name: key, label: label.to_s, beat_name: beat_record.name, start_frame: beat_record.start_frame, duration_frames: beat_record.duration_frames, metadata: .freeze ) validate_after!(@chapters.last, chapter) validate_bounds!(chapter) @chapters << chapter chapter end |
#as_json ⇒ Object
64 65 66 67 |
# File 'lib/animate_it/chapters.rb', line 64 def as_json(*) validate! @chapters.map(&:as_json) end |
#each ⇒ Object
58 59 60 |
# File 'lib/animate_it/chapters.rb', line 58 def each(&) @chapters.each(&) end |
#fetch(name) ⇒ Object
53 54 55 56 |
# File 'lib/animate_it/chapters.rb', line 53 def fetch(name) @chapters.find { |chapter| chapter.name == name.to_sym } || raise(Error, "Unknown chapter: #{name.inspect}. Declared: #{@chapters.map(&:name).inspect}") end |
#validate! ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/animate_it/chapters.rb', line 69 def validate! @chapters.each_with_index do |chapter, index| validate_after!(@chapters[index - 1], chapter) if index.positive? validate_bounds!(chapter) end self end |