Class: CukeSniffer::RuleTarget

Inherits:
Object
  • Object
show all
Includes:
Constants, RuleConfig, ROXML
Defined in:
lib/cuke_sniffer/rule_target.rb

Overview

Author

Robert Cochran ([email protected])

Copyright

Copyright © 2014 Robert Cochran

License

Distributes under the MIT License

Parent class for all objects that have rules executed against it Mixins: CukeSniffer::Constants, CukeSniffer::RuleConfig, ROXML

Direct Known Subclasses

FeatureRuleTarget, Hook, StepDefinition

Constant Summary

Constants included from RuleConfig

CukeSniffer::RuleConfig::ERROR, CukeSniffer::RuleConfig::FATAL, CukeSniffer::RuleConfig::INFO, CukeSniffer::RuleConfig::RULES, CukeSniffer::RuleConfig::WARNING

Constants included from Constants

Constants::COMMENT_REGEX, Constants::DATE_REGEX, Constants::DEFAULT_OUTPUT_FILE_NAME, Constants::FILE_IGNORE_LIST, Constants::HOOK_REGEX, Constants::HOOK_STYLES, Constants::MARKUP_SOURCE, Constants::SCENARIO_TITLE_STYLES, Constants::STEP_DEFINITION_REGEX, Constants::STEP_REGEX, Constants::STEP_STYLES, Constants::TAG_REGEX, Constants::THRESHOLDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ RuleTarget

Location must be in the format of “file_pathfile_name.rb:line_number”



35
36
37
38
39
40
# File 'lib/cuke_sniffer/rule_target.rb', line 35

def initialize(location)
  @location = location
  @score = 0
  @rules_hash = {}
  @class_type = self.class.to_s.gsub(/.*::/, "")
end

Instance Attribute Details

#locationObject

string: Location in which the object was found



24
25
26
# File 'lib/cuke_sniffer/rule_target.rb', line 24

def location
  @location
end

#rules_hashObject

hash: Contains the phrase every rule fired against the object and times it fired

  • Key: string

  • Value: int



29
30
31
# File 'lib/cuke_sniffer/rule_target.rb', line 29

def rules_hash
  @rules_hash
end

#scoreObject

int: Sum of the rules fired



21
22
23
# File 'lib/cuke_sniffer/rule_target.rb', line 21

def score
  @score
end

#typeObject

string: Type of the object being evaluated



32
33
34
# File 'lib/cuke_sniffer/rule_target.rb', line 32

def type
  @type
end

Instance Method Details

#==(comparison_object) ⇒ Object

:nodoc:



55
56
57
58
59
# File 'lib/cuke_sniffer/rule_target.rb', line 55

def == (comparison_object) # :nodoc:
  comparison_object.location == location &&
      comparison_object.score == score &&
      comparison_object.rules_hash == rules_hash
end

#good?Boolean

Compares the score against the objects threshold If a score is below the threshold it is good and returns true Return: Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/cuke_sniffer/rule_target.rb', line 45

def good?
  score <= Constants::THRESHOLDS[@class_type]
end

#is_comment?(line) ⇒ Boolean

TODO Abstraction needed for this regex matcher (constants?)

Returns:

  • (Boolean)


62
63
64
# File 'lib/cuke_sniffer/rule_target.rb', line 62

def is_comment?(line)
  true if line =~ /^\#.*$/
end

#problem_percentageObject

Calculates the score to threshold percentage of an object Return: Float



51
52
53
# File 'lib/cuke_sniffer/rule_target.rb', line 51

def problem_percentage
  score.to_f / Constants::THRESHOLDS[@class_type].to_f
end

#store_rule(rule, phrase = rule.phrase) ⇒ Object



66
67
68
69
70
# File 'lib/cuke_sniffer/rule_target.rb', line 66

def store_rule(rule, phrase = rule.phrase)
  @score += rule.score
  @rules_hash[phrase] ||= 0
  @rules_hash[phrase] += 1
end

#store_rule_many_times(rule, count, phrase = rule.phrase) ⇒ Object



72
73
74
75
76
# File 'lib/cuke_sniffer/rule_target.rb', line 72

def store_rule_many_times(rule, count, phrase = rule.phrase)
  count.times do
    store_rule(rule, phrase)
  end
end