From c5db6b64903d2de31605028cf938ea082f84d4f4 Mon Sep 17 00:00:00 2001 From: Xavier Shay Date: Sat, 18 Aug 2012 18:50:48 -0700 Subject: [PATCH] Generate translator code from check metadata. --- lib/cane/abc_check.rb | 2 +- lib/cane/cli/spec.rb | 4 +-- lib/cane/cli/translator.rb | 58 +++++++++++++++++++------------------- lib/cane/rake_task.rb | 5 +++- lib/cane/style_check.rb | 2 +- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/lib/cane/abc_check.rb b/lib/cane/abc_check.rb index a0e0738..9a9b3d3 100644 --- a/lib/cane/abc_check.rb +++ b/lib/cane/abc_check.rb @@ -15,7 +15,7 @@ def self.name; "ABC check"; end def self.options { glob: ['Glob to run ABC metrics over', '{app,lib}/**/*.rb'], - max: ['Ignore methods under this complexity', '15'] + max: ['Ignore methods under this complexity', '15', :to_i] } end diff --git a/lib/cane/cli/spec.rb b/lib/cane/cli/spec.rb index 317a15e..c918c7a 100644 --- a/lib/cane/cli/spec.rb +++ b/lib/cane/cli/spec.rb @@ -17,7 +17,7 @@ class Spec def self.defaults(check) x = check.options.each_with_object({}) {|(k, v), h| - h[("%s_%s" % [check.key, k]).to_sym] = v[1] + h[:"#{check.key}_#{k}"] = v[1] } x[:"no_#{check.key}"] = nil x @@ -49,7 +49,7 @@ def initialize def parse(args) parser.parse!(get_default_options + args) - Translator.new(options, OPTIONS).to_hash + Translator.new(options, OPTIONS, SIMPLE_CHECKS).to_hash rescue OptionsHandled nil end diff --git a/lib/cane/cli/translator.rb b/lib/cane/cli/translator.rb index 03d61cf..dbd236f 100644 --- a/lib/cane/cli/translator.rb +++ b/lib/cane/cli/translator.rb @@ -5,55 +5,55 @@ module CLI # Translates CLI options with given defaults to a hash suitable to be # passed to `Cane.run`. - class Translator < Struct.new(:options, :defaults) + class Translator < Struct.new(:options, :defaults, :checks) def to_hash result = {} - translate_abc_options(result) - translate_doc_options(result) - translate_style_options(result) + checks.each do |check| + translate_options(result, check) + end result[:threshold] = options.fetch(:threshold, []) - result[:max_violations] = option_with_default(:max_violations).to_i + result[:max_violations] = + options.fetch(:max_violations, defaults[:max_violations]).to_i result end - def translate_abc_options(result) - result[:abc] = { - glob: option_with_default(:abc_glob), - max: option_with_default(:abc_max).to_i, - exclusions: exclusions_for('abc') - } unless check_disabled(:no_abc, [:abc_glob, :abc_max]) + def translate_options(result, check) + unless check_disabled(check) + result[check.key] = { + exclusions: exclusions_for(check.key) + }.merge(extract_options(check)) + end end - def translate_style_options(result) - result[:style] = { - glob: option_with_default(:style_glob), - measure: option_with_default(:style_measure).to_i, - exclusions: exclusions_for('style') - } unless check_disabled(:no_style, [:style_glob]) + def extract_options(check) + check.options.each_with_object({}) do |(k, v), h| + h[k] = cast_for(v).call(options.fetch(to_cli_key(check, k), v[1])) + end end - def translate_doc_options(result) - result[:doc] = { - glob: option_with_default(:doc_glob), - } unless check_disabled(:no_doc, [:doc_glob]) - end + private + + def check_disabled(check) + disable_key = :"no_#{check.key}" + params = check.options.keys.map {|x| to_cli_key(check, x) } - def check_disabled(check, params) - relevant_options = options.keys & params + [check] + relevant_options = options.keys & params + [disable_key] - check == relevant_options[-1] + disable_key == relevant_options[-1] end - def option_with_default(key) - options.fetch(key, defaults.fetch(key)) + def cast_for(v) + (v[2] || ->(x){ x }).to_proc end - private + def to_cli_key(check, k) + :"#{check.key}_#{k}" + end def exclusions_for(tool) - Array(exclusions[tool]) + Array(exclusions[tool.to_s]) end def exclusions diff --git a/lib/cane/rake_task.rb b/lib/cane/rake_task.rb index b972b77..e0bd4a6 100644 --- a/lib/cane/rake_task.rb +++ b/lib/cane/rake_task.rb @@ -52,7 +52,10 @@ def options end def translated_options - Cane::CLI::Translator.new(options, OPTIONS).to_hash + Cane::CLI::Translator.new( + options, + OPTIONS, Cane::CLI::Spec::SIMPLE_CHECKS + ).to_hash end end end diff --git a/lib/cane/style_check.rb b/lib/cane/style_check.rb index b9d1bd1..0df27a8 100644 --- a/lib/cane/style_check.rb +++ b/lib/cane/style_check.rb @@ -15,7 +15,7 @@ def self.name; "style checking"; end def self.options { glob: ['Glob to run style checks over', '{app,lib,spec}/**/*.rb'], - measure: ['Max line length', '80'] + measure: ['Max line length', '80', :to_i] } end