Better ability to handle per-dsl-method defaults
parent
f2d693f8ff
commit
25b13fd592
|
|
@ -48,12 +48,11 @@ module Squib
|
||||||
# @return [Array] Returns an Array of hashes keyed by :width and :height that mark the ink extents of the text rendered.
|
# @return [Array] Returns an Array of hashes keyed by :width and :height that mark the ink extents of the text rendered.
|
||||||
# @api public
|
# @api public
|
||||||
def text(opts = {})
|
def text(opts = {})
|
||||||
opts = { stroke_width: 0, width: :auto, height: :auto }.merge(opts)
|
|
||||||
range = Args::CardRange.new(opts[:range], deck_size: size)
|
range = Args::CardRange.new(opts[:range], deck_size: size)
|
||||||
para = Args::Paragraph.new(font).load!(opts, expand_by: size, layout: layout)
|
para = Args::Paragraph.new(font).load!(opts, expand_by: size, layout: layout)
|
||||||
box = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
box = Args::Box.new(self, {width: :auto, height: :auto}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||||
trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||||
draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
draw = Args::Draw.new(custom_colors, {stroke_width: 0.0}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||||
embed = TextEmbed.new
|
embed = TextEmbed.new
|
||||||
yield(embed) if block_given? #store the opts for later use
|
yield(embed) if block_given? #store the opts for later use
|
||||||
extents = Array.new(@cards.size)
|
extents = Array.new(@cards.size)
|
||||||
|
|
|
||||||
|
|
@ -45,18 +45,21 @@ module Squib
|
||||||
# (1) Use whatever is specified if it is
|
# (1) Use whatever is specified if it is
|
||||||
# (2) Go over all layout specifications (if any) and look them up
|
# (2) Go over all layout specifications (if any) and look them up
|
||||||
# - Use layout when it's specified for that card
|
# - Use layout when it's specified for that card
|
||||||
# - Use default if no layout was specified,
|
# - Use "default" if no layout was specified, or the layout itself did not specify
|
||||||
# or the layout itself did not specify
|
# Defaut can be overriden for a given dsl method (@dsl_method_defaults)
|
||||||
|
# (e.g stroke width is 0.0 for text, non-zero everywhere else)
|
||||||
|
#
|
||||||
def defaultify(p, args, layout)
|
def defaultify(p, args, layout)
|
||||||
return args[p] if args.key? p # arg was specified, no defaults used
|
return args[p] if args.key? p # arg was specified, no defaults used
|
||||||
|
dsl_method_defaults = @dsl_method_defaults || {}
|
||||||
args[:layout].map do |layout_arg|
|
args[:layout].map do |layout_arg|
|
||||||
if layout_arg.nil?
|
if layout_arg.nil?
|
||||||
self.class.parameters[p] # no layout specified, use default
|
self.class.parameters.merge(dsl_method_defaults)[p] # no layout specified, use default
|
||||||
else
|
else
|
||||||
if layout[layout_arg.to_s].key? p.to_s
|
if layout[layout_arg.to_s].key? p.to_s
|
||||||
layout[layout_arg.to_s][p.to_s] # param specified in layout
|
layout[layout_arg.to_s][p.to_s] # param specified in layout
|
||||||
else
|
else
|
||||||
self.class.parameters[p] # layout specified, but not this param
|
self.class.parameters.merge(dsl_method_defaults)[p] # layout specified, but not this param
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,9 @@ module Squib
|
||||||
class Box
|
class Box
|
||||||
include ArgLoader
|
include ArgLoader
|
||||||
|
|
||||||
def initialize(deck = nil)
|
def initialize(deck = nil, dsl_method_defaults = {})
|
||||||
@deck = deck
|
@deck = deck
|
||||||
|
@dsl_method_defaults = dsl_method_defaults
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.parameters
|
def self.parameters
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,9 @@ module Squib
|
||||||
include ArgLoader
|
include ArgLoader
|
||||||
include ColorValidator
|
include ColorValidator
|
||||||
|
|
||||||
def initialize(custom_colors)
|
def initialize(custom_colors, dsl_method_defaults = {})
|
||||||
@custom_colors = custom_colors
|
@custom_colors = custom_colors
|
||||||
|
@dsl_method_defaults = dsl_method_defaults
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.parameters
|
def self.parameters
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,16 @@ describe Squib::Args::Draw do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'dsl overrides' do
|
||||||
|
subject(:draw) {Squib::Args::Draw.new(custom_colors, {stroke_width: 0.0})}
|
||||||
|
|
||||||
|
it 'works when specified' do
|
||||||
|
draw.load!({}) # go right to defaults
|
||||||
|
expect(draw.stroke_width).to eq( [0.0] ) #ordinarily a non-zero according
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
context 'validation' do
|
context 'validation' do
|
||||||
|
|
||||||
it 'converts to Cairo options' do
|
it 'converts to Cairo options' do
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue