Permalink
Browse files

Generate translator code from check metadata.

  • Loading branch information...
1 parent 25813b6 commit c5db6b64903d2de31605028cf938ea082f84d4f4 @xaviershay xaviershay committed Aug 19, 2012
Showing with 37 additions and 34 deletions.
  1. +1 −1 lib/cane/abc_check.rb
  2. +2 −2 lib/cane/cli/spec.rb
  3. +29 −29 lib/cane/cli/translator.rb
  4. +4 −1 lib/cane/rake_task.rb
  5. +1 −1 lib/cane/style_check.rb
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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

0 comments on commit c5db6b6

Please sign in to comment.