Class: RailsLens::Extensions::Base
- Inherits:
-
Object
- Object
- RailsLens::Extensions::Base
- Defined in:
- lib/rails_lens/extensions/base.rb
Direct Known Subclasses
Constant Summary collapse
- INTERFACE_VERSION =
'1.0'
Instance Attribute Summary collapse
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Class Method Summary collapse
- .compatible? ⇒ Boolean
- .detect? ⇒ Boolean
-
.gem_available?(gem_name) ⇒ Boolean
Helper method for gem-based detection.
- .gem_name ⇒ Object
- .interface_version ⇒ Object
Instance Method Summary collapse
-
#annotate ⇒ Object
Override this method to provide model-specific annotations.
-
#erd_additions ⇒ Object
Override this method to provide ERD additions.
-
#initialize(model_class) ⇒ Base
constructor
A new instance of Base.
-
#notes ⇒ Object
Override this method to provide analysis notes.
-
#safe_method_call(method_name, default_value = nil) ⇒ Object
Validation helper methods.
- #validate_array_result(result, method_name = 'unknown') ⇒ Object
- #validate_hash_result(result, required_keys = [], method_name = 'unknown') ⇒ Object
Constructor Details
#initialize(model_class) ⇒ Base
Returns a new instance of Base.
39 40 41 |
# File 'lib/rails_lens/extensions/base.rb', line 39 def initialize(model_class) @model_class = model_class end |
Instance Attribute Details
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
37 38 39 |
# File 'lib/rails_lens/extensions/base.rb', line 37 def model_class @model_class end |
Class Method Details
.compatible? ⇒ Boolean
23 24 25 26 |
# File 'lib/rails_lens/extensions/base.rb', line 23 def compatible? required_version = RailsLens.config.extensions[:interface_version] Gem::Version.new(interface_version) >= Gem::Version.new(required_version) end |
.detect? ⇒ Boolean
13 14 15 |
# File 'lib/rails_lens/extensions/base.rb', line 13 def detect? raise NotImplementedError, "#{self.class.name} must implement .detect?" end |
.gem_available?(gem_name) ⇒ Boolean
Helper method for gem-based detection
29 30 31 32 33 34 |
# File 'lib/rails_lens/extensions/base.rb', line 29 def gem_available?(gem_name) Gem::Specification.find_by_name(gem_name) true rescue Gem::LoadError false end |
.gem_name ⇒ Object
9 10 11 |
# File 'lib/rails_lens/extensions/base.rb', line 9 def gem_name raise NotImplementedError, "#{self.class.name} must implement .gem_name" end |
.interface_version ⇒ Object
17 18 19 20 21 |
# File 'lib/rails_lens/extensions/base.rb', line 17 def interface_version self::INTERFACE_VERSION rescue NameError INTERFACE_VERSION end |
Instance Method Details
#annotate ⇒ Object
Override this method to provide model-specific annotations
44 45 46 |
# File 'lib/rails_lens/extensions/base.rb', line 44 def annotate nil end |
#erd_additions ⇒ Object
Override this method to provide ERD additions
54 55 56 57 58 59 60 |
# File 'lib/rails_lens/extensions/base.rb', line 54 def erd_additions { relationships: [], badges: [], attributes: {} } end |
#notes ⇒ Object
Override this method to provide analysis notes
49 50 51 |
# File 'lib/rails_lens/extensions/base.rb', line 49 def notes [] end |
#safe_method_call(method_name, default_value = nil) ⇒ Object
Validation helper methods
63 64 65 66 67 68 69 70 |
# File 'lib/rails_lens/extensions/base.rb', line 63 def safe_method_call(method_name, default_value = nil) return default_value unless respond_to?(method_name) send(method_name) rescue StandardError => e log_method_error(method_name, e) default_value end |
#validate_array_result(result, method_name = 'unknown') ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/rails_lens/extensions/base.rb', line 72 def validate_array_result(result, method_name = 'unknown') return [] unless result.is_a?(Array) result.grep(String) rescue StandardError => e log_method_error(method_name, e) [] end |
#validate_hash_result(result, required_keys = [], method_name = 'unknown') ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rails_lens/extensions/base.rb', line 81 def validate_hash_result(result, required_keys = [], method_name = 'unknown') unless result.is_a?(Hash) log_method_error(method_name, "Expected Hash, got #{result.class}") return required_keys.index_with { |_key| [] } end # Ensure required keys exist with default values required_keys.each do |key| result[key] ||= key == :attributes ? {} : [] end result rescue StandardError => e log_method_error(method_name, e) required_keys.index_with { |key| key == :attributes ? {} : [] } end |