pry-rails 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,24 +72,69 @@ module PryRails
72
72
  def process
73
73
  Rails.application.eager_load!
74
74
 
75
- models = ActiveRecord::Base.descendants.map do |mod|
76
- model_string = mod.to_s + "\n"
77
- if mod.table_exists?
78
- model_string << mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n")
79
- else
80
- model_string << " Table doesn't exist"
81
- end
82
- mod.reflections.each do |model,ref|
83
- model_string << "\n #{ref.macro.to_s} #{model}"
84
- model_string << " through #{ref.options[:through]}" unless ref.options[:through].nil?
85
- end
86
- model_string
87
- end.join("\n")
75
+ if defined?(ActiveRecord::Base)
76
+ models = ActiveRecord::Base.descendants.map do |mod|
77
+ model_string = mod.to_s + "\n"
78
+ if mod.table_exists?
79
+ model_string << mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n")
80
+ else
81
+ model_string << " Table doesn't exist"
82
+ end
83
+ mod.reflections.each do |model,ref|
84
+ model_string << "\n #{ref.macro.to_s} #{model}"
85
+ model_string << " through #{ref.options[:through]}" unless ref.options[:through].nil?
86
+ end
87
+ model_string
88
+ end.join("\n")
89
+ elsif defined?(Mongoid::Document)
90
+ models = get_files.map do |path|
91
+ mod = extract_class_name(path)
92
+ model_string = "\033[1;34m#{mod.to_s}\033[0m\n"
93
+ begin
94
+ if mod.constantize.included_modules.include?(Mongoid::Document)
95
+ model_string << mod.constantize.fields.values.sort_by(&:name).map { |col|
96
+ " #{col.name}: \033[1;33m#{col.options[:type].to_s.downcase}\033[0m"
97
+ }.join("\n")
98
+ mod.constantize.relations.each do |model,ref|
99
+ model_string << "\n #{kind_of_relation(ref.relation.to_s)} \033[1;34m#{model}\033[0m"
100
+ model_string << ", autosave" if ref.options[:autosave]
101
+ model_string << ", autobuild" if ref.options[:autobuild]
102
+ model_string << ", validate" if ref.options[:validate]
103
+ model_string << ", dependent-#{ref.options[:dependent]}" if ref.options[:dependent]
104
+ end
105
+ else
106
+ model_string << " Collection doesn't exist"
107
+ end
108
+ model_string
109
+
110
+ rescue Exception
111
+ STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}"
112
+ end
113
+ end.join("\n")
114
+ end
88
115
 
89
116
  models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } unless opts[:G].nil?
90
-
91
117
  output.puts models
92
118
  end
119
+
120
+ def get_files(prefix ='')
121
+ Dir.glob(prefix << "app/models/**/*.rb")
122
+ end
123
+
124
+ def extract_class_name(filename)
125
+ filename.split('/')[2..-1].collect { |i| i.camelize }.join('::').chomp(".rb")
126
+ end
127
+
128
+ def kind_of_relation(string)
129
+ case string.gsub('Mongoid::Relations::', '')
130
+ when 'Referenced::Many' then 'has_many'
131
+ when 'Referenced::One' then 'has_one'
132
+ when 'Referenced::In' then 'belongs_to'
133
+ when 'Embedded::Many' then 'embeds_many'
134
+ when 'Embedded::One' then 'embeds_one'
135
+ when 'Embedded::In' then 'embedded_in'
136
+ end
137
+ end
93
138
  end
94
139
 
95
140
  create_command "show-middleware" do
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module PryRails
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-10 00:00:00.000000000 Z
12
+ date: 2012-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry