Class: Model
- Inherits:
-
Object
- Object
- Model
- Defined in:
- lib/rails_model_visualizer/model.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#super_class ⇒ Object
readonly
Returns the value of attribute super_class.
Instance Method Summary collapse
- #associations ⇒ Object
- #get_methods(type) ⇒ Object
- #indent(string) ⇒ Object
-
#initialize(rails_model) ⇒ Model
constructor
A new instance of Model.
- #private_methods ⇒ Object
- #public_methods ⇒ Object
- #source(model, method) ⇒ Object
- #source_code(source_file, method) ⇒ Object
- #to_div ⇒ Object
Constructor Details
#initialize(rails_model) ⇒ Model
Returns a new instance of Model.
4 5 6 7 8 9 10 |
# File 'lib/rails_model_visualizer/model.rb', line 4 def initialize(rails_model) @name = rails_model.name @columns = rails_model.columns @rails_model = rails_model @super_class = @rails_model.superclass @children = @rails_model.descendants end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
2 3 4 |
# File 'lib/rails_model_visualizer/model.rb', line 2 def columns @columns end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/rails_model_visualizer/model.rb', line 2 def name @name end |
#super_class ⇒ Object (readonly)
Returns the value of attribute super_class.
2 3 4 |
# File 'lib/rails_model_visualizer/model.rb', line 2 def super_class @super_class end |
Instance Method Details
#associations ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rails_model_visualizer/model.rb', line 114 def associations assoc = {} @rails_model.reflections.each do |name, association| if association.class.name.include?("HasManyReflection") type = "has_many" elsif association.class.name.include?("HasOneReflection") type = "has_one" elsif association.class.name.include?("BelongsToReflection") type = "belongs_to" else type = "other" end = [] association..each do |key, value| .push("#{key}: #{value}") end assoc[association.name.to_s] = {"type" => type, "options" => .join("\n") } end assoc end |
#get_methods(type) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rails_model_visualizer/model.rb', line 92 def get_methods(type) filename = "#{@name.underscore}.rb" result = {}; @rails_model.new.method(type).call(false).each do |pm| source_file = source(@rails_model, pm) if source_file[0].include?(filename) result[pm] = source_code(source_file, pm) end end result end |
#indent(string) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/rails_model_visualizer/model.rb', line 66 def indent(string) indent = ">" string.each_char do |char| break unless char == " " indent += ">" end indent = indent[2..-1] if indent.length > 1 indent + string end |
#private_methods ⇒ Object
110 111 112 |
# File 'lib/rails_model_visualizer/model.rb', line 110 def private_methods get_methods(:private_methods) end |
#public_methods ⇒ Object
106 107 108 |
# File 'lib/rails_model_visualizer/model.rb', line 106 def public_methods get_methods(:public_methods) end |
#source(model, method) ⇒ Object
61 62 63 64 |
# File 'lib/rails_model_visualizer/model.rb', line 61 def source(model, method) m = method.to_sym @rails_model.new.method(m).source_location end |
#source_code(source_file, method) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rails_model_visualizer/model.rb', line 76 def source_code(source_file, method) path, start_line = source_file source_code = [] line_to_read = start_line - 1 file = File.readlines(path) while true source_code.push(indent(file[line_to_read])) break if file[line_to_read] == " end\n" line_to_read += 1 end source_code.join("<br>") end |
#to_div ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rails_model_visualizer/model.rb', line 12 def to_div columns = [] @columns.each do |c| columns.push( "<div class=\"list-name\">#{c.name} <div class=\"list-source\">#{c.sql_type}</div> </div>") end methods = [] public_methods.each do |name, source_code| methods.push( "<div class=\"list-name\">#{name} <div class=\"list-source\">#{source_code}</div> </div>") end private_methods.each do |name, source_code| methods.push( "<div class=\"list-name\">#{name} <div class=\"list-source\">#{source_code}</div> </div>") end assoc_list = [] associations.each do |assoc_name, value| assoc_list.push( "<div class=\"list-name\">#{value["type"]}: #{assoc_name} <div class=\"list-source\">#{value["options"]}</div> </div>") end return ( "<div class=\"model\"> <h1>#{@name}</h1> <div class=\"model-details\"> <ul>SQL Columns #{columns.join("")} </ul> <ul>Methods #{methods.join("")} </ul> <ul>Associations #{assoc_list.join("")} </ul> </div> </div>" ) end |