Module: Squib

Defined in:
lib/squib/deck.rb,
lib/squib.rb,
lib/squib/card.rb,
lib/squib/conf.rb,
lib/squib/version.rb,
lib/squib/api/data.rb,
lib/squib/api/save.rb,
lib/squib/api/text.rb,
lib/squib/args/box.rb,
lib/squib/progress.rb,
lib/squib/api/image.rb,
lib/squib/api/units.rb,
lib/squib/args/draw.rb,
lib/squib/constants.rb,
lib/squib/api/shapes.rb,
lib/squib/args/paint.rb,
lib/squib/args/sheet.rb,
lib/squib/args/coords.rb,
lib/squib/args/import.rb,
lib/squib/api/settings.rb,
lib/squib/commands/new.rb,
lib/squib/graphics/hand.rb,
lib/squib/graphics/text.rb,
lib/squib/layout_parser.rb,
lib/squib/api/background.rb,
lib/squib/api/text_embed.rb,
lib/squib/args/embed_key.rb,
lib/squib/args/paragraph.rb,
lib/squib/args/scale_box.rb,
lib/squib/args/transform.rb,
lib/squib/graphics/image.rb,
lib/squib/sample_helpers.rb,
lib/squib/args/arg_loader.rb,
lib/squib/args/card_range.rb,
lib/squib/args/input_file.rb,
lib/squib/args/save_batch.rb,
lib/squib/graphics/shapes.rb,
lib/squib/args/svg_special.rb,
lib/squib/args/typographer.rb,
lib/squib/args/embed_adjust.rb,
lib/squib/args/hand_special.rb,
lib/squib/graphics/save_doc.rb,
lib/squib/graphics/showcase.rb,
lib/squib/args/dir_validator.rb,
lib/squib/graphics/background.rb,
lib/squib/args/color_validator.rb,
lib/squib/args/unit_conversion.rb,
lib/squib/graphics/save_images.rb,
lib/squib/args/showcase_special.rb,
lib/squib/graphics/gradient_regex.rb,
lib/squib/graphics/cairo_context_wrapper.rb

Overview

The project module

Defined Under Namespace

Modules: Commands Classes: TextEmbed

Constant Summary collapse

VERSION =

The next version to be released. Uses semantic versioning: http://semver.org/

Most of the time this is in the alpha of the next release. e.g. v0.0.5a is on its way to becoming v0.0.5

'0.9.0'
DEFAULT_FONT =

System-wide default font :nodoc:

'Arial 36'

Class Method Summary collapse

Class Method Details

.csv(opts = {}) ⇒ Hash

Pulls CSV data from .csv files into a column-based hash

Pulls the data into a Hash of arrays based on the columns. First row is assumed to be the header row. See the example samples/csv.rb in the source repository

Parsing uses Ruby's CSV, with options {headers: true, converters: :numeric} http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html

Examples:

# File data.csv looks like this (without the comment symbols)
# h1,h2
# 1,2
# 3,4
data = csv file: 'data.csv'
=> {'h1' => [1,3], 'h2' => [2,4]}

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • file (String)

    the CSV-formatted file to open. Opens relative to the current directory.

  • strip (Boolean) — default: true

    When true, strips leading and trailing whitespace on values and headers

  • explode (String) — default: 'qty'

    Quantity explosion will be applied to the column this name. See README for example.

Returns:

  • (Hash)

    a hash of arrays based on columns in the table



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/squib/api/data.rb', line 72

def csv(opts = {})
  file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0]
  import = Args::Import.new.load!(opts)
  table = CSV.read(file, headers: true, converters: :numeric)
  check_duplicate_csv_headers(table)
  hash = Hash.new
  table.headers.each do |header|
    new_header = header.to_s
    new_header = new_header.strip if import.strip?
    hash[new_header] ||= table[header]
  end
  if import.strip?
    new_hash = Hash.new
    hash.each do |header, col|
      new_hash[header] = col.map { |str| str = str.strip if str.respond_to?(:strip); str }
    end
    hash = new_hash
  end
  return explode_quantities(hash, import.explode)
end

.explode_quantities(data, qty) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/squib/api/data.rb', line 104

def explode_quantities(data, qty)
  return data unless data.key? qty.to_s.strip
  qtys = data[qty]
  new_data = {}
  data.each do |col, arr|
    new_data[col] = []
    qtys.each_with_index do |qty, index|
      qty.to_i.times { new_data[col] << arr[index] }
    end
  end
  return new_data
end

.loggerLogger

Access the internal logger that Squib uses. By default, Squib configure the logger to the WARN level Use this to suppress or increase output levels.

Examples:

Squib.logger.level = Logger::DEBUG #show waaaay more information than you probably need, unless you're a dev
Squib.logger.level = Logger::ERROR #basically turns it off

Returns:

  • (Logger)

    the ruby logger



20
21
22
23
24
25
26
27
28
29
# File 'lib/squib.rb', line 20

def logger
  if @logger.nil?
    @logger = Logger.new($stdout)
    @logger.level = Logger::WARN
    @logger.formatter = proc do |severity, datetime, m_progname, msg|
      "#{datetime} #{severity}: #{msg}\n"
    end
  end
  @logger
end

.xlsx(opts = {}) ⇒ Hash

Pulls Excel data from .xlsx files into a column-based hash

Pulls the data into a Hash of arrays based on the columns. First row is assumed to be the header row. See the example samples/excel.rb in the source repository

Examples:

# Excel file looks like this:
# | h1 | h2 |
# ------------
# | 1  | 2  |
# | 3  | 4  |
data = xlsx file: 'data.xlsx', sheet: 0
=> {'h1' => [1,3], 'h2' => [2,4]}

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • file (String)

    the file to open. Must end in .xlsx. Opens relative to the current directory.

  • sheet (Integer) — default: 0

    The zero-based index of the sheet from which to read.

  • strip (Boolean) — default: true

    When true, strips leading and trailing whitespace on values and headers

  • explode (String) — default: 'qty'

    Quantity explosion will be applied to the column this name. See README for example.

Returns:

  • (Hash)

    a hash of arrays based on columns in the spreadsheet



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/squib/api/data.rb', line 28

def xlsx(opts = {})
  input = Args::InputFile.new(file: 'deck.xlsx').load!(opts)
  import = Args::Import.new.load!(opts)
  s = Roo::Excelx.new(input.file[0])
  s.default_sheet = s.sheets[input.sheet[0]]
  data = {}
  s.first_column.upto(s.last_column) do |col|
    header = s.cell(s.first_row,col).to_s
    header.strip! if import.strip?
    data[header] = []
    (s.first_row + 1).upto(s.last_row) do |row|
      cell = s.cell(row,col)
      # Roo hack for avoiding unnecessary .0's on whole integers (https://github.com/roo-rb/roo/issues/139)
      cell = s.excelx_value(row,col) if s.excelx_type(row,col) == [:numeric_or_formula, 'General']
      cell.strip! if cell.respond_to?(:strip) && import.strip?
      cell = yield(header, cell) if block_given?
      data[header] << cell
    end#row
  end#col
  explode_quantities(data, import.explode)
end