Module: CultomePlayer::Utils
- Included in:
- CultomePlayer, Objects::Album, Objects::Artist, Objects::Drive, Objects::Genre, Objects::Parameter, Objects::Song, Player::Playlist::Playlists
- Defined in:
- lib/cultome_player/utils.rb
Instance Method Summary collapse
-
#arrange_in_columns(cols, widths, border) ⇒ String
Arrange an array of string into single string arranged by columns separed by an inner border.
-
#display(msg) ⇒ String
Print a string into stdout (not STDOUT) and finish with a newline character.
-
#display_over(msg) ⇒ String
Print a string into stdout (not STDOUT) but before insert a carriage return and dont append a newline character at the end.
- #ensure_db_schema ⇒ Object
-
#is_true_value?(value) ⇒ Boolean
Check if a string value can be a positive boolean value.
- #recreate_db_schema ⇒ Object
-
#swallow_stdout ⇒ String
Capture and dispose the standard output sended inside the block provided.
- #to_display_list(list) ⇒ Object
-
#with_connection(&db_logic) ⇒ Object
Provides a wrapper for database connection.
Instance Method Details
#arrange_in_columns(cols, widths, border) ⇒ String
Arrange an array of string into single string arranged by columns separed by an inner border.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cultome_player/utils.rb', line 73 def arrange_in_columns(cols, widths, border) row = "" idxs = cols.collect{|c| 0 } while cols.zip(idxs).any?{|col| col[0].length > col[1] } cols.each.with_index do |col, idx| slice_width = widths[idx] slice = col.slice(idxs[idx], slice_width) || "" # sacamos el pedazo de la columna row << slice.ljust(slice_width) # concatenamos a la fila idxs[idx] += slice_width # recorremos el indice row << " " * border # agregamos el border de la derecha end row = row.strip << "\n" # quitamos el ultimo border end return row.strip # quitamos el ultimo salto de linea end |
#display(msg) ⇒ String
Print a string into stdout (not STDOUT) and finish with a newline character.
26 27 28 29 |
# File 'lib/cultome_player/utils.rb', line 26 def display(msg) stdout.puts msg return "#{msg}\n" end |
#display_over(msg) ⇒ String
Print a string into stdout (not STDOUT) but before insert a carriage return and dont append a newline character at the end.
35 36 37 38 39 |
# File 'lib/cultome_player/utils.rb', line 35 def display_over(msg) msg = "\r#{msg}" stdout.print msg return msg end |
#ensure_db_schema ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/cultome_player/utils.rb', line 123 def ensure_db_schema begin # hacemos una simple query a la base de datos para verificar with_connection{ CultomePlayer::Objects::Song.first } rescue # si la query no funciona recreamos el esquema recreate_db_schema end end |
#is_true_value?(value) ⇒ Boolean
Check if a string value can be a positive boolean value.
18 19 20 |
# File 'lib/cultome_player/utils.rb', line 18 def is_true_value?(value) /true|yes|on|y|n|s|si|cierto/ === value end |
#recreate_db_schema ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/cultome_player/utils.rb', line 133 def recreate_db_schema with_connection do swallow_stdout do begin ActiveRecord::Schema.drop_table('songs') ActiveRecord::Schema.drop_table('albums') ActiveRecord::Schema.drop_table('artists') ActiveRecord::Schema.drop_table('genres') ActiveRecord::Schema.drop_table('genres_songs') ActiveRecord::Schema.drop_table('drives') rescue end begin ActiveRecord::Schema.define do create_table :songs do |t| t.string :name # If I Had A Gun t.integer :artist_id, default: 0 # Noel Gallagher t.integer :album_id, default: 0 # High Flying Birds t.integer :year # 2011 t.integer :track # 3 t.integer :duration # 210 sec t.integer :drive_id t.string :relative_path t.integer :points, default: 0 t.integer :plays, default: 0 t.datetime :last_played_at t. end create_table :albums do |t| t.string :name t.integer :points, default: 0 t. end create_table :artists do |t| t.string :name t.integer :points, default: 0 t. end create_table :genres do |t| t.integer :points, default: 0 t.string :name end create_table :genres_songs, id: false do |t| t.integer :song_id t.integer :genre_id end create_table :drives do |t| t.string :name t.string :path t.boolean :connected, default: true t. end end rescue end # begin end # swallow_stdout # Default and required values CultomePlayer::Objects::Album.find_or_create_by(id: 0, name: 'Unknown') CultomePlayer::Objects::Artist.find_or_create_by(id: 0, name: 'Unknown') return true end end |
#swallow_stdout ⇒ String
Capture and dispose the standard output sended inside the block provided.
96 97 98 99 100 101 102 103 104 |
# File 'lib/cultome_player/utils.rb', line 96 def swallow_stdout s = StringIO.new oldstd = $stdout $stdout = s yield return s.string ensure $stdout = oldstd end |
#to_display_list(list) ⇒ Object
8 9 10 11 12 |
# File 'lib/cultome_player/utils.rb', line 8 def to_display_list(list) return list.collect.with_index do |elem, idx| c4("#{(idx + 1).to_s.ljust(3)} | ") + elem.to_s end.join("\n") end |
#with_connection(&db_logic) ⇒ Object
Provides a wrapper for database connection.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cultome_player/utils.rb', line 109 def with_connection(&db_logic) begin ActiveRecord::Base.connection_pool rescue Exception => e ActiveRecord::Base.establish_connection( adapter: db_adapter, database: db_file ) ActiveRecord::Base.logger = Logger.new(File.open(db_log_file, 'a')) end ActiveRecord::Base.connection_pool.with_connection(&db_logic) end |