Skip to content

Commit

Permalink
Generate translator code from check metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Aug 19, 2012
1 parent 25813b6 commit c5db6b6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/cane/abc_check.rb
Expand Up @@ -15,7 +15,7 @@ def self.name; "ABC check"; end
def self.options def self.options
{ {
glob: ['Glob to run ABC metrics over', '{app,lib}/**/*.rb'], 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 end


Expand Down
4 changes: 2 additions & 2 deletions lib/cane/cli/spec.rb
Expand Up @@ -17,7 +17,7 @@ class Spec


def self.defaults(check) def self.defaults(check)
x = check.options.each_with_object({}) {|(k, v), h| 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[:"no_#{check.key}"] = nil
x x
Expand Down Expand Up @@ -49,7 +49,7 @@ def initialize
def parse(args) def parse(args)
parser.parse!(get_default_options + args) parser.parse!(get_default_options + args)


Translator.new(options, OPTIONS).to_hash Translator.new(options, OPTIONS, SIMPLE_CHECKS).to_hash
rescue OptionsHandled rescue OptionsHandled
nil nil
end end
Expand Down
58 changes: 29 additions & 29 deletions lib/cane/cli/translator.rb
Expand Up @@ -5,55 +5,55 @@ module CLI


# Translates CLI options with given defaults to a hash suitable to be # Translates CLI options with given defaults to a hash suitable to be
# passed to `Cane.run`. # passed to `Cane.run`.
class Translator < Struct.new(:options, :defaults) class Translator < Struct.new(:options, :defaults, :checks)
def to_hash def to_hash
result = {} result = {}
translate_abc_options(result) checks.each do |check|
translate_doc_options(result) translate_options(result, check)
translate_style_options(result) end


result[:threshold] = options.fetch(:threshold, []) 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 result
end end


def translate_abc_options(result) def translate_options(result, check)
result[:abc] = { unless check_disabled(check)
glob: option_with_default(:abc_glob), result[check.key] = {
max: option_with_default(:abc_max).to_i, exclusions: exclusions_for(check.key)
exclusions: exclusions_for('abc') }.merge(extract_options(check))
} unless check_disabled(:no_abc, [:abc_glob, :abc_max]) end
end end


def translate_style_options(result) def extract_options(check)
result[:style] = { check.options.each_with_object({}) do |(k, v), h|
glob: option_with_default(:style_glob), h[k] = cast_for(v).call(options.fetch(to_cli_key(check, k), v[1]))
measure: option_with_default(:style_measure).to_i, end
exclusions: exclusions_for('style')
} unless check_disabled(:no_style, [:style_glob])
end end


def translate_doc_options(result) private
result[:doc] = {
glob: option_with_default(:doc_glob), def check_disabled(check)
} unless check_disabled(:no_doc, [:doc_glob]) disable_key = :"no_#{check.key}"
end params = check.options.keys.map {|x| to_cli_key(check, x) }


def check_disabled(check, params) relevant_options = options.keys & params + [disable_key]
relevant_options = options.keys & params + [check]


check == relevant_options[-1] disable_key == relevant_options[-1]
end end


def option_with_default(key) def cast_for(v)
options.fetch(key, defaults.fetch(key)) (v[2] || ->(x){ x }).to_proc
end end


private def to_cli_key(check, k)
:"#{check.key}_#{k}"
end


def exclusions_for(tool) def exclusions_for(tool)
Array(exclusions[tool]) Array(exclusions[tool.to_s])
end end


def exclusions def exclusions
Expand Down
5 changes: 4 additions & 1 deletion lib/cane/rake_task.rb
Expand Up @@ -52,7 +52,10 @@ def options
end end


def translated_options 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 end
end end
2 changes: 1 addition & 1 deletion lib/cane/style_check.rb
Expand Up @@ -15,7 +15,7 @@ def self.name; "style checking"; end
def self.options def self.options
{ {
glob: ['Glob to run style checks over', '{app,lib,spec}/**/*.rb'], glob: ['Glob to run style checks over', '{app,lib,spec}/**/*.rb'],
measure: ['Max line length', '80'] measure: ['Max line length', '80', :to_i]
} }
end end


Expand Down

0 comments on commit c5db6b6

Please sign in to comment.