blueprint-generators-rails 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4ec72422d405b2f409c8523041fd0891e2bf3db
4
- data.tar.gz: 79d1471fd4ce53751988e4fcf89afa3069cf8524
3
+ metadata.gz: d800389259cd4f2c7deb3ccee4d9b8fad2d547dd
4
+ data.tar.gz: 91e0960b4fd8af98abf10ddf4294f1072a6699b2
5
5
  SHA512:
6
- metadata.gz: b7ff8758f7e0a01e4a359be7ceb45b1c5ef390971d6f230ea7b53b3b6edb098e980ae5cf3f817550dd769cd3202b5037b368cbd4d0070c07ed6858bf5d4e46a3
7
- data.tar.gz: e8220c8901c91c5e17d020458fc8f9447596b6b393a0e9d472889329e0fc0a4b2059a34322e7424f64af0daafe707980edef025e730a0fc023733970ad6f101d
6
+ metadata.gz: c210d07d0afb8d16cf2b0c8508232cacc228504edbf0565b260eeceb9f7e7fffe2d107924753718d56d48d70fa3af6a19586be80db21fada2cfc68c6d65603e9
7
+ data.tar.gz: 70d7c70e613cd3a7dfdd175fdd87a4df79908b74518b22f0c0935af0f1821c5cc4e8eabce39ba01ad4b6556fde60ecd2cd10b25618e6b749fde575e681b1889e
@@ -1,7 +1,7 @@
1
1
  module Blueprint
2
2
  module Generators
3
3
  module Rails
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
6
6
  end
7
7
  end
@@ -4,7 +4,7 @@ namespace :blueprint do
4
4
 
5
5
  desc 'Generate a Conceptual Model diagram for the current Rails project'
6
6
  task :cm, [:options] => :environment do |t, args|
7
- Rails.application.eager_load!
7
+ # Rails.application.eager_load!
8
8
 
9
9
  # set the debug flag
10
10
  @debug = args[:options] == 'debug'
@@ -16,87 +16,112 @@ namespace :blueprint do
16
16
  step_count = 1
17
17
 
18
18
  # get the configured app name
19
- app_name = Rails.application.class.parent_name
19
+ app_name = Dir.pwd.split('/').last.capitalize
20
+ # app_name = Rails.application.class.parent_name
20
21
 
21
- print_debug step_count, "Generating conceptual model PogoScript for " + app_name
22
+ print_debug step_count, "Application name is " + app_name
22
23
  step_count += 1
23
24
 
24
- concepts = ActiveRecord::Base.descendants
25
- concepts.each { |m|
26
- next if m.name.starts_with?'HABTM' # we skip Rails 'special' HABTM model classes
25
+ unless Dir.exist?(Dir.pwd + '/app/models')
26
+ print_debug step_count, 'Could not find models directory. Stopping analysis.'
27
+ return 0
28
+ end
27
29
 
28
- concept_name = humanise(m.name)
30
+ Dir.chdir(Dir.pwd + '/app/models') do
29
31
 
30
- # add the concept to the model hash
31
- model[concept_name] = [ ]
32
+ # list all files in the directory
33
+ Dir.foreach('.') { |f|
32
34
 
33
- print_debug step_count, "Adding concept " + concept_name
34
- step_count += 1
35
+ # only deal with files that have a '.rb' extension
36
+ if File.extname(f) == '.rb'
37
+ # puts "Found: #{f}"
35
38
 
36
- unless m.superclass.to_s == 'ActiveRecord::Base'
37
- is_a_name = humanise(m.superclass.to_s.singularize.capitalize)
39
+ # process each file
40
+ File.open(f) do |m|
41
+ concept_name = nil
38
42
 
39
- # add the node relationship to the concept
40
- model[concept_name].push({ :type => 'is a', :name => is_a_name })
43
+ # process each line of the file
44
+ m.each_line do |line|
41
45
 
42
- print_debug step_count, "Concept " + concept_name + " is a " + is_a_name
43
- step_count += 1
44
- end
46
+ # search for the class declaration line
47
+ clazz, super_clazz = line.match(/class ([^<]*) < (.*)/).try(:captures)
45
48
 
46
- associations = m.reflect_on_all_associations
47
- associations.each { |a|
49
+ # if we find a class declaration line, add the new concept to the model
50
+ unless clazz.nil?
51
+ # puts "Parsed: #{clazz} : #{super_clazz}"
52
+ concept_name = clazz.pluralize
48
53
 
49
- # TODO this is an OK solution but has some shortcomings - we need to figure out how to get the actual
50
- # TODO associated model name (not the macro name which we then singularize and capitalize)
54
+ # add the concept to the model hash
55
+ model[concept_name] = [ ]
51
56
 
52
- case a.macro
53
- when :belongs_to, :has_one
54
- has_one_name = humanise(a.name.to_s.singularize.capitalize)
57
+ print_debug step_count, "Adding concept " + concept_name
58
+ step_count += 1
55
59
 
56
- # add the node relationship to the concept
57
- model[concept_name].push({ :type => 'has one', :name => has_one_name })
60
+ unless super_clazz == 'ActiveRecord::Base'
61
+ is_a_name = super_clazz.singularize
58
62
 
59
- print_debug step_count, "Concept " + concept_name + " has one " + has_one_name
60
- step_count += 1
63
+ # add the node relationship to the concept
64
+ model[concept_name].push({ :type => 'is a', :name => is_a_name })
61
65
 
62
- when :has_many
63
- has_many_name = humanise(a.name.to_s.singularize.capitalize)
66
+ print_debug step_count, "Concept " + concept_name + " is a " + is_a_name
67
+ step_count += 1
68
+ end
69
+ end
64
70
 
65
- # add the node relationship to the concept
66
- model[concept_name].push({ :type => 'has many', :name => has_many_name })
71
+ # search for a 'has_one' or 'belongs_to' declaration
72
+ a, has_one_clazz = line.match(/(has_one|belongs_to) :([^,]+)/).try(:captures)
73
+ unless has_one_clazz.nil?
74
+ has_one_name = has_one_clazz.capitalize.singularize.strip
67
75
 
68
- print_debug step_count, "Concept " + concept_name + " has one " + has_many_name
69
- step_count += 1
76
+ # add the node relationship to the concept
77
+ model[concept_name].push({ :type => 'has one', :name => has_one_name })
70
78
 
71
- when :has_and_belongs_to_many
72
- # this is a many-to-many, so we add two 'has many' relationships (one of each side)
73
- has_many_name = humanise(a.name.to_s.singularize.capitalize)
79
+ print_debug step_count, "Concept " + concept_name + " has one " + has_one_name
80
+ step_count += 1
81
+ end
74
82
 
75
- # add the first side of the 'has many' if it does not already exist
76
- if model[concept_name].find { |v| v[:type] == 'has many' && v[:name] == has_many_name }.nil?
77
- model[concept_name].push({ :type => 'has many', :name => has_many_name })
78
- end
83
+ # search for a 'has_many' declaration
84
+ b, has_many_clazz = line.match(/(has_many) :([^,]+)/).try(:captures)
85
+ unless has_many_clazz.nil?
86
+ has_many_name = has_many_clazz.capitalize.pluralize.strip
79
87
 
80
- # if the model hash doesn't have any entry for the many side of the relationship, create it
81
- if model[has_many_name].nil?
82
- model[has_many_name] = [ ]
83
- end
88
+ # add the node relationship to the concept
89
+ model[concept_name].push({ :type => 'has many', :name => has_many_name })
84
90
 
85
- # add the second side of the 'has many' if it does not already exist
86
- if model[has_many_name].find { |v| v[:type] == 'has many' && v[:name] == concept_name }.nil?
87
- model[has_many_name].push({ :type => 'has many', :name => concept_name })
88
- end
91
+ print_debug step_count, "Concept " + concept_name + " has one " + has_many_name
92
+ step_count += 1
93
+ end
89
94
 
90
- print_debug step_count, "Concept " + concept_name + " has many-to-many with " + has_many_name
91
- step_count += 1
95
+ # search for a 'has_many' declaration
96
+ c, habtm_clazz = line.match(/(has_and_belongs_to_many) :([^,]+)/).try(:captures)
97
+ unless habtm_clazz.nil?
98
+ # this is a many-to-many, so we add two 'has many' relationships (one of each side)
99
+ habtm_name = habtm_clazz.capitalize.pluralize.strip
92
100
 
93
- else
94
- print_debug step_count, "Did not recognise macro type: " + a.macro.to_s
95
- step_count += 1
101
+ # add the first side of the 'has many' if it does not already exist
102
+ if model[concept_name].find { |v| v[:type] == 'has many' && v[:name] == habtm_name }.nil?
103
+ model[concept_name].push({ :type => 'has many', :name => habtm_name })
104
+ end
105
+
106
+ # if the model hash doesn't have any entry for the many side of the relationship, create it
107
+ if model[habtm_name].nil?
108
+ model[habtm_name] = [ ]
109
+ end
110
+
111
+ # add the second side of the 'has many' if it does not already exist
112
+ if model[habtm_name].find { |v| v[:type] == 'has many' && v[:name] == concept_name }.nil?
113
+ model[habtm_name].push({ :type => 'has many', :name => concept_name })
114
+ end
115
+
116
+ print_debug step_count, "Concept " + concept_name + " has many-to-many with " + habtm_name
117
+ step_count += 1
118
+ end
96
119
 
120
+ end
121
+ end
97
122
  end
98
123
  }
99
- }
124
+ end
100
125
 
101
126
  # now generate the PogoScript
102
127
  pogo = "conceptual model for \"" + app_name + "\""
@@ -140,19 +165,6 @@ namespace :blueprint do
140
165
 
141
166
  private
142
167
 
143
- def self.humanise(str)
144
- # this block applies a naming clean-up by camel-casing any words after an underscore (e.g.: Invited_by => InvitedBy)
145
-
146
- tokens = str.scan(/[_]+[\w]/)
147
- unless tokens.empty?
148
- tokens.each { |t|
149
- str[t]= t[-1, 1].capitalize
150
- }
151
- end
152
-
153
- str
154
- end
155
-
156
168
  def self.print_debug(step_count, str)
157
169
  if @debug
158
170
  puts "#{step_count}. " + str
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprint-generators-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjii
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-17 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler