From 955d64a01fa2a334d62d887ff051ef5f027a4fcc Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Wed, 16 Mar 2016 17:02:52 -0400 Subject: [PATCH] feature: add use_layout method to load layouts at runtime Closes #141 --- docs/dsl/use_layout.rst | 16 ++++++++++++++++ lib/squib/api/settings.rb | 6 ++++++ lib/squib/layout_parser.rb | 4 ++-- samples/layouts.rb | 10 ++++++++++ spec/layout_parser_spec.rb | 14 +++++++++++++- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 docs/dsl/use_layout.rst diff --git a/docs/dsl/use_layout.rst b/docs/dsl/use_layout.rst new file mode 100644 index 0000000..6aae13f --- /dev/null +++ b/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 +-------- diff --git a/lib/squib/api/settings.rb b/lib/squib/api/settings.rb index 6a4ac67..aa5bc02 100644 --- a/lib/squib/api/settings.rb +++ b/lib/squib/api/settings.rb @@ -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 diff --git a/lib/squib/layout_parser.rb b/lib/squib/layout_parser.rb index fa223d9..327a9b4 100644 --- a/lib/squib/layout_parser.rb +++ b/lib/squib/layout_parser.rb @@ -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 diff --git a/samples/layouts.rb b/samples/layouts.rb index a96037f..1aa20f8 100644 --- a/samples/layouts.rb +++ b/samples/layouts.rb @@ -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 diff --git a/spec/layout_parser_spec.rb b/spec/layout_parser_spec.rb index 1019d0c..21fd745 100644 --- a/spec/layout_parser_spec.rb +++ b/spec/layout_parser_spec.rb @@ -186,4 +186,16 @@ describe Squib::LayoutParser do }) end -end \ No newline at end of file + 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