Class: Keela::Baseline
- Inherits:
-
Object
- Object
- Keela::Baseline
- Defined in:
- lib/keela/baseline.rb
Overview
Handles reading and writing baseline files with multiple strategy sections.
File format:
methods:
app/models/user.rb:
- unused_method
scopes:
app/models/user.rb:
- unused_scope
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #data ⇒ Object
- #exists? ⇒ Boolean
- #get(strategy_name) ⇒ Object
-
#initialize(path) ⇒ Baseline
constructor
A new instance of Baseline.
- #load ⇒ Object
- #save ⇒ Object
- #set(strategy_name, unused_collection) ⇒ Object
Constructor Details
#initialize(path) ⇒ Baseline
Returns a new instance of Baseline.
19 20 21 22 |
# File 'lib/keela/baseline.rb', line 19 def initialize(path) @path = path @data = nil end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/keela/baseline.rb', line 17 def path @path end |
Instance Method Details
#data ⇒ Object
34 35 36 |
# File 'lib/keela/baseline.rb', line 34 def data @data ||= load end |
#exists? ⇒ Boolean
24 25 26 |
# File 'lib/keela/baseline.rb', line 24 def exists? path && File.exist?(path) end |
#get(strategy_name) ⇒ Object
38 39 40 |
# File 'lib/keela/baseline.rb', line 38 def get(strategy_name) data[strategy_name] || {} end |
#load ⇒ Object
28 29 30 31 32 |
# File 'lib/keela/baseline.rb', line 28 def load return {} unless exists? @data = YAML.load_file(path) || {} end |
#save ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/keela/baseline.rb', line 49 def save return unless path header = <<~HEADER # Unused code identified by Keela. # These are potential targets for removal. # # If an item listed here is actually in use, # remove it from this file and add it to your excluded file. # HEADER sorted_data = data.sort.to_h yaml_content = if sorted_data.empty? || sorted_data.values.all?(&:empty?) "#{header}---\n{}\n" else "#{header}#{format_yaml(sorted_data)}" end File.write(path, yaml_content) end |
#set(strategy_name, unused_collection) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/keela/baseline.rb', line 42 def set(strategy_name, unused_collection) # Ensure we've loaded existing data before setting load if @data.nil? && exists? @data ||= {} @data[strategy_name] = unused_collection.sort.to_h end |