blueprint-generators-rails 0.0.8 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 575a658dff129cd604d23f2c5a75c9a8c0e1fdb8
4
- data.tar.gz: 7dc6ad06758386e8081812f32af30a3f9cbb4c62
3
+ metadata.gz: ed74bf88f68d1cf0892cd924c01c3867d5565520
4
+ data.tar.gz: a23b7c4d5e80b834dfc9105a48a61938dea4b23b
5
5
  SHA512:
6
- metadata.gz: 72f5f62af8171a98708756a84e4216b787201fdcfdc1592b6b7d891f1441eab4d1a678b956871fc3c30c70e08c7de8fe0a997f6b25933cb450300d3fe08a67df
7
- data.tar.gz: 88a256a12ab19301597490c657d8996d8e17fdcd28175a344f759ef24f0679ee05fcfffced7c55a9f393f0f4ea5799900e93c9dc74777acd0a7342ec3265c70d
6
+ metadata.gz: a78b4e02fe8544c9a7ed2e2e2d1a934255d047e6fde1d84eeafa5c29979189c07a9fbae7a1883c32455c607e3ad504430bc93c5953e6000028593d3c722e32c9
7
+ data.tar.gz: 5291415e66479f219ab46171baf0d3cedab1c3bc528b95f48bdc7629caf9c1c54b2ba29d26300f89ad67ca5d97d8ce9b4df93c265e4f03460721003dfca85e1b
@@ -1,7 +1,7 @@
1
1
  module Blueprint
2
2
  module Generators
3
3
  module Rails
4
- VERSION = "0.0.8"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,39 +1,46 @@
1
1
  namespace :blueprint do
2
2
 
3
+ @debug = false
4
+
3
5
  desc 'Generate a Conceptual Model diagram for the current Rails project'
4
6
  task :cm, [:options] => :environment do |t, args|
5
7
  Rails.application.eager_load!
6
8
 
9
+ # set the debug flag
10
+ @debug = args[:options] == 'debug'
11
+
12
+ # we store the detected model in a hash - which we later serialize to PogoScript
13
+ model = { }
14
+
7
15
  # for debugging purposes
8
16
  step_count = 1
9
17
 
18
+ # get the configured app name
10
19
  app_name = Rails.application.class.parent_name
11
- pogo = "conceptual model for \"" + app_name + "\""
12
20
 
13
- if args[:options] == 'debug'
14
- puts "#{step_count}. Generating conceptual model PogoScript for " + app_name
15
- step_count += 1
16
- end
21
+ print_debug step_count, "Generating conceptual model PogoScript for " + app_name
22
+ step_count += 1
17
23
 
18
- models = ActiveRecord::Base.descendants
19
- models.each { |m|
24
+ concepts = ActiveRecord::Base.descendants
25
+ concepts.each { |m|
26
+ next if m.name.starts_with?'HABTM' # we skip Rails 'special' HABTM model classes
20
27
 
21
28
  concept_name = humanise(m.name)
22
- pogo << "\n concept \"" + concept_name + "\"\n"
23
29
 
24
- if args[:options] == 'debug'
25
- puts "#{step_count}. Adding concept " + concept_name
26
- step_count += 1
27
- end
30
+ # add the concept to the model hash
31
+ model[concept_name] = [ ]
32
+
33
+ print_debug step_count, "Adding concept " + concept_name
34
+ step_count += 1
28
35
 
29
36
  unless m.superclass.to_s == 'ActiveRecord::Base'
30
37
  is_a_name = humanise(m.superclass.to_s.singularize.capitalize)
31
- pogo << " is a \"" + is_a_name + "\"\n"
32
38
 
33
- if args[:options] == 'debug'
34
- puts "#{step_count}. Concept " + concept_name + " is a " + is_a_name
35
- step_count += 1
36
- end
39
+ # add the node relationship to the concept
40
+ model[concept_name].push({ :type => 'is a', :name => is_a_name })
41
+
42
+ print_debug step_count, "Concept " + concept_name + " is a " + is_a_name
43
+ step_count += 1
37
44
  end
38
45
 
39
46
  associations = m.reflect_on_all_associations
@@ -45,36 +52,72 @@ namespace :blueprint do
45
52
  case a.macro
46
53
  when :belongs_to, :has_one
47
54
  has_one_name = humanise(a.name.to_s.singularize.capitalize)
48
- pogo << " has one \"" + has_one_name + "\"\n"
49
55
 
50
- if args[:options] == 'debug'
51
- puts "#{step_count}. Concept " + concept_name + " has one " + has_one_name
52
- step_count += 1
53
- end
56
+ # add the node relationship to the concept
57
+ model[concept_name].push({ :type => 'has one', :name => has_one_name })
58
+
59
+ print_debug step_count, "Concept " + concept_name + " has one " + has_one_name
60
+ step_count += 1
54
61
 
55
62
  when :has_many
56
63
  has_many_name = humanise(a.name.to_s.singularize.capitalize)
57
- pogo << " has many \"" + has_many_name + "\"\n"
58
64
 
59
- if args[:options] == 'debug'
60
- puts "#{step_count}. Concept " + concept_name + " has one " + has_many_name
61
- step_count += 1
65
+ # add the node relationship to the concept
66
+ model[concept_name].push({ :type => 'has many', :name => has_many_name })
67
+
68
+ print_debug step_count, "Concept " + concept_name + " has one " + has_many_name
69
+ step_count += 1
70
+
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)
74
+
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 })
62
78
  end
63
79
 
64
- else
65
- # TODO support other macro types and variants (i.e.: has_and_belongs_to_many, through, etc)
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
66
84
 
67
- if args[:options] == 'debug'
68
- puts "#{step_count}. Did not recognise macro type!"
69
- step_count += 1
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 })
70
88
  end
71
89
 
72
- # TODO error condition
90
+ print_debug step_count, "Concept " + concept_name + " has many-to-many with " + has_many_name
91
+ step_count += 1
92
+
93
+ else
94
+ print_debug step_count, "Did not recognise macro type: " + a.macro.to_s
95
+ step_count += 1
96
+
97
+ end
98
+ }
99
+ }
73
100
 
101
+ # now generate the PogoScript
102
+ pogo = "conceptual model for \"" + app_name + "\""
103
+ model.each { |name, relationships|
104
+ pogo << "\n concept \"" + name + "\"\n"
105
+
106
+ relationships.each { |r|
107
+ case r[:type]
108
+ when 'is a'
109
+ pogo << " is a \"" + r[:name] + "\"\n"
110
+ when 'has one'
111
+ pogo << " has one \"" + r[:name] + "\"\n"
112
+ when 'has many'
113
+ pogo << " has many \"" + r[:name] + "\"\n"
114
+ else
115
+ # TODO implement
74
116
  end
75
117
  }
76
118
  }
77
119
 
120
+ # output the result
78
121
  puts ''
79
122
  puts 'Navigate to the link below and paste the provided script into the editor'
80
123
  puts ''
@@ -110,4 +153,10 @@ namespace :blueprint do
110
153
  str
111
154
  end
112
155
 
156
+ def self.print_debug(step_count, str)
157
+ if @debug
158
+ puts "#{step_count}. " + str
159
+ end
160
+ end
161
+
113
162
  end
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.0.8
4
+ version: 0.1.0
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-05 00:00:00.000000000 Z
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler