module Squib

Public Class Methods

card_size(name, landscape: false) click to toggle source
# File lib/squib_plus/card_size.rb, line 28
def card_size(name, landscape: false)
  dimensions = @@card_sizes.fetch name
  dimensions = { width: dimensions[:height], height: dimensions[:width] } if landscape

  { dpi: 300 }.merge dimensions
end
combine_pdfs(sheets, file) click to toggle source

# Combine PnP Squib.combine_pdfs([

{ :front => "_output/front.pdf", :back => "_output/back.pdf" },

], “_output/pnp.pdf”)

# File lib/squib_plus/pdf.rb, line 26
def combine_pdfs(sheets, file)
  pdf = CombinePDF.new

  sheets.each do |sheet|
    front = CombinePDF.load(sheet[:front])

    # Collate the back pages immediately after the relevant page,
    # if a back-side is provided.
    back = CombinePDF.load(sheet[:back]) if sheet.key?(:back)

    i = 0
    front.pages.each do |page|
      pdf << page
      pdf << back.pages[i] if sheet.key?(:back)

      break if i == front.pages.count - 1

      i += 1
    end
  end

  pdf.save file
end
gsheet(sheet_id, index = 0) click to toggle source

gsheet allows you to load Deck data using Google Sheets.

First, share your sheet using File > Publish to the web Select ‘Entire document’ and ‘.csv’ as the format Copy the URL like

https://docs.google.com/spreadsheets/d/e/2PACX-.../pub?output=csv
                                         ^^^^^^^^^
The code you need to pass is the long ID after /d/e.

If the sheet has multiple tabs, you can specify a later tab using the gid from the non-share URL like

https://docs.google.com/spreadsheets/d/.../edit#gid=123456
                                                    ^^^^^^
The gid is at the end of the URL after #gid=

Example:

deck = Squib.gsheet('2PACX-...', 123456)

Squib::Deck.new(cards: deck['name'].size) do
  ...
end

You can optionally specify the tab to use with the second argument

# File lib/squib_plus/google_sheets.rb, line 29
def gsheet(sheet_id, index = 0)
  agent = Mechanize.new
  response = agent.get_file("https://docs.google.com/spreadsheets/d/e/#{sheet_id}/pub?gid=#{index}&output=csv")
  response = response.encode('ascii-8bit').force_encoding('utf-8')

  Squib.csv data: response
end
subset(column, criteria, matching: true) click to toggle source

Subset a selection of cards based on some criteria. Can be passed directly into the range argument of other DSL functions.

Example:

gems = Squib.subset(deck['category'], -> (c) { c === 'gem' })
text range: gems, str: deck['gem_num'], layout: 'gem_label'

You can invert the match using matching: false This allows you to use a single criteria function for positive/negative matches.

# File lib/squib_plus/subset.rb, line 14
def subset(column, criteria, matching: true)
  sub = {}
  column.each_with_index { |c, i| (sub[criteria.call(c)] ||= []) << i }

  sub[matching]
end

Private Instance Methods

card_size(name, landscape: false) click to toggle source
# File lib/squib_plus/card_size.rb, line 28
def card_size(name, landscape: false)
  dimensions = @@card_sizes.fetch name
  dimensions = { width: dimensions[:height], height: dimensions[:width] } if landscape

  { dpi: 300 }.merge dimensions
end
combine_pdfs(sheets, file) click to toggle source

# Combine PnP Squib.combine_pdfs([

{ :front => "_output/front.pdf", :back => "_output/back.pdf" },

], “_output/pnp.pdf”)

# File lib/squib_plus/pdf.rb, line 26
def combine_pdfs(sheets, file)
  pdf = CombinePDF.new

  sheets.each do |sheet|
    front = CombinePDF.load(sheet[:front])

    # Collate the back pages immediately after the relevant page,
    # if a back-side is provided.
    back = CombinePDF.load(sheet[:back]) if sheet.key?(:back)

    i = 0
    front.pages.each do |page|
      pdf << page
      pdf << back.pages[i] if sheet.key?(:back)

      break if i == front.pages.count - 1

      i += 1
    end
  end

  pdf.save file
end
gsheet(sheet_id, index = 0) click to toggle source

gsheet allows you to load Deck data using Google Sheets.

First, share your sheet using File > Publish to the web Select ‘Entire document’ and ‘.csv’ as the format Copy the URL like

https://docs.google.com/spreadsheets/d/e/2PACX-.../pub?output=csv
                                         ^^^^^^^^^
The code you need to pass is the long ID after /d/e.

If the sheet has multiple tabs, you can specify a later tab using the gid from the non-share URL like

https://docs.google.com/spreadsheets/d/.../edit#gid=123456
                                                    ^^^^^^
The gid is at the end of the URL after #gid=

Example:

deck = Squib.gsheet('2PACX-...', 123456)

Squib::Deck.new(cards: deck['name'].size) do
  ...
end

You can optionally specify the tab to use with the second argument

# File lib/squib_plus/google_sheets.rb, line 29
def gsheet(sheet_id, index = 0)
  agent = Mechanize.new
  response = agent.get_file("https://docs.google.com/spreadsheets/d/e/#{sheet_id}/pub?gid=#{index}&output=csv")
  response = response.encode('ascii-8bit').force_encoding('utf-8')

  Squib.csv data: response
end
subset(column, criteria, matching: true) click to toggle source

Subset a selection of cards based on some criteria. Can be passed directly into the range argument of other DSL functions.

Example:

gems = Squib.subset(deck['category'], -> (c) { c === 'gem' })
text range: gems, str: deck['gem_num'], layout: 'gem_label'

You can invert the match using matching: false This allows you to use a single criteria function for positive/negative matches.

# File lib/squib_plus/subset.rb, line 14
def subset(column, criteria, matching: true)
  sub = {}
  column.each_with_index { |c, i| (sub[criteria.call(c)] ||= []) << i }

  sub[matching]
end