Browse Source

cleaner method sigs

dev
Andy Meneely 6 years ago
parent
commit
70b3c0b136
  1. 3
      lib/squib/args/box.rb
  2. 50
      lib/squib/args/card_range.rb
  3. 137
      lib/squib/args/draw.rb
  4. 4
      lib/squib/dsl/background.rb
  5. 6
      lib/squib/dsl/grid.rb

3
lib/squib/args/box.rb

@ -2,8 +2,7 @@ require_relative 'arg_loader'
module Squib::Args
module_function
def extract_box(opts, deck, dsl_method_defaults = {})
module_function def extract_box(opts, deck, dsl_method_defaults = {})
Box.new(deck, dsl_method_defaults).extract!(opts, deck)
end

50
lib/squib/args/card_range.rb

@ -1,32 +1,34 @@
module Squib
# @api private
module Args
class CardRange
include Enumerable
module Squib::Args
def initialize(input, deck_size: 1)
@range = validate(input, deck_size)
end
module_function def extract_range(opts, deck)
CardRange.new(opts[:range], deck_size: deck.size)
end
class CardRange
include Enumerable
# Hook into enumerable by delegating to @range
def each(&block)
@range.each { |i| block.call(i) }
end
def initialize(input, deck_size: 1)
@range = validate(input, deck_size)
end
def size
@range.size
end
# Hook into enumerable by delegating to @range
def each(&block)
@range.each { |i| block.call(i) }
end
private
def validate(input, deck_size)
input ||= :all # default
input = 0..(deck_size - 1) if input == :all
input = (input.to_i)..(input.to_i) if input.respond_to? :to_i
raise ArgumentError.new("#{input} must be Enumerable (i.e. respond_to :each).") unless input.respond_to? :each
raise ArgumentError.new("#{input} is outside of deck range of 0..#{deck_size - 1}") if (!input.max.nil?) && (input.max > (deck_size - 1))
input
end
def size
@range.size
end
private
def validate(input, deck_size)
input ||= :all # default
input = 0..(deck_size - 1) if input == :all
input = (input.to_i)..(input.to_i) if input.respond_to? :to_i
raise ArgumentError.new("#{input} must be Enumerable (i.e. respond_to :each).") unless input.respond_to? :each
raise ArgumentError.new("#{input} is outside of deck range of 0..#{deck_size - 1}") if (!input.max.nil?) && (input.max > (deck_size - 1))
input
end
end
end

137
lib/squib/args/draw.rb

@ -2,91 +2,92 @@ require 'cairo'
require_relative 'arg_loader'
require_relative 'color_validator'
module Squib
# @api private
module Args
module Squib::Args
class Draw
include ArgLoader
include ColorValidator
module_function def extract_draw(opts, deck, dsl_method_defaults = {})
Draw.new(deck.custom_colors, dsl_method_defaults).extract!(opts, deck)
end
def initialize(custom_colors, dsl_method_defaults = {})
@custom_colors = custom_colors
@dsl_method_defaults = dsl_method_defaults
end
class Draw
include ArgLoader
include ColorValidator
def self.parameters
{ color: :black,
fill_color: '#0000',
stroke_color: :black,
stroke_width: 2.0,
stroke_strategy: :fill_first,
join: :miter,
cap: 'butt',
dash: ''
}
end
def initialize(custom_colors, dsl_method_defaults = {})
@custom_colors = custom_colors
@dsl_method_defaults = dsl_method_defaults
end
def self.expanding_parameters
parameters.keys # all of them are expandable
end
def self.parameters
{ color: :black,
fill_color: '#0000',
stroke_color: :black,
stroke_width: 2.0,
stroke_strategy: :fill_first,
join: :miter,
cap: 'butt',
dash: ''
}
end
def self.params_with_units
[:stroke_width]
end
def self.expanding_parameters
parameters.keys # all of them are expandable
end
def validate_join(arg, _i)
case arg.to_s.strip.downcase
when 'miter'
Cairo::LINE_JOIN_MITER
when 'round'
Cairo::LINE_JOIN_ROUND
when 'bevel'
Cairo::LINE_JOIN_BEVEL
end
end
def self.params_with_units
[:stroke_width]
end
def validate_cap(arg, _i)
case arg.to_s.strip.downcase
when 'butt'
Cairo::LINE_CAP_BUTT
when 'round'
Cairo::LINE_CAP_ROUND
when 'square'
Cairo::LINE_CAP_SQUARE
end
def validate_join(arg, _i)
case arg.to_s.strip.downcase
when 'miter'
Cairo::LINE_JOIN_MITER
when 'round'
Cairo::LINE_JOIN_ROUND
when 'bevel'
Cairo::LINE_JOIN_BEVEL
end
end
def validate_dash(arg, _i)
arg.to_s.split.collect do |x|
UnitConversion.parse(x, @dpi).to_f
end
def validate_cap(arg, _i)
case arg.to_s.strip.downcase
when 'butt'
Cairo::LINE_CAP_BUTT
when 'round'
Cairo::LINE_CAP_ROUND
when 'square'
Cairo::LINE_CAP_SQUARE
end
end
def validate_fill_color(arg, _i)
colorify(arg, @custom_colors)
def validate_dash(arg, _i)
arg.to_s.split.collect do |x|
UnitConversion.parse(x, @dpi).to_f
end
end
def validate_stroke_color(arg, _i)
colorify(arg, @custom_colors)
end
def validate_fill_color(arg, _i)
colorify(arg, @custom_colors)
end
def validate_color(arg, _i)
colorify(arg, @custom_colors)
end
def validate_stroke_color(arg, _i)
colorify(arg, @custom_colors)
end
def validate_stroke_strategy(arg, _i)
case arg.to_s.downcase.strip
when 'fill_first'
:fill_first
when 'stroke_first'
:stroke_first
else
raise "Only 'stroke_first' or 'fill_first' allowed"
end
end
def validate_color(arg, _i)
colorify(arg, @custom_colors)
end
def validate_stroke_strategy(arg, _i)
case arg.to_s.downcase.strip
when 'fill_first'
:fill_first
when 'stroke_first'
:stroke_first
else
raise "Only 'stroke_first' or 'fill_first' allowed"
end
end
end
end

4
lib/squib/dsl/background.rb

@ -26,8 +26,8 @@ module Squib
def run(opts)
warn_if_unexpected opts
range = Args::CardRange.new(opts[:range], deck_size: deck.size)
draw = Args::Draw.new(@deck.custom_colors).extract!(opts, deck)
range = Args.extract_range opts, deck
draw = Args.extract_draw opts, deck
range.each { |i| @deck.cards[i].background(draw.color[i]) }
end
end

6
lib/squib/dsl/grid.rb

@ -25,9 +25,9 @@ module Squib
def run(opts)
warn_if_unexpected opts
range = Args::CardRange.new(opts[:range], deck_size: deck.size)
draw = Args::Draw.new(@deck.custom_colors).extract!(opts, deck)
box = Args.extract_box(opts, deck)
range = Args.extract_range opts, deck
draw = Args.extract_draw opts, deck
box = Args.extract_box opts, deck
range.each { |i| deck.cards[i].grid(box[i], draw[i]) }
end
end

Loading…
Cancel
Save