feature: add use_layout method to load layouts at runtime

Closes #141
dev
Andy Meneely 2016-03-16 17:02:52 -04:00
parent b74fbf5787
commit 955d64a01f
5 changed files with 47 additions and 3 deletions

16
docs/dsl/use_layout.rst Normal file
View File

@ -0,0 +1,16 @@
use_layout
==========
Load a layout file and merge into the current set of layouts.
Options
-------
file
default: ``'layout.yml'``
The file or array of files to load. Treated exactly how :doc:`/dsl/deck` parses it.
Examples
--------

View File

@ -31,5 +31,11 @@ module Squib
@font = (opts[:font] == :default) ? Squib::DEFAULT_FONT: opts[:font]
end
# Load a new layout into the deck
# @api public
def use_layout(file: 'layout.yml')
@layout = LayoutParser.load_layout(file, @layout)
end
end
end

View File

@ -7,8 +7,8 @@ module Squib
# Load the layout file(s), if exists
# @api private
def self.load_layout(files)
layout = {}
def self.load_layout(files, initial = {})
layout = initial
Squib::logger.info { " using layout(s): #{files}" }
Array(files).each do |file|
thefile = file

View File

@ -60,3 +60,13 @@ Squib::Deck.new(layout: 'hand.yml') do
png file: 'pokercard.png', alpha: 0.5
save_png prefix: 'layout_builtin_hand_'
end
# Layouts can also be specified in their own DSL method call
# Each layout call will progressively be merged with the priors
Squib::Deck.new do
use_layout file: 'custom-layout.yml'
use_layout file: 'custom-layout2.yml'
text str: 'The Title', layout: :title # from custom-layout.yml
text str: 'The Subtitle',layout: :subtitle # redefined in custom-layout2.yml
save_png prefix: 'layout3_'
end

View File

@ -186,4 +186,16 @@ describe Squib::LayoutParser do
})
end
it 'loads progressively on multiple calls' do
a = layout_file('multifile-a.yml')
b = layout_file('multifile-b.yml')
layout = Squib::LayoutParser.load_layout(a)
layout = Squib::LayoutParser.load_layout(b, layout)
expect(layout).to eq({
'title' => { 'x' => 300 },
'subtitle' => { 'x' => 200 },
'desc' => { 'x' => 400 }
})
end
end