forked from joshk/completeness-fu
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved setup logic into a builder clean room so the defines block can …
…not be used for messing with the internals for AR
- Loading branch information
Showing
3 changed files
with
53 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module CompletenessFu | ||
|
||
# A simple clean room for setting up the completeness check information | ||
class ScoringBuilder | ||
|
||
attr_accessor :completeness_checks, :model_weightings, :cache_score_details, :default_weighting | ||
|
||
def self.generate(model, &block) | ||
sb = ScoringBuilder.new | ||
|
||
sb.completeness_checks = [] | ||
sb.default_weighting = CompletenessFu.default_weightings | ||
sb.model_weightings = CompletenessFu.common_weightings | ||
|
||
sb.instance_eval(&block) | ||
|
||
{ :completeness_checks => sb.completeness_checks, | ||
:model_weightings => sb.model_weightings, | ||
:cache_score_details => sb.cache_score_details, | ||
:default_weighting => sb.default_weighting } | ||
end | ||
|
||
|
||
private | ||
|
||
def check(name, check, weighting = nil) | ||
weighting ||= self.default_weighting | ||
weighting = self.model_weightings[weighting] if weighting.is_a?(Symbol) | ||
self.completeness_checks << { :name => name, :check => check, :weighting => weighting} | ||
end | ||
|
||
def weightings(custom_weighting_opts) | ||
use_common = custom_weighting_opts.delete(:merge_with_common) | ||
if use_common | ||
self.model_weightings.merge!(custom_weights) | ||
else | ||
self.model_weightings = custom_weighting_opts | ||
end | ||
end | ||
|
||
def cache_score(score_type = :relative) | ||
self.cache_score_details = lambda { |instance| instance.send :cache_completeness_score, score_type } | ||
end | ||
end | ||
|
||
end |