module CultomePlayer::Utils

Public Instance Methods

arrange_in_columns(cols, widths, border) click to toggle source

Arrange an array of string into single string arranged by columns separed by an inner border.

@param cols [List<String>] The strings to be arranged. @param widths [List<Integer>] The width of the columns. @param border [Integer] The width of the inner borders. @return [String] The string representation of columns.

# 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) click to toggle source

Print a string into stdout (not STDOUT) and finish with a newline character.

@param msg [String] The value to be printed. @return [String] The value printed.

# File lib/cultome_player/utils.rb, line 26
def display(msg)
  stdout.puts msg
  return "#{msg}\n"
end
display_over(msg) click to toggle source

Print a string into stdout (not STDOUT) but before insert a carriage return and dont append a newline character at the end.

@param msg [String] The value to be printed. @return [String] The value printed.

# File lib/cultome_player/utils.rb, line 35
def display_over(msg)
  msg = "\r#{msg}"
  stdout.print msg
  return msg
end
ensure_db_schema() click to toggle source
# 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) click to toggle source

Check if a string value can be a positive boolean value.

@param value [String] String to check if can be a positive boolean value. @return [Boolean] True if value is a positive boolean value. False otherwise.

# 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() click to toggle source
# 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.timestamps
          end

          create_table :albums do |t|
            t.string :name
            t.integer :points, default: 0
            t.timestamps
          end

          create_table :artists do |t|
            t.string :name
            t.integer :points, default: 0
            t.timestamps
          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.timestamps
          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() { || ... } click to toggle source

Capture and dispose the standard output sended inside the block provided.

@return [String] The swallowed data.

# 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) click to toggle source
# 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) click to toggle source

Provides a wrapper for database connection.

@param db_block [Block] The block to be executed inside a database connection.

# 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