yardstick 0.9.6 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.rspec +4 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +11 -16
  6. data/CONTRIBUTING.md +11 -0
  7. data/Gemfile +3 -59
  8. data/Gemfile.devtools +54 -0
  9. data/Guardfile +32 -0
  10. data/LICENSE +1 -1
  11. data/README.md +71 -55
  12. data/Rakefile +2 -7
  13. data/TODO +12 -0
  14. data/bin/yardstick +1 -0
  15. data/config/devtools.yml +2 -0
  16. data/config/flay.yml +2 -2
  17. data/config/flog.yml +1 -1
  18. data/config/mutant.yml +3 -0
  19. data/config/{site.reek → reek.yml} +31 -25
  20. data/config/rubocop.yml +49 -0
  21. data/config/yardstick.yml +31 -0
  22. data/lib/yardstick.rb +35 -15
  23. data/lib/yardstick/cli.rb +7 -7
  24. data/lib/yardstick/config.rb +176 -0
  25. data/lib/yardstick/document.rb +215 -0
  26. data/lib/yardstick/document_set.rb +21 -0
  27. data/lib/yardstick/measurement.rb +18 -145
  28. data/lib/yardstick/measurement_set.rb +2 -2
  29. data/lib/yardstick/parser.rb +61 -0
  30. data/lib/yardstick/processor.rb +21 -35
  31. data/lib/yardstick/rake/measurement.rb +7 -49
  32. data/lib/yardstick/rake/verify.rb +28 -55
  33. data/lib/yardstick/report_output.rb +57 -0
  34. data/lib/yardstick/rule.rb +64 -37
  35. data/lib/yardstick/rule_config.rb +56 -0
  36. data/lib/yardstick/rules/api_tag.rb +95 -0
  37. data/lib/yardstick/rules/example_tag.rb +33 -0
  38. data/lib/yardstick/rules/return_tag.rb +21 -0
  39. data/lib/yardstick/rules/summary.rb +87 -0
  40. data/lib/yardstick/version.rb +1 -1
  41. data/lib/yardstick/yard_ext.rb +1 -6
  42. data/spec/integration/yardstick/class_methods/measure_spec.rb +22 -0
  43. data/spec/integration/yardstick/class_methods/measure_string_spec.rb +20 -0
  44. data/spec/integration/yardstick/document/file_spec.rb +16 -0
  45. data/spec/integration/yardstick/document/line_spec.rb +25 -0
  46. data/spec/integration/yardstick/document/path_spec.rb +45 -0
  47. data/spec/integration/yardstick/processor/process_string_spec.rb +401 -0
  48. data/spec/integration/yardstick/rake/measurement_spec.rb +22 -0
  49. data/spec/integration/yardstick/rake/verify_spec.rb +19 -0
  50. data/spec/shared/measured_itself_behavior.rb +15 -0
  51. data/spec/spec_helper.rb +13 -26
  52. data/spec/support/document_mock.rb +16 -0
  53. data/spec/support/rules.rb +51 -0
  54. data/spec/unit/yardstick/class_methods/measure_spec.rb +31 -0
  55. data/spec/unit/yardstick/class_methods/measure_string_spec.rb +33 -0
  56. data/spec/unit/yardstick/cli_spec.rb +11 -8
  57. data/spec/unit/yardstick/config/class_methods/coerce_spec.rb +32 -0
  58. data/spec/unit/yardstick/config/for_rule_spec.rb +47 -0
  59. data/spec/unit/yardstick/config/initialize_spec.rb +21 -0
  60. data/spec/unit/yardstick/config/output_spec.rb +15 -0
  61. data/spec/unit/yardstick/config/output_writer_spec.rb +20 -0
  62. data/spec/unit/yardstick/config/path_spec.rb +13 -0
  63. data/spec/unit/yardstick/config/path_writer_spec.rb +11 -0
  64. data/spec/unit/yardstick/config/require_exact_threshold_predicate_spec.rb +19 -0
  65. data/spec/unit/yardstick/config/require_exact_threshold_writer_spec.rb +19 -0
  66. data/spec/unit/yardstick/config/set_defaults_spec.rb +42 -0
  67. data/spec/unit/yardstick/config/threshold_writer_spec.rb +15 -0
  68. data/spec/unit/yardstick/config/verbose_predicate_spec.rb +19 -0
  69. data/spec/unit/yardstick/config/verbose_writer_spec.rb +19 -0
  70. data/spec/unit/yardstick/document/api_predicate_spec.rb +25 -0
  71. data/spec/unit/yardstick/document/class_methods/measure_spec.rb +25 -0
  72. data/spec/unit/yardstick/document/class_methods/register_rule_spec.rb +19 -0
  73. data/spec/unit/yardstick/document/class_methods/registered_rules_spec.rb +9 -0
  74. data/spec/unit/yardstick/document/docstring_spec.rb +11 -0
  75. data/spec/unit/yardstick/document/file_spec.rb +14 -0
  76. data/spec/unit/yardstick/document/has_tag_predicate_spec.rb +15 -0
  77. data/spec/unit/yardstick/document/line_spec.rb +12 -0
  78. data/spec/unit/yardstick/document/null_tag/text_spec.rb +9 -0
  79. data/spec/unit/yardstick/document/null_tag/types_spec.rb +9 -0
  80. data/spec/unit/yardstick/document/path_spec.rb +12 -0
  81. data/spec/unit/yardstick/document/summary_text_spec.rb +23 -0
  82. data/spec/unit/yardstick/document/tag_text_spec.rb +17 -0
  83. data/spec/unit/yardstick/document/tag_types_spec.rb +18 -0
  84. data/spec/unit/yardstick/document/visibility_spec.rb +22 -0
  85. data/spec/unit/yardstick/document_set/measure_spec.rb +28 -0
  86. data/spec/unit/yardstick/measurement/description_spec.rb +12 -0
  87. data/spec/unit/yardstick/measurement/initialize_spec.rb +12 -0
  88. data/spec/unit/yardstick/measurement/ok_predicate_spec.rb +21 -0
  89. data/spec/unit/yardstick/measurement/puts_spec.rb +62 -0
  90. data/spec/unit/yardstick/measurement/skip_predicate_spec.rb +27 -0
  91. data/spec/unit/yardstick/measurement_set/coverage_spec.rb +23 -0
  92. data/spec/unit/yardstick/measurement_set/failed_spec.rb +28 -0
  93. data/spec/unit/yardstick/measurement_set/initialize_spec.rb +27 -0
  94. data/spec/unit/yardstick/measurement_set/puts_spec.rb +63 -0
  95. data/spec/unit/yardstick/measurement_set/successful_spec.rb +13 -0
  96. data/spec/unit/yardstick/measurement_set/total_spec.rb +12 -0
  97. data/spec/unit/yardstick/ordered_set/each_spec.rb +18 -0
  98. data/spec/unit/yardstick/ordered_set/empty_predicate_spec.rb +19 -0
  99. data/spec/unit/yardstick/ordered_set/include_predicate_spec.rb +21 -0
  100. data/spec/unit/yardstick/ordered_set/index_spec.rb +21 -0
  101. data/spec/unit/yardstick/ordered_set/left_shift_operator_spec.rb +16 -0
  102. data/spec/unit/yardstick/ordered_set/length_spec.rb +11 -0
  103. data/spec/unit/yardstick/ordered_set/merge_spec.rb +16 -0
  104. data/spec/unit/yardstick/parser/class_methods/parse_paths_spec.rb +28 -0
  105. data/spec/unit/yardstick/parser/class_methods/parse_string_spec.rb +28 -0
  106. data/spec/unit/yardstick/processor/process_spec.rb +17 -0
  107. data/spec/unit/yardstick/processor/process_string_spec.rb +17 -0
  108. data/spec/unit/yardstick/rake/measurement/initialize_spec.rb +64 -0
  109. data/spec/unit/yardstick/rake/measurement/yardstick_measure_spec.rb +22 -0
  110. data/spec/unit/yardstick/rake/verify/initialize_spec.rb +72 -0
  111. data/spec/unit/yardstick/rake/verify/verify_measurements_spec.rb +85 -0
  112. data/spec/unit/yardstick/report_output/class_methods/coerce_spec.rb +18 -0
  113. data/spec/unit/yardstick/report_output/initialize_spec.rb +11 -0
  114. data/spec/unit/yardstick/report_output/to_s_spec.rb +12 -0
  115. data/spec/unit/yardstick/report_output/write_spec.rb +31 -0
  116. data/spec/unit/yardstick/round_percentage_spec.rb +9 -0
  117. data/spec/unit/yardstick/rule/class_methods/coerce_spec.rb +21 -0
  118. data/spec/unit/yardstick/rule/class_methods/inherited_spec.rb +15 -0
  119. data/spec/unit/yardstick/rule/enabled_predicate_spec.rb +18 -0
  120. data/spec/unit/yardstick/rule/initialize_spec.rb +25 -0
  121. data/spec/unit/yardstick/rule/validatable_predicate_spec.rb +11 -0
  122. data/spec/unit/yardstick/rule_config/enabled_for_path_predicate_spec.rb +41 -0
  123. data/spec/unit/yardstick/rule_config/initialize_spec.rb +16 -0
  124. data/spec/unit/yardstick/rules/api_tag/inclusion/valid_predicate_spec.rb +27 -0
  125. data/spec/unit/yardstick/rules/api_tag/presence/valid_predicate_spec.rb +25 -0
  126. data/spec/unit/yardstick/rules/api_tag/private_method/valid_predicate_spec.rb +21 -0
  127. data/spec/unit/yardstick/rules/api_tag/private_method/validatable_predicate_spec.rb +21 -0
  128. data/spec/unit/yardstick/rules/api_tag/protected_method/valid_predicate_spec.rb +25 -0
  129. data/spec/unit/yardstick/rules/api_tag/protected_method/validatable_predicate_spec.rb +21 -0
  130. data/spec/unit/yardstick/rules/example_tag/valid_predicate_spec.rb +25 -0
  131. data/spec/unit/yardstick/rules/example_tag/validatable_predicate_spec.rb +32 -0
  132. data/spec/unit/yardstick/rules/return_tag/valid_predicate_spec.rb +21 -0
  133. data/spec/unit/yardstick/rules/summary/delimiter/valid_predicate_spec.rb +21 -0
  134. data/spec/unit/yardstick/rules/summary/length/valid_predicate_spec.rb +27 -0
  135. data/spec/unit/yardstick/rules/summary/presence/valid_predicate_spec.rb +21 -0
  136. data/spec/unit/yardstick/rules/summary/presence/validatable_predicate_spec.rb +21 -0
  137. data/spec/unit/yardstick/rules/summary/single_line/valid_predicate_spec.rb +21 -0
  138. data/yardstick.gemspec +7 -11
  139. metadata +222 -65
  140. data/.rvmrc +0 -1
  141. data/lib/yardstick/measurable.rb +0 -67
  142. data/lib/yardstick/method.rb +0 -113
  143. data/lib/yardstick/rule_set.rb +0 -22
  144. data/spec/support/config_alias.rb +0 -5
  145. data/spec/unit/yardstick/measurement_set_spec.rb +0 -266
  146. data/spec/unit/yardstick/measurement_spec.rb +0 -257
  147. data/spec/unit/yardstick/method_spec.rb +0 -357
  148. data/spec/unit/yardstick/rake/measurement_spec.rb +0 -175
  149. data/spec/unit/yardstick/rake/verify_spec.rb +0 -231
  150. data/spec/unit/yardstick/rule_spec.rb +0 -36
  151. data/spec/unit/yardstick_spec.rb +0 -71
  152. data/tasks/metrics/ci.rake +0 -9
  153. data/tasks/metrics/flay.rake +0 -53
  154. data/tasks/metrics/flog.rake +0 -44
  155. data/tasks/metrics/heckle.rake +0 -52
  156. data/tasks/metrics/metric_fu.rake +0 -31
  157. data/tasks/metrics/reek.rake +0 -21
  158. data/tasks/metrics/roodi.rake +0 -19
  159. data/tasks/metrics/yardstick.rake +0 -27
  160. data/tasks/spec.rake +0 -64
  161. data/tasks/yard.rake +0 -11
@@ -0,0 +1,49 @@
1
+ AllCops:
2
+ Includes:
3
+ - '**/*.rake'
4
+ - 'Gemfile'
5
+ - 'Gemfile.devtools'
6
+ Excludes:
7
+ - '**/vendor/**'
8
+
9
+ # Avoid parameter lists longer than five parameters.
10
+ ParameterLists:
11
+ Max: 3
12
+ CountKeywordArgs: true
13
+
14
+ # Avoid more than `Max` levels of nesting.
15
+ BlockNesting:
16
+ Max: 3
17
+
18
+ # Align with the style guide.
19
+ CollectionMethods:
20
+ PreferredMethods:
21
+ collect: 'map'
22
+ inject: 'reduce'
23
+ find: 'detect'
24
+ find_all: 'select'
25
+
26
+ # Do not force public/protected/private keyword to be indented at the same
27
+ # level as the def keyword. My personal preference is to outdent these keywords
28
+ # because I think when scanning code it makes it easier to identify the
29
+ # sections of code and visually separate them. When the keyword is at the same
30
+ # level I think it sort of blends in with the def keywords and makes it harder
31
+ # to scan the code and see where the sections are.
32
+ AccessControl:
33
+ Enabled: false
34
+
35
+ # Limit line length
36
+ LineLength:
37
+ Max: 146 # TODO: lower to 79
38
+
39
+ # Disable documentation checking until a class needs to be documented once
40
+ Documentation:
41
+ Enabled: false
42
+
43
+ # Do not favor modifier if/unless usage when you have a single-line body
44
+ IfUnlessModifier:
45
+ Enabled: false
46
+
47
+ # Do not use sprintf instead of String#%.
48
+ FavorSprintf:
49
+ Enabled: false
data/config/yardstick.yml CHANGED
@@ -1,2 +1,33 @@
1
1
  ---
2
2
  threshold: 100
3
+ rules:
4
+ ApiTag::Presence:
5
+ enabled: true
6
+ exclude: []
7
+ ApiTag::Inclusion:
8
+ enabled: true
9
+ exclude: []
10
+ ApiTag::ProtectedMethod:
11
+ enabled: true
12
+ exclude: []
13
+ ApiTag::PrivateMethod:
14
+ enabled: true
15
+ exclude: []
16
+ ExampleTag:
17
+ enabled: true
18
+ exclude: []
19
+ ReturnTag:
20
+ enabled: true
21
+ exclude: []
22
+ Summary::Presence:
23
+ enabled: true
24
+ exclude: []
25
+ Summary::Length:
26
+ enabled: true
27
+ exclude: []
28
+ Summary::Delimiter:
29
+ enabled: true
30
+ exclude: []
31
+ Summary::SingleLine:
32
+ enabled: true
33
+ exclude: []
data/lib/yardstick.rb CHANGED
@@ -1,21 +1,29 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'pathname'
4
- require 'rational'
4
+ require 'delegate'
5
5
 
6
6
  require 'backports'
7
7
  require 'yard'
8
8
 
9
9
  require 'yardstick/ordered_set'
10
- require 'yardstick/rule_set'
11
10
  require 'yardstick/measurement'
12
11
 
13
- require 'yardstick/measurement_set'
14
- require 'yardstick/processor'
12
+ require 'yardstick/config'
13
+ require 'yardstick/report_output'
14
+ require 'yardstick/document'
15
+
15
16
  require 'yardstick/rule'
17
+ require 'yardstick/rule_config'
18
+ require 'yardstick/rules/api_tag'
19
+ require 'yardstick/rules/example_tag'
20
+ require 'yardstick/rules/summary'
21
+ require 'yardstick/rules/return_tag'
16
22
 
17
- require 'yardstick/measurable'
18
- require 'yardstick/method'
23
+ require 'yardstick/measurement_set'
24
+ require 'yardstick/document_set'
25
+ require 'yardstick/processor'
26
+ require 'yardstick/parser'
19
27
 
20
28
  require 'yardstick/yard_ext'
21
29
 
@@ -27,19 +35,18 @@ module Yardstick
27
35
  # Measure a list of files
28
36
  #
29
37
  # @example
30
- # Yardstick.measure('article.rb') # => [ Measurement ]
38
+ # config = Yardstick::Config.coerce(path: 'article.rb')
39
+ # Yardstick.measure(config) # => [ MeasurementSet ]
31
40
  #
32
- # @param [Array<#to_s>, #to_s] path
33
- # optional list of paths to measure
34
- # @param [Hash] options
41
+ # @param [Config] config
35
42
  # optional configuration
36
43
  #
37
44
  # @return [Yardstick::MeasurementSet]
38
45
  # the measurements for each file
39
46
  #
40
47
  # @api public
41
- def self.measure(path = 'lib/**/*.rb', options = {})
42
- Processor.process_path(path)
48
+ def self.measure(config = Config.new)
49
+ Processor.new(config).process
43
50
  end
44
51
 
45
52
  # Measure a string of code and YARD documentation
@@ -51,15 +58,28 @@ module Yardstick
51
58
  #
52
59
  # @param [#to_str] string
53
60
  # the string to measure
54
- # @param [Hash] options
61
+ # @param [Config] config
55
62
  # optional configuration
56
63
  #
57
64
  # @return [Yardstick::MeasurementSet]
58
65
  # the measurements for the string
59
66
  #
60
67
  # @api public
61
- def self.measure_string(string, options = {})
62
- Processor.process_string(string)
68
+ def self.measure_string(string, config = Config.new)
69
+ Processor.new(config).process_string(string)
70
+ end
71
+
72
+ # Round percentage to 1/10th of a percent
73
+ #
74
+ # @param [Float] percentage
75
+ # the percentage to round
76
+ #
77
+ # @return [Float]
78
+ # the rounded percentage
79
+ #
80
+ # @api private
81
+ def self.round_percentage(percentage)
82
+ (percentage * 10).floor / 10.0
63
83
  end
64
84
 
65
85
  end # module Yardstick
data/lib/yardstick/cli.rb CHANGED
@@ -20,7 +20,7 @@ module Yardstick
20
20
  #
21
21
  # @api public
22
22
  def self.run(*args)
23
- measurements = Yardstick.measure(*parse_options(args))
23
+ measurements = Yardstick.measure(parse_config(args))
24
24
  measurements.puts
25
25
  measurements
26
26
  end
@@ -30,29 +30,29 @@ module Yardstick
30
30
  # @param [Array<String>] args
31
31
  # the command-line options
32
32
  #
33
- # @return [Array(Array<String>, Hash)]
33
+ # @return [Config]
34
34
  # the list of files, and options parsed from the command-line
35
35
  #
36
36
  # @api private
37
- def self.parse_options(args)
37
+ def self.parse_config(args)
38
38
  args << '--help' if args.empty?
39
39
  options = {}
40
40
  option_parser(options).parse!(args)
41
- [ args, options ]
41
+ Config.new(options.merge(path: args))
42
42
  rescue OptionParser::InvalidOption => error
43
43
  display_exit(error.message)
44
44
  end
45
45
 
46
46
  # Return an OptionParser instance for the command-line app
47
47
  #
48
- # @param [Hash] options
48
+ # @param [Hash] _options
49
49
  # the options to set when parsing the command-line arguments
50
50
  #
51
51
  # @return [Yardstick::OptionParser]
52
52
  # the option parser instance
53
53
  #
54
54
  # @api private
55
- def self.option_parser(options)
55
+ def self.option_parser(_options)
56
56
  opts = OptionParser.new
57
57
  opts.on_tail('-v', '--version', 'print version information and exit') { display_exit("#{opts.program_name} #{Yardstick::VERSION}") }
58
58
  opts.on_tail('-h', '--help', 'display this help and exit') { display_exit(opts.to_s) }
@@ -73,7 +73,7 @@ module Yardstick
73
73
  end
74
74
 
75
75
  class << self
76
- private :option_parser, :parse_options, :display_exit
76
+ private :option_parser, :parse_config, :display_exit
77
77
  end
78
78
 
79
79
  end # module CLI
@@ -0,0 +1,176 @@
1
+ # encoding: utf-8
2
+
3
+ module Yardstick
4
+ # Handles Yardstick configuration
5
+ #
6
+ class Config
7
+ InvalidRule = Class.new(StandardError)
8
+
9
+ NAMESPACE_PREFIX = 'Yardstick::Rules::'.freeze
10
+
11
+ # Set the threshold
12
+ #
13
+ # @return [undefined]
14
+ #
15
+ # @api public
16
+ attr_writer :threshold
17
+
18
+ # Threshold value
19
+ #
20
+ # @return [Integer]
21
+ #
22
+ # @api private
23
+ attr_reader :threshold
24
+
25
+ # Specify if the threshold should match the coverage
26
+ #
27
+ # @return [undefined]
28
+ #
29
+ # @api public
30
+ attr_writer :require_exact_threshold
31
+
32
+ # List of paths to measure
33
+ #
34
+ # @return [undefined]
35
+ #
36
+ # @api public
37
+ attr_writer :path
38
+
39
+ # List of paths to measure
40
+ #
41
+ # @return [String]
42
+ #
43
+ # @api private
44
+ attr_reader :path
45
+
46
+ # Specify if the coverage summary should be displayed
47
+ #
48
+ # @return [undefined]
49
+ #
50
+ # @api public
51
+ attr_writer :verbose
52
+
53
+ # The path to the file where the measurements will be written
54
+ #
55
+ # @return [Yardstick::ReportOutput]
56
+ #
57
+ # @api private
58
+ attr_reader :output
59
+
60
+ # Coerces hash into a config object
61
+ #
62
+ # @param [Hash] hash
63
+ #
64
+ # @yieldparam [Yardstick::Config] config
65
+ # the config object
66
+ #
67
+ # @return [Config]
68
+ #
69
+ # @api private
70
+ def self.coerce(hash, &block)
71
+ new(normalize_hash(hash), &block)
72
+ end
73
+
74
+ # Converts string keys into symbol keys
75
+ #
76
+ # @param [Hash] hash
77
+ #
78
+ # @return [Hash]
79
+ # normalized hash
80
+ #
81
+ # @api private
82
+ def self.normalize_hash(hash)
83
+ hash.each_with_object({}) do |(key, value), normalized_hash|
84
+ normalized_value = value.is_a?(Hash) ? normalize_hash(value) : value
85
+ normalized_hash[key.to_sym] = normalized_value
86
+ end
87
+ end
88
+
89
+ # Initializes new config
90
+ #
91
+ # @param [Hash] options
92
+ # optional configuration
93
+ #
94
+ # @yieldparam [Yardstick::Config] config
95
+ # the config object
96
+ #
97
+ # @return [Yardstick::Config]
98
+ #
99
+ # @api private
100
+ def initialize(options = {}, &block)
101
+ set_defaults(options)
102
+
103
+ yield(self) if block_given?
104
+ end
105
+
106
+ # Return config for given rule
107
+ #
108
+ # @param [Class] rule_class
109
+ #
110
+ # @return [RuleConfig]
111
+ #
112
+ # @api private
113
+ def for_rule(rule_class)
114
+ key = rule_class.to_s[NAMESPACE_PREFIX.length..-1]
115
+
116
+ if key
117
+ RuleConfig.new(@rules.fetch(key.to_sym, {}))
118
+ else
119
+ raise InvalidRule, "every rule must begin with #{NAMESPACE_PREFIX}"
120
+ end
121
+ end
122
+
123
+ # Specify if the coverage summary should be displayed
124
+ #
125
+ # @return [Boolean]
126
+ #
127
+ # @api private
128
+ def verbose?
129
+ @verbose
130
+ end
131
+
132
+ # Return if the threshold should match the coverage
133
+ #
134
+ # @return [Boolean]
135
+ #
136
+ # @api private
137
+ def require_exact_threshold?
138
+ @require_exact_threshold
139
+ end
140
+
141
+ # The path to the file where the measurements will be written
142
+ #
143
+ # @param [String, Pathname] output
144
+ # optional output path for measurements
145
+ #
146
+ # @return [undefined]
147
+ #
148
+ # @api public
149
+ def output=(output)
150
+ @output = ReportOutput.coerce(output)
151
+ end
152
+
153
+ private
154
+
155
+ # Sets default options
156
+ #
157
+ # @param [Hash] options
158
+ # optional configuration
159
+ #
160
+ # @return [undefined]
161
+ #
162
+ # @api private
163
+ def set_defaults(options)
164
+ @threshold = options.fetch(:threshold, 100)
165
+ @verbose = options.fetch(:verbose, true)
166
+ @path = options.fetch(:path, 'lib/**/*.rb')
167
+ @require_exact_threshold = options.fetch(:require_exact_threshold, true)
168
+ @rules = options.fetch(:rules, {})
169
+ self.output = 'measurements/report.txt'
170
+ end
171
+
172
+ class << self
173
+ private :normalize_hash
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,215 @@
1
+ # encoding: utf-8
2
+
3
+ module Yardstick
4
+
5
+ # Wraps a yard docstring to make a nicer interface
6
+ class Document
7
+ @registered_rules = Set.new
8
+
9
+ class << self
10
+ # Shows currently registered rules
11
+ #
12
+ # @return [Array<Class>]
13
+ #
14
+ # @api private
15
+ attr_reader :registered_rules
16
+ end
17
+
18
+ # Register rule with document
19
+ #
20
+ # @param [Class] rule_class
21
+ # subclass of Yardstick::Rule
22
+ #
23
+ # @return [undefined]
24
+ #
25
+ # @api private
26
+ def self.register_rule(rule_class)
27
+ registered_rules << rule_class
28
+ end
29
+
30
+ # Measures docstring against enabled rules
31
+ #
32
+ # @param [Yardstring::Document] document
33
+ # document that will be measured
34
+ # @param [Yardstick::Config] config
35
+ # a configuration
36
+ #
37
+ # @return [MeasurementSet]
38
+ #
39
+ # @api private
40
+ def self.measure(document, config)
41
+ MeasurementSet.new(
42
+ registered_rules.map do |rule_class|
43
+ Measurement.new(rule_class.coerce(document, config))
44
+ end
45
+ )
46
+ end
47
+
48
+ # Return document yard docstring
49
+ #
50
+ # @return [YARD::Docstring]
51
+ #
52
+ # @api private
53
+ attr_reader :docstring
54
+
55
+ # Initializes Document object with docstring
56
+ #
57
+ # @param [Yard::Docstring]
58
+ #
59
+ # @return [undefined]
60
+ #
61
+ # @api private
62
+ def initialize(docstring)
63
+ @docstring = docstring
64
+ end
65
+
66
+ # The raw text for the summary
67
+ #
68
+ # @return [String]
69
+ # the summary text
70
+ #
71
+ # @api private
72
+ def summary_text
73
+ @docstring.split(/\r?\n\r?\n/).first.to_s
74
+ end
75
+
76
+ # Tests if document has a tag
77
+ #
78
+ # @param [String] name
79
+ # the name of the tag
80
+ #
81
+ # @return [Boolean]
82
+ # true if tag exists
83
+ #
84
+ # @api private
85
+ def has_tag?(name)
86
+ @docstring.has_tag?(name)
87
+ end
88
+
89
+ # The text for a specified tag
90
+ #
91
+ # @param [String] tag_name
92
+ # the name of the tag
93
+ #
94
+ # @return [String, nil]
95
+ # the tag text if the tag exists
96
+ #
97
+ # @api private
98
+ def tag_text(tag_name)
99
+ tag(tag_name).text
100
+ end
101
+
102
+ # The types for a specified tag
103
+ #
104
+ # @param [String] tag_name
105
+ # the name of the tag
106
+ #
107
+ # @return [Array<String>]
108
+ # a collection of tag types
109
+ #
110
+ # @api private
111
+ def tag_types(tag_name)
112
+ tag(tag_name).types
113
+ end
114
+
115
+ # The method visibility: public, protected or private
116
+ #
117
+ # @return [Symbol]
118
+ # the visibility of the method
119
+ #
120
+ # @api private
121
+ def visibility
122
+ object.visibility
123
+ end
124
+
125
+ # Check if the method API type matches
126
+ #
127
+ # @param [Array<String>] types
128
+ # a collection of API types
129
+ #
130
+ # @return [Boolean]
131
+ # true if the API type matches
132
+ #
133
+ # @api private
134
+ def api?(types)
135
+ types.include?(tag_text('api'))
136
+ end
137
+
138
+ # The filename for the code
139
+ #
140
+ # @return [Pathname]
141
+ # the filename
142
+ #
143
+ # @api private
144
+ def file
145
+ Pathname(object.file)
146
+ end
147
+
148
+ # The line number for the code
149
+ #
150
+ # @return [Integer]
151
+ # the line number
152
+ #
153
+ # @api private
154
+ def line
155
+ object.line
156
+ end
157
+
158
+ # The class and method name for the code
159
+ #
160
+ # @return [String]
161
+ # the class and method name
162
+ #
163
+ # @api private
164
+ def path
165
+ object.path
166
+ end
167
+
168
+ private
169
+
170
+ # The code object for the docstring
171
+ #
172
+ # @return [YARD::CodeObjects::Base]
173
+ # the code object
174
+ #
175
+ # @api private
176
+ def object
177
+ @docstring.object
178
+ end
179
+
180
+ # Finds tag by tag name
181
+ #
182
+ # @param [String] name
183
+ # the name of the tag
184
+ #
185
+ # @return [YARD::Tags::Tag, NullTag]
186
+ #
187
+ # @api private
188
+ def tag(name)
189
+ @docstring.tag(name) || NullTag.new
190
+ end
191
+
192
+ # Null object for YARD::Tags::Tag
193
+ #
194
+ class NullTag
195
+ # Empty text
196
+ #
197
+ # @return [nil]
198
+ #
199
+ # @api private
200
+ def text
201
+ nil
202
+ end
203
+
204
+ # Empty list of types
205
+ #
206
+ # @return [Array]
207
+ # an empty array
208
+ #
209
+ # @api private
210
+ def types
211
+ []
212
+ end
213
+ end
214
+ end
215
+ end