Class: FatModelFinder::FileData
- Inherits:
-
Object
- Object
- FatModelFinder::FileData
- Defined in:
- lib/fat_model_finder/file_data.rb
Overview
Class to store attributes of file data, later to be parsed to JSON file for storage
Constant Summary collapse
- METHOD_THRESHOLD =
TODO: we can have a method that overrides these rules and is configurable by the user later. These can just be set as the default values…
10- CALLBACK_THRESHOLD =
5- VALIDATION_THRESHOLD =
5- ASSOCIATION_THRESHOLD =
5- LINE_COUNT_THRESHOLD =
300
Instance Attribute Summary collapse
-
#association_count ⇒ Object
Returns the value of attribute association_count.
-
#callback_count ⇒ Object
Returns the value of attribute callback_count.
-
#char_count ⇒ Object
Returns the value of attribute char_count.
-
#fat ⇒ Object
Returns the value of attribute fat.
-
#fat_model_data ⇒ Object
Returns the value of attribute fat_model_data.
-
#file ⇒ Object
Returns the value of attribute file.
-
#file_extension ⇒ Object
Returns the value of attribute file_extension.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#file_size ⇒ Object
Returns the value of attribute file_size.
-
#is_empty ⇒ Object
Returns the value of attribute is_empty.
-
#last_modified ⇒ Object
Returns the value of attribute last_modified.
-
#line_count ⇒ Object
Returns the value of attribute line_count.
-
#method_count ⇒ Object
Returns the value of attribute method_count.
-
#validation_count ⇒ Object
Returns the value of attribute validation_count.
-
#word_count ⇒ Object
Returns the value of attribute word_count.
Instance Method Summary collapse
- #calculate_if_fat_model ⇒ Object
- #count_associations ⇒ Object
- #count_callbacks ⇒ Object
- #count_methods ⇒ Object
- #count_validations ⇒ Object
-
#initialize(file:) ⇒ FileData
constructor
A new instance of FileData.
- #set_base_attributes ⇒ Object
-
#to_h ⇒ Object
Convert the object to a hash for JSON serialization.
Constructor Details
#initialize(file:) ⇒ FileData
Returns a new instance of FileData.
18 19 20 21 22 23 24 25 26 |
# File 'lib/fat_model_finder/file_data.rb', line 18 def initialize(file:) @file = file @method_count = nil @callback_count = nil @association_count = nil @validation_count = nil @fat = false @fat_model_data = {} end |
Instance Attribute Details
#association_count ⇒ Object
Returns the value of attribute association_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def association_count @association_count end |
#callback_count ⇒ Object
Returns the value of attribute callback_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def callback_count @callback_count end |
#char_count ⇒ Object
Returns the value of attribute char_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def char_count @char_count end |
#fat ⇒ Object
Returns the value of attribute fat.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def fat @fat end |
#fat_model_data ⇒ Object
Returns the value of attribute fat_model_data.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def fat_model_data @fat_model_data end |
#file ⇒ Object
Returns the value of attribute file.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def file @file end |
#file_extension ⇒ Object
Returns the value of attribute file_extension.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def file_extension @file_extension end |
#file_name ⇒ Object
Returns the value of attribute file_name.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def file_name @file_name end |
#file_size ⇒ Object
Returns the value of attribute file_size.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def file_size @file_size end |
#is_empty ⇒ Object
Returns the value of attribute is_empty.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def is_empty @is_empty end |
#last_modified ⇒ Object
Returns the value of attribute last_modified.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def last_modified @last_modified end |
#line_count ⇒ Object
Returns the value of attribute line_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def line_count @line_count end |
#method_count ⇒ Object
Returns the value of attribute method_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def method_count @method_count end |
#validation_count ⇒ Object
Returns the value of attribute validation_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def validation_count @validation_count end |
#word_count ⇒ Object
Returns the value of attribute word_count.
14 15 16 |
# File 'lib/fat_model_finder/file_data.rb', line 14 def word_count @word_count end |
Instance Method Details
#calculate_if_fat_model ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fat_model_finder/file_data.rb', line 82 def calculate_if_fat_model @fat_model_data = { method_count_high: @method_count.to_i > METHOD_THRESHOLD, callback_count_high: @callback_count.to_i > CALLBACK_THRESHOLD, association_count_high: @association_count.to_i > ASSOCIATION_THRESHOLD, validation_count_high: @validation_count.to_i > VALIDATION_THRESHOLD, line_count_high: @line_count.to_i > LINE_COUNT_THRESHOLD } # A model is considered fat if greater than or eq to 2 of the conditions are true OR the line count is huge @fat = @fat_model_data.values.count(true) >= 2 || @fat_model_data[:line_count_high] end |
#count_associations ⇒ Object
70 71 72 73 74 |
# File 'lib/fat_model_finder/file_data.rb', line 70 def count_associations file_content = File.read(@file) # Match Active Record associations like has_many, belongs_to, etc. @association_count = file_content.scan(/^\s*(has_many|has_one|belongs_to|has_and_belongs_to_many)/).size end |
#count_callbacks ⇒ Object
64 65 66 67 68 |
# File 'lib/fat_model_finder/file_data.rb', line 64 def count_callbacks file_content = File.read(@file) # Match Active Record callbacks like before_save, after_create, etc. @callback_count = file_content.scan(/^\s*(before_|after_)\w+/).size end |
#count_methods ⇒ Object
59 60 61 62 |
# File 'lib/fat_model_finder/file_data.rb', line 59 def count_methods file_content = File.read(@file) @method_count = file_content.scan(/^\s*def\s+/).size end |
#count_validations ⇒ Object
76 77 78 79 80 |
# File 'lib/fat_model_finder/file_data.rb', line 76 def count_validations file_content = File.read(@file) # Match both `validates` and `validate` for validations @validation_count = file_content.scan(/^\s*(validates|validate)\b/).size end |
#set_base_attributes ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fat_model_finder/file_data.rb', line 28 def set_base_attributes @line_count = File.foreach(@file).count @file_name = File.basename(@file) @file_size = File.size(@file) @file_extension = File.extname(@file) @last_modified = File.mtime(@file) @word_count = File.read(@file).split.size @char_count = File.read(@file).length @is_empty = File.zero?(@file) end |
#to_h ⇒ Object
Convert the object to a hash for JSON serialization
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fat_model_finder/file_data.rb', line 40 def to_h { file_name: @file_name, file_size: @file_size, file_extension: @file_extension, last_modified: @last_modified, line_count: @line_count, word_count: @word_count, char_count: @char_count, is_empty: @is_empty, method_count: @method_count, callback_count: @callback_count, association_count: @association_count, validation_count: @validation_count, fat: @fat, fat_model_data: @fat_model_data } end |