13 changed files with 154 additions and 50 deletions
@ -1,55 +1,57 @@
|
||||
require_relative 'arg_loader' |
||||
|
||||
module Squib |
||||
# @api private |
||||
module Args |
||||
module Squib::Args |
||||
|
||||
class Box |
||||
include ArgLoader |
||||
module_function |
||||
def extract_box(opts, deck, dsl_method_defaults = {}) |
||||
Box.new(deck, dsl_method_defaults).extract!(opts, deck) |
||||
end |
||||
|
||||
def initialize(deck = nil, dsl_method_defaults = {}) |
||||
@deck = deck |
||||
@dsl_method_defaults = dsl_method_defaults |
||||
end |
||||
class Box |
||||
include ArgLoader |
||||
|
||||
def self.parameters |
||||
{ x: 0, y: 0, |
||||
width: :deck, height: :deck, |
||||
radius: nil, x_radius: 0, y_radius: 0 |
||||
} |
||||
end |
||||
def initialize(deck = nil, dsl_method_defaults = {}) |
||||
@deck = deck |
||||
@dsl_method_defaults = dsl_method_defaults |
||||
end |
||||
|
||||
def self.expanding_parameters |
||||
parameters.keys # all of them |
||||
end |
||||
def self.parameters |
||||
{ x: 0, y: 0, |
||||
width: :deck, height: :deck, |
||||
radius: nil, x_radius: 0, y_radius: 0 |
||||
} |
||||
end |
||||
|
||||
def self.params_with_units |
||||
parameters.keys # all of them |
||||
end |
||||
def self.expanding_parameters |
||||
parameters.keys # all of them |
||||
end |
||||
|
||||
def validate_width(arg, _i) |
||||
return arg if @deck.nil? |
||||
return @deck.width if arg == :deck |
||||
arg |
||||
end |
||||
def self.params_with_units |
||||
parameters.keys # all of them |
||||
end |
||||
|
||||
def validate_height(arg, _i) |
||||
return arg if @deck.nil? |
||||
return @deck.height if arg == :deck |
||||
arg |
||||
end |
||||
def validate_width(arg, _i) |
||||
return arg if @deck.nil? |
||||
return @deck.width if arg == :deck |
||||
arg |
||||
end |
||||
|
||||
def validate_x_radius(arg, i) |
||||
return radius[i] unless radius[i].nil? |
||||
arg |
||||
end |
||||
def validate_height(arg, _i) |
||||
return arg if @deck.nil? |
||||
return @deck.height if arg == :deck |
||||
arg |
||||
end |
||||
|
||||
def validate_y_radius(arg, i) |
||||
return radius[i] unless radius[i].nil? |
||||
arg |
||||
end |
||||
def validate_x_radius(arg, i) |
||||
return radius[i] unless radius[i].nil? |
||||
arg |
||||
end |
||||
|
||||
def validate_y_radius(arg, i) |
||||
return radius[i] unless radius[i].nil? |
||||
arg |
||||
end |
||||
|
||||
end |
||||
|
||||
end |
||||
|
||||
@ -0,0 +1,35 @@
|
||||
require_relative '../errors_warnings/warn_unexpected_params' |
||||
|
||||
module Squib |
||||
class Deck |
||||
def grid(opts = {}) |
||||
DSL::Grid.new(self, __callee__).run(opts) |
||||
end |
||||
end |
||||
|
||||
module DSL |
||||
class Grid |
||||
include WarnUnexpectedParams |
||||
attr_reader :dsl_method, :deck |
||||
|
||||
def initialize(deck, dsl_method) |
||||
@deck = deck |
||||
@dsl_method = dsl_method |
||||
end |
||||
|
||||
def self.accepted_params |
||||
%i(x y width height |
||||
fill_color stroke_color stroke_width stroke_strategy dash cap |
||||
range layout) |
||||
end |
||||
|
||||
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.each { |i| deck.cards[i].grid(box[i], draw[i]) } |
||||
end |
||||
end |
||||
end |
||||
end |
||||
@ -0,0 +1,14 @@
|
||||
require 'spec_helper' |
||||
require 'active_support' |
||||
require 'active_support/core_ext/string/inflections' |
||||
|
||||
def documented_options(dsl_method) |
||||
rst = doc_dsl_rst dsl_method.to_s.underscore |
||||
options_rst = rst[/Options.+^+(.+)^+/m] |
||||
includes = options_rst.scan(/\.\. include:: \/args\/(.+)\.rst/).flatten |
||||
includes.each do |key| |
||||
options_rst.gsub!(".. include:: /args/#{key}.rst", doc_args_rst(key)) |
||||
end |
||||
opts = options_rst.lines.select { |line| line.match? /^[a-z]/ } |
||||
opts.map { |o| o.strip.to_sym } |
||||
end |
||||
@ -0,0 +1,18 @@
|
||||
require 'spec_helper' |
||||
require_relative 'docs_helper' |
||||
|
||||
describe 'docs spec helper' do |
||||
|
||||
it 'gets all documented options for background' do |
||||
options = documented_options(:Background) |
||||
expect(options.sort).to eq(%i(color range)) |
||||
end |
||||
|
||||
it 'gets all documented options for grid' do |
||||
expected = %i(x y width height fill_color stroke_color stroke_width |
||||
stroke_strategy dash cap range layout) |
||||
options = documented_options(:Grid) |
||||
expect(options.sort).to eq(expected.sort) |
||||
end |
||||
|
||||
end |
||||
@ -0,0 +1,15 @@
|
||||
require_relative 'docs_helper' |
||||
|
||||
describe Squib::DSL do |
||||
context 'methods' do |
||||
|
||||
Squib::DSL.constants.each do |m| |
||||
it "accepted params for #{m} are in the docs" do |
||||
accepted_params = Squib::DSL.const_get(m).accepted_params |
||||
expect(accepted_params).to eq(documented_options(m)) |
||||
end |
||||
end |
||||
|
||||
|
||||
end |
||||
end |
||||
@ -0,0 +1,8 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe Squib::Deck do |
||||
context '#background' do |
||||
|
||||
|
||||
end |
||||
end |
||||
Loading…
Reference in new issue