Browse Source

feature: add use_layout method to load layouts at runtime

Closes #141
dev
Andy Meneely 10 years ago
parent
commit
955d64a01f
  1. 16
      docs/dsl/use_layout.rst
  2. 6
      lib/squib/api/settings.rb
  3. 4
      lib/squib/layout_parser.rb
  4. 10
      samples/layouts.rb
  5. 14
      spec/layout_parser_spec.rb

16
docs/dsl/use_layout.rst

@ -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
--------

6
lib/squib/api/settings.rb

@ -31,5 +31,11 @@ module Squib
@font = (opts[:font] == :default) ? Squib::DEFAULT_FONT: opts[:font] @font = (opts[:font] == :default) ? Squib::DEFAULT_FONT: opts[:font]
end 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
end end

4
lib/squib/layout_parser.rb

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

10
samples/layouts.rb

@ -60,3 +60,13 @@ Squib::Deck.new(layout: 'hand.yml') do
png file: 'pokercard.png', alpha: 0.5 png file: 'pokercard.png', alpha: 0.5
save_png prefix: 'layout_builtin_hand_' save_png prefix: 'layout_builtin_hand_'
end 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

14
spec/layout_parser_spec.rb

@ -186,4 +186,16 @@ describe Squib::LayoutParser do
}) })
end end
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

Loading…
Cancel
Save