From 76ef45cfa448a7f77728b582dca138ed32d0792e Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Fri, 13 Mar 2020 10:38:34 -0400 Subject: [PATCH] documentation specs, beginning cleaner conversion --- lib/squib/api/shapes.rb | 12 +-- lib/squib/args/box.rb | 80 ++++++++++--------- lib/squib/deck.rb | 6 ++ lib/squib/dsl/background.rb | 2 +- lib/squib/dsl/grid.rb | 35 ++++++++ .../errors_warnings/warn_unexpected_params.rb | 1 + samples/shapes/_draw_shapes.rb | 4 +- spec/docs/docs_helper.rb | 14 ++++ spec/docs/docs_helper_spec.rb | 18 +++++ spec/docs/options_documented_spec.rb | 15 ++++ spec/dsl/background_spec.rb | 8 ++ spec/spec_helper.rb | 8 +- squib.gemspec | 1 + 13 files changed, 154 insertions(+), 50 deletions(-) create mode 100644 lib/squib/dsl/grid.rb create mode 100644 spec/docs/docs_helper.rb create mode 100644 spec/docs/docs_helper_spec.rb create mode 100644 spec/docs/options_documented_spec.rb create mode 100644 spec/dsl/background_spec.rb diff --git a/lib/squib/api/shapes.rb b/lib/squib/api/shapes.rb index 6189b48..e0bb56f 100644 --- a/lib/squib/api/shapes.rb +++ b/lib/squib/api/shapes.rb @@ -34,12 +34,12 @@ module Squib end # DSL method. See http://squib.readthedocs.io - def grid(opts = {}) - range = Args::CardRange.new(opts[:range], deck_size: size) - 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) - range.each { |i| @cards[i].grid(box[i], draw[i]) } - end + # def grid(opts = {}) + # range = Args::CardRange.new(opts[:range], deck_size: size) + # 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) + # range.each { |i| @cards[i].grid(box[i], draw[i]) } + # end # DSL method. See http://squib.readthedocs.io def triangle(opts = {}) diff --git a/lib/squib/args/box.rb b/lib/squib/args/box.rb index 63bb7d2..dd1fd1b 100644 --- a/lib/squib/args/box.rb +++ b/lib/squib/args/box.rb @@ -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 diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 3485b10..e318f00 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -112,5 +112,11 @@ module Squib require_relative 'api/text' require_relative 'api/units' + ################### + ### DSL METHODS ### + ################### + require_relative 'dsl/background' + require_relative 'dsl/grid' + end end diff --git a/lib/squib/dsl/background.rb b/lib/squib/dsl/background.rb index aa50288..0ba428a 100644 --- a/lib/squib/dsl/background.rb +++ b/lib/squib/dsl/background.rb @@ -17,7 +17,7 @@ module Squib @dsl_method = dsl_method end - def accepted_params + def self.accepted_params %i{ range color diff --git a/lib/squib/dsl/grid.rb b/lib/squib/dsl/grid.rb new file mode 100644 index 0000000..c1db018 --- /dev/null +++ b/lib/squib/dsl/grid.rb @@ -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 diff --git a/lib/squib/errors_warnings/warn_unexpected_params.rb b/lib/squib/errors_warnings/warn_unexpected_params.rb index 655d582..d027671 100644 --- a/lib/squib/errors_warnings/warn_unexpected_params.rb +++ b/lib/squib/errors_warnings/warn_unexpected_params.rb @@ -4,6 +4,7 @@ module Squib::WarnUnexpectedParams using Rainbow # we can colorize strings now! def warn_if_unexpected(opts, uplevel: 5) + accepted_params = self.class.accepted_params unexpected = opts.keys - accepted_params unexpected.each do |key| warn "Unexpected parameter '#{key.to_s.yellow}:' to #{dsl_method.to_s.cyan}(). Accepted parameters: #{accepted_params}", diff --git a/samples/shapes/_draw_shapes.rb b/samples/shapes/_draw_shapes.rb index 47afc87..ad7be09 100644 --- a/samples/shapes/_draw_shapes.rb +++ b/samples/shapes/_draw_shapes.rb @@ -3,8 +3,8 @@ require 'squib' Squib::Deck.new do 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: 200, height: 200, stroke_color: '#0066FF', stroke_width: 3, 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 rect x: 305, y: 105, width: 200, height: 50, dash: '4 2' diff --git a/spec/docs/docs_helper.rb b/spec/docs/docs_helper.rb new file mode 100644 index 0000000..bc0cf33 --- /dev/null +++ b/spec/docs/docs_helper.rb @@ -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 diff --git a/spec/docs/docs_helper_spec.rb b/spec/docs/docs_helper_spec.rb new file mode 100644 index 0000000..1e800e4 --- /dev/null +++ b/spec/docs/docs_helper_spec.rb @@ -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 diff --git a/spec/docs/options_documented_spec.rb b/spec/docs/options_documented_spec.rb new file mode 100644 index 0000000..d1b121d --- /dev/null +++ b/spec/docs/options_documented_spec.rb @@ -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 diff --git a/spec/dsl/background_spec.rb b/spec/dsl/background_spec.rb new file mode 100644 index 0000000..d393a98 --- /dev/null +++ b/spec/dsl/background_spec.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +describe Squib::Deck do + context '#background' do + + + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ae71942..a0cdf8d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -62,8 +62,12 @@ def project_template(file) "#{File.expand_path(File.dirname(__FILE__))}/../lib/squib/project_template/#{file}" end -def doc(file) - "#{File.expand_path(File.dirname(__FILE__))}/../docs/#{file}" +def doc_dsl_rst(name) + 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 def conf(file) diff --git a/squib.gemspec b/squib.gemspec index 9b95889..5fc22be 100644 --- a/squib.gemspec +++ b/squib.gemspec @@ -42,6 +42,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'rsvg2', '~> 3.4' spec.add_runtime_dependency 'ruby-progressbar', '~> 1.10' + spec.add_development_dependency 'activesupport' spec.add_development_dependency 'bundler' spec.add_development_dependency 'coveralls', '>= 0.8.21' spec.add_development_dependency 'game_icons'