diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d27fc6..f67b184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ * Added `showcase` feature to create a fancy-looking 3D reflection to showcase your cards. Documented, tested, and added a sample for it. * Added a basic Rakefile, documented in README. -* Some internal refactoring and better testing around layouts +* Some internal refactoring, better testing, and more documentation with layouts ## v0.1.0 * Added `save_sheet` command that saves a range into PNG sheets, complete with trim, gap, margin, columns, and sometimes automagically computed rows. See samples/saves.rb. diff --git a/README.md b/README.md index 2fe3a55..4a3fd95 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,20 @@ yang: x: += 50 ``` +Furthermore, if you want to extend multiple parents, it looks like this: + +```yaml +socrates: + x: 100 +aristotle: + y: 200 +aristotle: + extends: + - socrates + - plato + x: += 50 +``` + ### Multiple layout files Squib also supports the combination of multiple layout files. As shown in the above example, if you provide an `Array` of files then Squib will merge them sequentially. Colliding keys will be completely re-defined by the later file. Extends is processed after _each file_. YAML merge keys are NOT supported across multiple files - use extends instead. Here's a demonstrative example: diff --git a/lib/squib/layout_parser.rb b/lib/squib/layout_parser.rb index 3f4a446..63245ad 100644 --- a/lib/squib/layout_parser.rb +++ b/lib/squib/layout_parser.rb @@ -34,6 +34,7 @@ module Squib def self.recurse_extends(yml, key, visited ) assert_not_visited(key, visited) return yml[key] unless has_extends?(yml, key) + return yml[key] unless parents_exist?(yml, key) visited[key] = key parent_keys = [yml[key]['extends']].flatten h = {} @@ -62,6 +63,19 @@ module Squib !!yml[key] && yml[key].key?('extends') end + # Checks if we have any absentee parents + # @api private + def self.parents_exist?(yml, key) + exists = true + Array(yml[key]['extends']).each do |parent| + unless yml.key?(parent) + exists = false unless + Squib.logger.error "Processing layout: '#{key}' attempts to extend a missing '#{yml[key]['extends']}'" + end + end + return exists + end + # Safeguard against malformed circular extends # :nodoc: # @api private diff --git a/spec/data/layouts/extends-nonexists.yml b/spec/data/layouts/extends-nonexists.yml new file mode 100644 index 0000000..dcf5469 --- /dev/null +++ b/spec/data/layouts/extends-nonexists.yml @@ -0,0 +1,3 @@ +verbal: + extends: kaisersoze + font_size: 25 \ No newline at end of file diff --git a/spec/layout_parser_spec.rb b/spec/layout_parser_spec.rb index 93f7c78..1019d0c 100644 --- a/spec/layout_parser_spec.rb +++ b/spec/layout_parser_spec.rb @@ -173,4 +173,17 @@ describe Squib::LayoutParser do Squib::LayoutParser.load_layout('yeti') end + it 'freaks out if you extend something doesn\'t exist' do + expect(Squib.logger) + .to receive(:error) + .with("Processing layout: 'verbal' attempts to extend a missing 'kaisersoze'") + layout = Squib::LayoutParser.load_layout(layout_file('extends-nonexists.yml')) + expect(layout).to eq({ + 'verbal' => { + 'font_size' => 25, + 'extends' => 'kaisersoze' + } + }) + end + end \ No newline at end of file