Browse Source

samples: gistifying excel.rb and csv.rb

dev
Andy Meneely 10 years ago
parent
commit
8cd651d5f1
  1. 4
      .gitmodules
  2. 33
      samples/csv_import.rb
  3. 1
      samples/data
  4. 55
      samples/excel.rb
  5. BIN
      samples/explode_quantities.xlsx
  6. 3
      samples/quantity_explosion.csv
  7. 3
      samples/sample.csv
  8. BIN
      samples/sample.xlsx
  9. 0
      spec/data/samples/data/_csv.rb.txt
  10. 0
      spec/data/samples/data/_excel.rb.txt
  11. 8
      spec/data/samples/images/_more_load_images.rb.txt
  12. 17
      spec/samples/samples_regression_spec.rb

4
.gitmodules vendored

@ -27,3 +27,7 @@
[submodule "samples/saves"] [submodule "samples/saves"]
path = samples/saves path = samples/saves
url = https://gist.github.com/3c60c13b25f525abd037db4055abf35c.git url = https://gist.github.com/3c60c13b25f525abd037db4055abf35c.git
[submodule "samples/data"]
path = samples/data
url = https://gist.github.com/6f89115805e205c647a779b7b4866151.git

33
samples/csv_import.rb

@ -1,33 +0,0 @@
require 'squib'
Squib::Deck.new(cards: 2) do
background color: :white
# Outputs a hash of arrays with the header names as keys
data = csv file: 'sample.csv'
text str: data['Type'], x: 250, y: 55, font: 'Arial 54'
text str: data['Level'], x: 65, y: 65, font: 'Arial 72'
save format: :png, prefix: 'sample_csv_'
# You can also specify the sheet, starting at 0
data = xlsx file: 'sample.xlsx', sheet: 2
end
# CSV is also a Squib-module-level function, so this also works:
data = Squib.csv file: 'quantity_explosion.csv' # 2 rows...
num_cards = data['Name'].size # ...but 4 cards!
Squib::Deck.new(cards: num_cards) do
background color: :white
rect # card border
text str: data['Name'], font: 'Arial 54'
save_sheet prefix: 'sample_csv_qty_', columns: 4
end
# Additionally, CSV supports inline data specifically
data = Squib.csv data: <<-EOCSV
Name,Cost
Knight,3
Orc,1
EOCSV

1
samples/data

@ -0,0 +1 @@
Subproject commit d9d087ebf415698bb81605c042b4b7e84bcdcb52

55
samples/excel.rb

@ -1,55 +0,0 @@
require 'squib'
Squib::Deck.new(cards: 3) do
background color: :white
# Reads the first sheet by default (sheet 0)
# Outputs a hash of arrays with the header names as keys
data = xlsx file: 'sample.xlsx'
text str: data['Name'], x: 250, y: 55, font: 'Arial 54'
text str: data['Level'], x: 65, y: 65, font: 'Arial 72'
text str: data['Description'], x: 65, y: 600, font: 'Arial 36'
save format: :png, prefix: 'sample_excel_' # save to individual pngs
end
# xlsx is also a Squib-module-level function, so this also works:
data = Squib.xlsx file: 'explode_quantities.xlsx' # 2 rows...
num_cards = data['Name'].size # ...but 4 cards!
Squib::Deck.new(cards: num_cards) do
background color: :white
rect # card border
text str: data['Name'], font: 'Arial 54'
save_sheet prefix: 'sample_xlsx_qty_', columns: 4
end
# Here's another example, a bit more realistic. Here's what's going on:
# * We call xlsx from Squib directly - BEFORE Squib::Deck creation. This
# allows us to infer the number of cards based on the size of the "Name"
# field
# * We make use of quantity explosion. Fields named "Qty" or "Quantity"
# (any capitalization), or any other in the "qty_header" get expanded by the
# number given
# * We also make sure that trailing and leading whitespace is stripped
# from each value. This is the default behavior in Squib, but the options
# are here just to make sure.
resource_data = Squib.xlsx(file: 'sample.xlsx', sheet: 2, strip: true) do |header, value|
case header
when 'Cost'
"$#{value}k" # e.g. "3" becomes "$3k"
else
value # always return the original value if you didn't do anything to it
end
end
Squib::Deck.new(cards: resource_data['Name'].size) do
background color: :white
rect width: :deck, height: :deck
text str: resource_data['Name'], align: :center, width: :deck, hint: 'red'
text str: resource_data['Cost'], align: :right, width: :deck, hint: 'red'
save_sheet prefix: 'sample_excel_resources_' # save to a whole sheet
end

BIN
samples/explode_quantities.xlsx

Binary file not shown.

3
samples/quantity_explosion.csv

@ -1,3 +0,0 @@
Name,Qty
Basilisk,3
High Templar,1
1 Name Qty
2 Basilisk 3
3 High Templar 1

3
samples/sample.csv

@ -1,3 +0,0 @@
Type,"Level"
Thief,1
Mastermind,2
1 Type Level
2 Thief 1
3 Mastermind 2

BIN
samples/sample.xlsx

Binary file not shown.

0
spec/data/samples/csv_import.rb.txt → spec/data/samples/data/_csv.rb.txt

0
spec/data/samples/excel.rb.txt → spec/data/samples/data/_excel.rb.txt

8
spec/data/samples/images/_more_load_images.rb.txt

@ -108,7 +108,7 @@ cairo: scale([1.0, 1.0])
cairo: rotate([0]) cairo: rotate([0])
cairo: transform([Matrix]) cairo: transform([Matrix])
cairo: translate([-300, -350]) cairo: translate([-300, -350])
cairo: rounded_rectangle([300, 350, 128.0, 64.0, 0, 0]) cairo: rounded_rectangle([300, 350, 200.0, 100.0, 0, 0])
cairo: clip([]) cairo: clip([])
cairo: translate([0, 0]) cairo: translate([0, 0])
cairo: set_source([ImageSurface, 300, 350]) cairo: set_source([ImageSurface, 300, 350])
@ -166,7 +166,7 @@ cairo: scale([1.0, 1.0])
cairo: rotate([0]) cairo: rotate([0])
cairo: transform([Matrix]) cairo: transform([Matrix])
cairo: translate([-300, -535]) cairo: translate([-300, -535])
cairo: rounded_rectangle([300, 535, 128.0, 64.0, 0, 0]) cairo: rounded_rectangle([300, 535, 200.0, 100.0, 0, 0])
cairo: clip([]) cairo: clip([])
cairo: translate([0, 0]) cairo: translate([0, 0])
cairo: set_source([ImageSurface, 300, 535]) cairo: set_source([ImageSurface, 300, 535])
@ -216,8 +216,8 @@ cairo: save([])
cairo: translate([0, 0]) cairo: translate([0, 0])
cairo: transform([Matrix]) cairo: transform([Matrix])
cairo: rotate([0]) cairo: rotate([0])
cairo: scale([0.8021390374331551, 0.8032128514056225]) cairo: scale([6.0, 6.0])
cairo: rounded_rectangle([0, 0, 748.0, 747.0, 0, 0]) cairo: rounded_rectangle([0, 0, 100.0, 100.0, 0, 0])
cairo: clip([]) cairo: clip([])
cairo: translate([0, 0]) cairo: translate([0, 0])
cairo: render_rsvg_handle([RSVG::Handle, "#thing"]) cairo: render_rsvg_handle([RSVG::Handle, "#thing"])

17
spec/samples/samples_regression_spec.rb

@ -4,12 +4,6 @@ require 'pp'
describe 'Squib samples' do describe 'Squib samples' do
around(:each) do |example|
Dir.chdir(samples_dir) do
example.run
end
end
# This test could use some explanation # This test could use some explanation
# Much of the development of Squib has been sample-driven. Every time I want # Much of the development of Squib has been sample-driven. Every time I want
# new syntax or feature, I write a sample, get it working, and then write # new syntax or feature, I write a sample, get it working, and then write
@ -42,12 +36,12 @@ describe 'Squib samples' do
autoscale_font/_autoscale_font.rb autoscale_font/_autoscale_font.rb
basic.rb basic.rb
cairo_access.rb cairo_access.rb
csv_import.rb data/_csv.rb
config_text_markup.rb config_text_markup.rb
custom_config.rb custom_config.rb
shapes/_draw_shapes.rb shapes/_draw_shapes.rb
embed_text.rb embed_text.rb
excel.rb data/_excel.rb
gradients.rb gradients.rb
saves/_hand.rb saves/_hand.rb
hello_world.rb hello_world.rb
@ -63,8 +57,11 @@ describe 'Squib samples' do
it "has not changed for #{sample}", slow: true do it "has not changed for #{sample}", slow: true do
log = StringIO.new log = StringIO.new
mock_cairo(log) mock_cairo(log)
load sample full_sample_path = File.expand_path "#{samples_dir}/#{sample}"
overwrite_sample(sample, log) # Use TEMPORARILY once happy with the new sample log Dir.chdir(File.dirname("#{samples_dir}/#{sample}")) do
load full_sample_path
end
# overwrite_sample(sample, log) # Use TEMPORARILY once happy with the new sample log
test_file_str = File.open(sample_regression_file(sample), 'r:UTF-8').read test_file_str = File.open(sample_regression_file(sample), 'r:UTF-8').read
expect(log.string).to eq(test_file_str) expect(log.string).to eq(test_file_str)
end end

Loading…
Cancel
Save