Created
March 12, 2021 20:40
-
-
Save dvinciguerra/ec2ef2935ee14adee6f8674248b74554 to your computer and use it in GitHub Desktop.
My personal GUT calculation classes (label extraction support)
This file contains hidden or 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
| module GUT | |
| extend self | |
| MIN_CRITICAL_SCORE = 20 | |
| module ScoreFromLabels | |
| def self.included(base) | |
| base.extend(ClassMethods) | |
| end | |
| module ClassMethods | |
| SCORE_RANGE = (0.0..5.0) | |
| SCORE_PATTERN = /^\[GUT\]\[(\d+\.?\d*)\]/ | |
| def gut_label?(value) | |
| value.match?(self::SCORE_PATTERN) | |
| end | |
| def score_from_label(value) | |
| score = capture_score(value) | |
| return 0.0 unless SCORE_RANGE.include? score | |
| score | |
| end | |
| def capture_score(value) | |
| regex = value.match(self::SCORE_PATTERN) | |
| (regex[1] rescue 0).to_f | |
| end | |
| end | |
| end | |
| module Gravity | |
| include GUT::ScoreFromLabels | |
| SCORE_PATTERN = /^\[G\]\[(\d+\.?\d*)\]/ | |
| end | |
| module Urgency | |
| include GUT::ScoreFromLabels | |
| SCORE_PATTERN = /^\[U\]\[(\d+\.?\d*)\]/ | |
| end | |
| module Tendency | |
| include GUT::ScoreFromLabels | |
| SCORE_PATTERN = /^\[T\]\[(\d+\.?\d*)\]/ | |
| end | |
| # Retrieve a Gravity score from label matching pattern [G][N] where N 0.0 to 5.0 | |
| # | |
| # @param [String] label value pattern starting with '[G][N]' | |
| # @return [Float] score value | |
| def gravity_score(value) | |
| GUT::Gravity.score_from_label(value) | |
| end | |
| # Retrieve a Urgency score from label matching pattern [U][N] where N 0.0 to 5.0 | |
| # | |
| # @param [String] label value pattern starting with '[U][N]' | |
| # @return [Float] score value | |
| def urgency_score(value) | |
| GUT::Urgency.score_from_label(value) | |
| end | |
| # Retrieve a Tendency score from label matching pattern [T][N] where N 0.0 to 5.0 | |
| # | |
| # @param [String] label value pattern starting with '[T][N]' | |
| # @return [Float] score value | |
| def tendency_score(value) | |
| GUT::Tendency.score_from_label(value) | |
| end | |
| # Retrieve a calculated GUT score from labels matching pattern [G|U|T][N] where N 0.0 to 5.0 | |
| # | |
| # @param gravity_value [String] | |
| # @param urgency_value [String] | |
| # @param tendency_value [String] | |
| # @return [Float] total calculated score value | |
| def calculate_score_from_labels(gravity_value, urgency_value, tendency_value) | |
| gravity = gravity_score(gravity_value) | |
| tendency = tendency_score(tendency_value) | |
| urgency = urgency_score(urgency_value) | |
| calculate_score(gravity, urgency, tendency) | |
| end | |
| # Retrieve a calculated GUT score from numeric values | |
| # | |
| # @param gravity_value [Number] | |
| # @param urgency_value [Number] | |
| # @param tendency_value [Number] | |
| # @return [Float] total calculated score value | |
| def calculate_score(gravity_value, urgency_value, tendency_value) | |
| gravity_value * urgency_value * tendency_value | |
| end | |
| # Is a total score critical? | |
| # | |
| # @param total_score [Number] | |
| # @return [Boolean] | |
| def critical_score?(total_score) | |
| total_score > MIN_CRITICAL_SCORE | |
| end | |
| end | |
| ## TESTING | |
| require 'minitest/autorun' | |
| describe GUT do | |
| describe ".gravity_score" do | |
| describe "when label pattern matches" do | |
| it "returns expected score value" do | |
| label = '[G][1]' | |
| score = GUT.gravity_score(label) | |
| _(score).must_equal 1.0 | |
| end | |
| end | |
| describe "when label pattern is invalid" do | |
| it "returns zero score value" do | |
| label = '[XXX][1]' | |
| score = GUT.gravity_score(label) | |
| _(score).must_equal 0.0 | |
| end | |
| end | |
| end | |
| describe ".urgency_score" do | |
| describe "when label pattern matches" do | |
| it "returns expected score value" do | |
| label = '[U][1]' | |
| score = GUT.urgency_score(label) | |
| _(score).must_equal 1.0 | |
| end | |
| end | |
| describe "when label pattern is invalid" do | |
| it "returns zero score value" do | |
| label = '[XXX][1]' | |
| score = GUT.urgency_score(label) | |
| _(score).must_equal 0.0 | |
| end | |
| end | |
| end | |
| describe ".tendency_score" do | |
| describe "when label pattern matches" do | |
| it "returns expected score value" do | |
| label = '[T][1]' | |
| score = GUT.tendency_score(label) | |
| _(score).must_equal 1.0 | |
| end | |
| end | |
| describe "when label pattern is invalid" do | |
| it "returns zero score value" do | |
| label = '[XXX][1]' | |
| score = GUT.tendency_score(label) | |
| _(score).must_equal 0.0 | |
| end | |
| end | |
| end | |
| describe ".calculate_score_from_labels" do | |
| it "returns expected total score value" do | |
| score = GUT.calculate_score_from_labels( | |
| '[G][2]', | |
| '[U][2]', | |
| '[T][2]', | |
| ) | |
| _(score).must_equal 8.0 | |
| end | |
| end | |
| describe ".calculate_score" do | |
| it "returns expected total score value" do | |
| gravity_value, urgency_value, tendency_value = [2, 2, 2] | |
| score = GUT.calculate_score(gravity_value, urgency_value, tendency_value) | |
| _(score).must_equal 8.0 | |
| end | |
| end | |
| describe ".critical_score?" do | |
| it { _(GUT::MIN_CRITICAL_SCORE).must_equal 20 } | |
| it "returns true for scores greater than MIN_CRITICAL_SCORE" do | |
| gravity_value, urgency_value, tendency_value = [5, 5, 5] | |
| score = GUT.calculate_score(gravity_value, urgency_value, tendency_value) | |
| _(GUT.critical_score?(score)).must_equal true | |
| end | |
| it "returns false for scores greater than MIN_CRITICAL_SCORE" do | |
| gravity_value, urgency_value, tendency_value = [2, 2, 3] | |
| score = GUT.calculate_score(gravity_value, urgency_value, tendency_value) | |
| _(GUT.critical_score?(score)).must_equal false | |
| end | |
| end | |
| end | |
| describe GUT::Gravity do | |
| describe ".gut_label?" do | |
| describe "when label pattern matches" do | |
| it { _(GUT::Gravity.gut_label?('[G][5]')).must_equal true } | |
| end | |
| describe "when label pattern is invalid" do | |
| it { _(GUT::Gravity.gut_label?('[XXX][5]')).must_equal false } | |
| end | |
| end | |
| describe ".score_from_label" do | |
| describe "when label pattern matches" do | |
| it { _(GUT::Gravity.score_from_label('[G][5]')).must_equal 5.0 } | |
| end | |
| describe "when label pattern is invalid" do | |
| it { _(GUT::Gravity.score_from_label('[g][5]')).must_equal 0.0 } | |
| it { _(GUT::Gravity.score_from_label('[X][5]')).must_equal 0.0 } | |
| it { _(GUT::Gravity.score_from_label('[G][15]')).must_equal 0.0 } | |
| end | |
| end | |
| end | |
| describe GUT::Urgency do | |
| describe ".gut_label?" do | |
| describe "when label pattern matches" do | |
| it { _(GUT::Urgency.gut_label?('[U][5]')).must_equal true } | |
| end | |
| describe "when label pattern is invalid" do | |
| it { _(GUT::Urgency.gut_label?('[XXX][5]')).must_equal false } | |
| end | |
| end | |
| describe ".score_from_label" do | |
| describe "when label pattern matches" do | |
| it { _(GUT::Urgency.score_from_label('[U][5]')).must_equal 5.0 } | |
| end | |
| describe "when label pattern is invalid" do | |
| it { _(GUT::Urgency.score_from_label('[u][5]')).must_equal 0.0 } | |
| it { _(GUT::Urgency.score_from_label('[X][5]')).must_equal 0.0 } | |
| it { _(GUT::Urgency.score_from_label('[U][15]')).must_equal 0.0 } | |
| end | |
| end | |
| end | |
| describe GUT::Tendency do | |
| describe ".gut_label?" do | |
| describe "when label pattern matches" do | |
| it { _(GUT::Tendency.gut_label?('[T][5]')).must_equal true } | |
| end | |
| describe "when label pattern is invalid" do | |
| it { _(GUT::Tendency.gut_label?('[XXX][5]')).must_equal false } | |
| end | |
| end | |
| describe ".score_from_label" do | |
| describe "when label pattern matches" do | |
| it { _(GUT::Tendency.score_from_label('[T][5]')).must_equal 5.0 } | |
| end | |
| describe "when label pattern is invalid" do | |
| it { _(GUT::Tendency.score_from_label('[t][5]')).must_equal 0.0 } | |
| it { _(GUT::Tendency.score_from_label('[X][5]')).must_equal 0.0 } | |
| it { _(GUT::Tendency.score_from_label('[T][15]')).must_equal 0.0 } | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment