Class: ReadmeScore::Document::Score
- Inherits:
-
Object
- Object
- ReadmeScore::Document::Score
- Defined in:
- lib/readme-score/document/score.rb
Constant Summary collapse
- SCORE_METRICS =
[ { metric: :number_of_code_blocks, description: "Number of code blocks", value_per: 5, max: 40 }, { metric: :number_of_non_code_sections, description: "Number of non-code sections", value_per: 5, max: 30 }, { metric: :has_lists?, description: "Has any lists?", value: 10 }, { metric: :number_of_images, description: "Number of images", value_per: 5, max: 15 }, { metric: :number_of_gifs, description: "Number of GIFs", value_per: 5, max: 15 }, { metric: :cumulative_code_block_length, description: "Amount of code", value_per: 0.0009475244447271192, max: 10 }, { metric_name: :low_code_block_penalty, description: "Penalty for lack of code blocks", metric: :number_of_code_blocks, if_less_than: 3, value: -10 } ]
Instance Attribute Summary collapse
-
#metrics ⇒ Object
Returns the value of attribute metrics.
Instance Method Summary collapse
- #human_breakdown ⇒ Object
-
#initialize(metrics) ⇒ Score
constructor
A new instance of Score.
- #inspect ⇒ Object
- #score_breakdown(as_description = false) ⇒ Object (also: #breakdown)
- #to_f ⇒ Object
- #total_score ⇒ Object (also: #to_i)
Constructor Details
#initialize(metrics) ⇒ Score
Returns a new instance of Score.
52 53 54 |
# File 'lib/readme-score/document/score.rb', line 52 def initialize(metrics) @metrics = metrics end |
Instance Attribute Details
#metrics ⇒ Object
Returns the value of attribute metrics.
50 51 52 |
# File 'lib/readme-score/document/score.rb', line 50 def metrics @metrics end |
Instance Method Details
#human_breakdown ⇒ Object
82 83 84 |
# File 'lib/readme-score/document/score.rb', line 82 def human_breakdown score_breakdown(true) end |
#inspect ⇒ Object
99 100 101 |
# File 'lib/readme-score/document/score.rb', line 99 def inspect "#<#{self.class} - #{total_score}>" end |
#score_breakdown(as_description = false) ⇒ Object Also known as: breakdown
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/readme-score/document/score.rb', line 56 def score_breakdown(as_description = false) breakdown = {} SCORE_METRICS.each { |h| metric_option = OpenStruct.new(h) metric_name = metric_option.metric_name || metric_option.metric metric_score_value = 0 # points for each occurance if metric_option.value_per metric_score_value = [metrics.send(metric_option.metric) * metric_option.value_per, metric_option.max].min elsif metric_option.if_less_than if metrics.send(metric_option.metric) < metric_option.if_less_than metric_score_value = metric_option.value end else metric_score_value = metrics.send(metric_option.metric) ? metric_option.value : 0 end if as_description breakdown[metric_option.description] = [metric_score_value, metric_option.max || metric_option.value] else breakdown[metric_name] = metric_score_value end } breakdown end |
#to_f ⇒ Object
95 96 97 |
# File 'lib/readme-score/document/score.rb', line 95 def to_f to_i.to_f end |
#total_score ⇒ Object Also known as: to_i
86 87 88 89 90 91 92 |
# File 'lib/readme-score/document/score.rb', line 86 def total_score score = 0 score_breakdown.each {|metric, points| score += points.to_i } [[score, 100].min, 0].max end |