documentation specs, beginning cleaner conversion
parent
750cb40267
commit
76ef45cfa4
|
|
@ -34,12 +34,12 @@ module Squib
|
||||||
end
|
end
|
||||||
|
|
||||||
# DSL method. See http://squib.readthedocs.io
|
# DSL method. See http://squib.readthedocs.io
|
||||||
def grid(opts = {})
|
# def grid(opts = {})
|
||||||
range = Args::CardRange.new(opts[:range], deck_size: size)
|
# range = Args::CardRange.new(opts[:range], deck_size: size)
|
||||||
draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
# draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||||
box = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
# box = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||||
range.each { |i| @cards[i].grid(box[i], draw[i]) }
|
# range.each { |i| @cards[i].grid(box[i], draw[i]) }
|
||||||
end
|
# end
|
||||||
|
|
||||||
# DSL method. See http://squib.readthedocs.io
|
# DSL method. See http://squib.readthedocs.io
|
||||||
def triangle(opts = {})
|
def triangle(opts = {})
|
||||||
|
|
|
||||||
|
|
@ -1,55 +1,57 @@
|
||||||
require_relative 'arg_loader'
|
require_relative 'arg_loader'
|
||||||
|
|
||||||
module Squib
|
module Squib::Args
|
||||||
# @api private
|
|
||||||
module Args
|
|
||||||
|
|
||||||
class Box
|
module_function
|
||||||
include ArgLoader
|
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 = {})
|
class Box
|
||||||
@deck = deck
|
include ArgLoader
|
||||||
@dsl_method_defaults = dsl_method_defaults
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.parameters
|
def initialize(deck = nil, dsl_method_defaults = {})
|
||||||
{ x: 0, y: 0,
|
@deck = deck
|
||||||
width: :deck, height: :deck,
|
@dsl_method_defaults = dsl_method_defaults
|
||||||
radius: nil, x_radius: 0, y_radius: 0
|
end
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.expanding_parameters
|
def self.parameters
|
||||||
parameters.keys # all of them
|
{ x: 0, y: 0,
|
||||||
end
|
width: :deck, height: :deck,
|
||||||
|
radius: nil, x_radius: 0, y_radius: 0
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def self.params_with_units
|
def self.expanding_parameters
|
||||||
parameters.keys # all of them
|
parameters.keys # all of them
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_width(arg, _i)
|
def self.params_with_units
|
||||||
return arg if @deck.nil?
|
parameters.keys # all of them
|
||||||
return @deck.width if arg == :deck
|
end
|
||||||
arg
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_height(arg, _i)
|
def validate_width(arg, _i)
|
||||||
return arg if @deck.nil?
|
return arg if @deck.nil?
|
||||||
return @deck.height if arg == :deck
|
return @deck.width if arg == :deck
|
||||||
arg
|
arg
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_x_radius(arg, i)
|
def validate_height(arg, _i)
|
||||||
return radius[i] unless radius[i].nil?
|
return arg if @deck.nil?
|
||||||
arg
|
return @deck.height if arg == :deck
|
||||||
end
|
arg
|
||||||
|
end
|
||||||
|
|
||||||
def validate_y_radius(arg, i)
|
def validate_x_radius(arg, i)
|
||||||
return radius[i] unless radius[i].nil?
|
return radius[i] unless radius[i].nil?
|
||||||
arg
|
arg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_y_radius(arg, i)
|
||||||
|
return radius[i] unless radius[i].nil?
|
||||||
|
arg
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -112,5 +112,11 @@ module Squib
|
||||||
require_relative 'api/text'
|
require_relative 'api/text'
|
||||||
require_relative 'api/units'
|
require_relative 'api/units'
|
||||||
|
|
||||||
|
###################
|
||||||
|
### DSL METHODS ###
|
||||||
|
###################
|
||||||
|
require_relative 'dsl/background'
|
||||||
|
require_relative 'dsl/grid'
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ module Squib
|
||||||
@dsl_method = dsl_method
|
@dsl_method = dsl_method
|
||||||
end
|
end
|
||||||
|
|
||||||
def accepted_params
|
def self.accepted_params
|
||||||
%i{
|
%i{
|
||||||
range
|
range
|
||||||
color
|
color
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -4,6 +4,7 @@ module Squib::WarnUnexpectedParams
|
||||||
using Rainbow # we can colorize strings now!
|
using Rainbow # we can colorize strings now!
|
||||||
|
|
||||||
def warn_if_unexpected(opts, uplevel: 5)
|
def warn_if_unexpected(opts, uplevel: 5)
|
||||||
|
accepted_params = self.class.accepted_params
|
||||||
unexpected = opts.keys - accepted_params
|
unexpected = opts.keys - accepted_params
|
||||||
unexpected.each do |key|
|
unexpected.each do |key|
|
||||||
warn "Unexpected parameter '#{key.to_s.yellow}:' to #{dsl_method.to_s.cyan}(). Accepted parameters: #{accepted_params}",
|
warn "Unexpected parameter '#{key.to_s.yellow}:' to #{dsl_method.to_s.cyan}(). Accepted parameters: #{accepted_params}",
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@ require 'squib'
|
||||||
Squib::Deck.new do
|
Squib::Deck.new do
|
||||||
background color: :white
|
background color: :white
|
||||||
|
|
||||||
grid x: 10, y: 10, width: 50, height: 50, stroke_color: '#0066FF', stroke_width: 1.5, angle: 0.1
|
grid x: 10, y: 10, width: 50, height: 50, stroke_color: '#0066FF', stroke_width: 1.5
|
||||||
grid x: 10, y: 10, width: 200, height: 200, stroke_color: '#0066FF', stroke_width: 3, angle: 0.1
|
grid x: 10, y: 10, width: 200, height: 200, stroke_color: '#0066FF', stroke_width: 3
|
||||||
|
|
||||||
rect x: 305, y: 105, width: 200, height: 50, dash: '4 2'
|
rect x: 305, y: 105, width: 200, height: 50, dash: '4 2'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -62,8 +62,12 @@ def project_template(file)
|
||||||
"#{File.expand_path(File.dirname(__FILE__))}/../lib/squib/project_template/#{file}"
|
"#{File.expand_path(File.dirname(__FILE__))}/../lib/squib/project_template/#{file}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def doc(file)
|
def doc_dsl_rst(name)
|
||||||
"#{File.expand_path(File.dirname(__FILE__))}/../docs/#{file}"
|
File.read("#{File.expand_path(File.dirname(__FILE__))}/../docs/dsl/#{name}.rst")
|
||||||
|
end
|
||||||
|
|
||||||
|
def doc_args_rst(name)
|
||||||
|
File.read("#{File.expand_path(File.dirname(__FILE__))}/../docs/args/#{name}.rst")
|
||||||
end
|
end
|
||||||
|
|
||||||
def conf(file)
|
def conf(file)
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ Gem::Specification.new do |spec|
|
||||||
spec.add_runtime_dependency 'rsvg2', '~> 3.4'
|
spec.add_runtime_dependency 'rsvg2', '~> 3.4'
|
||||||
spec.add_runtime_dependency 'ruby-progressbar', '~> 1.10'
|
spec.add_runtime_dependency 'ruby-progressbar', '~> 1.10'
|
||||||
|
|
||||||
|
spec.add_development_dependency 'activesupport'
|
||||||
spec.add_development_dependency 'bundler'
|
spec.add_development_dependency 'bundler'
|
||||||
spec.add_development_dependency 'coveralls', '>= 0.8.21'
|
spec.add_development_dependency 'coveralls', '>= 0.8.21'
|
||||||
spec.add_development_dependency 'game_icons'
|
spec.add_development_dependency 'game_icons'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue