Bringing back the extends feature
Adding in my own merge key implementation that allows you to bring in
other keys and modfiy the data. Makes moving boxes around easier.
A quasi-revert of 00e8b59144
...but with something useful!
This commit is contained in:
+56
-3
@@ -49,8 +49,7 @@ module Squib
|
||||
# @param height: [Integer] the height of each card in pixels
|
||||
# @param cards: [Integer] the number of cards in the deck
|
||||
# @param dpi: [Integer] the pixels per inch when rendering out to PDF or calculating using inches.
|
||||
# @param config: [String] the Yaml file used for global settings of this deck
|
||||
# @param layout: [String] the Yaml file used for layouts.
|
||||
# @param config: [String] the file used for global settings of this deck
|
||||
# @param block [Block] the main body of the script.
|
||||
# @api public
|
||||
def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
|
||||
@@ -63,7 +62,7 @@ module Squib
|
||||
@progress_bar = Squib::Progress.new(false)
|
||||
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
||||
load_config(config)
|
||||
@layout = YAML.load_file(layout) unless layout.nil?
|
||||
load_layout(layout)
|
||||
if block_given?
|
||||
instance_eval(&block)
|
||||
end
|
||||
@@ -101,6 +100,60 @@ module Squib
|
||||
end
|
||||
end
|
||||
|
||||
# Load the layout configuration file, if exists
|
||||
# @api private
|
||||
def load_layout(file)
|
||||
return if file.nil?
|
||||
@layout = {}
|
||||
yml = YAML.load_file(file)
|
||||
yml.each do |key, value|
|
||||
@layout[key] = recurse_extends(yml, key, {})
|
||||
end
|
||||
end
|
||||
|
||||
# Process the extends recursively
|
||||
# :nodoc:
|
||||
# @api private
|
||||
def recurse_extends(yml, key, visited )
|
||||
assert_not_visited(key, visited)
|
||||
return yml[key] unless has_extends?(yml, key)
|
||||
visited[key] = key
|
||||
parent_keys = [yml[key]['extends']].flatten
|
||||
h = {}
|
||||
parent_keys.each do |parent_key|
|
||||
from_extends = yml[key].merge(recurse_extends(yml, parent_key, visited)) do |key, child_val, parent_val|
|
||||
if child_val.to_s.strip.start_with?('+=')
|
||||
parent_val + child_val.sub("+=",'').strip.to_f
|
||||
elsif child_val.to_s.strip.start_with?('-=')
|
||||
parent_val - child_val.sub("-=",'').strip.to_f
|
||||
else
|
||||
child_val #child overrides parent when merging, no +=
|
||||
end
|
||||
end
|
||||
h = h.merge(from_extends) do |key, older_sibling, younger_sibling|
|
||||
younger_sibling #when two siblings have the same entry, the "younger" (lower one) overrides
|
||||
end
|
||||
end
|
||||
return h
|
||||
end
|
||||
|
||||
# Does this layout entry have an extends field?
|
||||
# i.e. is it a base-case or will it need recursion?
|
||||
# :nodoc:
|
||||
# @api private
|
||||
def has_extends?(yml, key)
|
||||
yml[key].key?('extends')
|
||||
end
|
||||
|
||||
# Safeguard against malformed circular extends
|
||||
# :nodoc:
|
||||
# @api private
|
||||
def assert_not_visited(key, visited)
|
||||
if visited.key? key
|
||||
raise "Invalid layout: circular extends with '#{key}'"
|
||||
end
|
||||
end
|
||||
|
||||
##################
|
||||
### PUBLIC API ###
|
||||
##################
|
||||
|
||||
Reference in New Issue
Block a user