Class: Databasium::Model
- Inherits:
-
Object
- Object
- Databasium::Model
- Defined in:
- app/services/databasium/model.rb
Defined Under Namespace
Classes: ModelData
Constant Summary collapse
- PATHS =
[ "models" ].freeze
- RELATIONS =
%w[belongs_to has_many has_one has_and_belongs_to_many].freeze
- RELATIONS_REGEX =
/\A(#{Regexp.union(RELATIONS).source})/
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#relations ⇒ Object
readonly
Returns the value of attribute relations.
Instance Method Summary collapse
- #create_model_data(model_name:, attributes:, relations:, unknown:) ⇒ Object
-
#get_all_models_from_db(search: nil) ⇒ Object
might be worth switching to reading from the dir directly in future def get_all_models_from_dir(search: nil) model_files = [] PATHS.each { |path| model_files += Dir.glob(Rails.root.join(“app”, path, “*/.rb”)) } model_names = model_files .map { |file| File.basename(file).sub(/.rb$/, “”).classify } .reject do |name| %w[ApplicationRecord Concerns].include?(name) || !name.safe_constantize&.table_exists? end model_names = model_names.select { |name| name =~ /#search/i } if search model_names end.
- #get_model_data_from_file(model_name) ⇒ Object
-
#initialize ⇒ Model
constructor
A new instance of Model.
- #read_model_file(model_name) ⇒ Object
Constructor Details
#initialize ⇒ Model
Returns a new instance of Model.
7 8 |
# File 'app/services/databasium/model.rb', line 7 def initialize end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
2 3 4 |
# File 'app/services/databasium/model.rb', line 2 def attributes @attributes end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
2 3 4 |
# File 'app/services/databasium/model.rb', line 2 def model_name @model_name end |
#relations ⇒ Object (readonly)
Returns the value of attribute relations.
2 3 4 |
# File 'app/services/databasium/model.rb', line 2 def relations @relations end |
Instance Method Details
#create_model_data(model_name:, attributes:, relations:, unknown:) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'app/services/databasium/model.rb', line 84 def create_model_data(model_name:, attributes:, relations:, unknown:) ModelData.new( model_name: model_name, attributes: attributes, relations: relations, unknown: unknown ) end |
#get_all_models_from_db(search: nil) ⇒ Object
might be worth switching to reading from the dir directly in future def get_all_models_from_dir(search: nil)
model_files = []
PATHS.each { |path| model_files += Dir.glob(Rails.root.join("app", path, "**/*.rb")) }
model_names =
model_files
.map { |file| File.basename(file).sub(/\.rb$/, "").classify }
.reject do |name|
%w[ApplicationRecord Concerns].include?(name) || !name.safe_constantize&.table_exists?
end
model_names = model_names.select { |name| name =~ /#{search}/i } if search
model_names
end
24 25 26 27 28 29 30 |
# File 'app/services/databasium/model.rb', line 24 def get_all_models_from_db(search: nil) conn = ActiveRecord::Base.connection tables = conn.tables - %w[ar_internal_metadata schema_migrations] tables = tables.map { |t| t.classify } tables = tables.select { |t| t =~ /#{search}/i } if search tables end |
#get_model_data_from_file(model_name) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/services/databasium/model.rb', line 36 def get_model_data_from_file(model_name) raw_model = constantize_model(model_name) model = { validations: [], columns: [], unknown: [], relations: [], columns_hash: {} } raw_model.columns.each do |column| model[:columns_hash][column.name] = { type: column.type, validations: [] } end index = 0 inside_class = false File.foreach(model_file_path(model_name)) do |line| raw_line = line line = line.strip.lstrip parsed_line = {} if line.start_with?("#") parsed_line = parse_column(line) elsif line.start_with?("validates :") parsed_line = parse_validation(line) scan_name = parsed_line[:content][:name] model_column = model[:columns_hash].fetch(scan_name, nil) if model_column.present? model_column[:validations] << { type: parsed_line[:content][:type], value: parsed_line[:content][:value] } end elsif line.match?(RELATIONS_REGEX) parsed_line = parse_relation(line) else if !raw_line.include?("class") parsed_line = { type: :unknown, content: {} } else inside_class = true end end if parsed_line.present? && !(raw_line.blank? && !inside_class) parsed_line[:content].merge!( { index: index, line: parsed_line[:type] == :unknown ? raw_line : line } ) model[parsed_line[:type]] << parsed_line[:content] end index += 1 end model end |
#read_model_file(model_name) ⇒ Object
32 33 34 |
# File 'app/services/databasium/model.rb', line 32 def read_model_file(model_name) File.read(model_file_path(model_name)) end |