m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/lib/song.rb
diff options
context:
space:
mode:
authorMarcin Chrzanowski <m@m-chrzan.xyz>2021-09-12 14:17:19 +0200
committerMarcin Chrzanowski <m@m-chrzan.xyz>2021-09-12 14:17:19 +0200
commitf3039744d72454e7e3b8cb1032266bcc172ce488 (patch)
tree5a92a698056e2cbec6f0b121a9ce31596e4a4581 /lib/song.rb
Initial commit
Diffstat (limited to 'lib/song.rb')
-rw-r--r--lib/song.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/song.rb b/lib/song.rb
new file mode 100644
index 0000000..4076956
--- /dev/null
+++ b/lib/song.rb
@@ -0,0 +1,47 @@
+require 'yaml'
+
+require './lib/recording'
+
+class Song
+ attr_accessor :title, :id, :lyrics, :recordings
+ def initialize title, id
+ @title = title
+ @id = id
+ @recordings = []
+ end
+
+ def self.from_yaml song_id
+ metadata = YAML.load(File.read(src(song_meta song_id)))
+ song = Song.new metadata['title'], song_id
+
+ song.lyrics = nil
+ lyrics_file = src (song_lyrics song_id)
+ if File.file? lyrics_file
+ song.lyrics = File.read lyrics_file
+ end
+
+ if metadata['recordings']
+ metadata['recordings'].each do |recording|
+ if recording['type'] == 'youtube'
+ song.recordings.push YouTubeRecording.new(recording['link'])
+ elsif recording['type'] == 'bandcamp'
+ song.recordings.push BandcampRecording.new(recording['link'])
+ end
+ end
+ end
+ song
+ end
+
+ def split_lyrics
+ @lyrics.split("\n\n").map { |paragraph| paragraph.split "\n" }
+ end
+
+ def render_sheet_music
+ if File.file? (src (song_ly id))
+ template = ERB.new(File.read('templates/sheet_music.html.erb'), trim_mode: '-')
+ template.result binding
+ else
+ ""
+ end
+ end
+end