effective_developer 0.6.2 → 0.6.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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e76b8818691ec2238e08a818d209ba7e4d1746734ef3c7c2c2cbe8f4e08aa12
|
4
|
+
data.tar.gz: a283fca85ffd1c0dc6d73287bd6c239c898e6b6e2aa8500ebc08642960f7ad5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dc45f6f25c607353f5ea9651cda2054e2d6f2996fac603ebd37df05191d4a2d7981662e7e68681163856a566eeb452c4b0030623d130f2d33507bea36956548
|
7
|
+
data.tar.gz: 8a4ec7eab0237e8f0ac37a80e1cb7ea3974514b8228a6f8711a9fa00b66e0d2cecb5b22d336c28afd954bb916337b809a1205b8820d3637fb66574829710b73d
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# Writes the effective_resource do .. end block into the model file
|
2
|
+
module Effective
|
3
|
+
class Annotator
|
4
|
+
|
5
|
+
def initialize(resource: 'All', folders: 'app/models/')
|
6
|
+
@resources = Array(resource).map { |resource| resource.to_s.classify }
|
7
|
+
@folders = Array(folders)
|
8
|
+
end
|
9
|
+
|
10
|
+
def annotate!
|
11
|
+
@folders.each do |folder|
|
12
|
+
Dir.glob(folder + '**/*').each do |path|
|
13
|
+
next if File.directory?(path)
|
14
|
+
|
15
|
+
name = path.sub(folder, '').split('.')[0...-1].join('.')
|
16
|
+
resource = Effective::Resource.new(name)
|
17
|
+
klass = resource.klass
|
18
|
+
|
19
|
+
next if klass.blank?
|
20
|
+
next unless klass.ancestors.include?(ActiveRecord::Base)
|
21
|
+
next if klass.abstract_class?
|
22
|
+
next unless @resources.include?('All') || @resources.include?(klass.name)
|
23
|
+
|
24
|
+
annotate(resource, path)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
puts 'All Done. Have a great day.'
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def annotate(resource, path)
|
35
|
+
puts "Annotate: #{path}"
|
36
|
+
|
37
|
+
Effective::CodeWriter.new(path) do |writer|
|
38
|
+
index = find_insert_at(writer)
|
39
|
+
content = build_content(resource)
|
40
|
+
|
41
|
+
remove_existing(writer)
|
42
|
+
writer.insert(content, index)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def find_insert_at(writer)
|
47
|
+
index = writer.first { |line| line.include?('effective_resource do') || line.include?('structure do') }
|
48
|
+
|
49
|
+
index ||= begin
|
50
|
+
index = writer.first { |line| line.include?('validates :') || line.include?('scope :') || line.include?('def ') }
|
51
|
+
index - 1 if index
|
52
|
+
end
|
53
|
+
|
54
|
+
[1, index.to_i-1].max
|
55
|
+
end
|
56
|
+
|
57
|
+
def remove_existing(writer)
|
58
|
+
from = writer.first { |line| line.include?('effective_resource do') || line.include?('structure do') }
|
59
|
+
return unless from.present?
|
60
|
+
|
61
|
+
to = writer.first(from: from) { |line| line == 'end' || line == '# end' }
|
62
|
+
return unless to.present?
|
63
|
+
|
64
|
+
writer.remove(from: from, to: to+1)
|
65
|
+
end
|
66
|
+
|
67
|
+
def build_content(resource)
|
68
|
+
attributes = resource.klass_attributes(all: true)
|
69
|
+
atts = attributes.except(resource.klass.primary_key.to_sym, :created_at, :updated_at)
|
70
|
+
|
71
|
+
max = atts.map { |k, v| k.to_s.length }.max.to_i + 4
|
72
|
+
max = max + 1 unless (max % 2 == 0)
|
73
|
+
|
74
|
+
lines = atts.map { |k, v| k.to_s + (' ' * (max - k.to_s.length)) + ":#{v.first}" }
|
75
|
+
lines += ['', 'timestamps'] if attributes.key?(:created_at) && attributes.key?(:updated_at)
|
76
|
+
|
77
|
+
['effective_resource do'] + lines + ['end']
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -228,13 +228,17 @@ module Effective
|
|
228
228
|
lines.each { |line| @changed = true if line.gsub!(source, target) }
|
229
229
|
end
|
230
230
|
|
231
|
+
def remove(from:, to:)
|
232
|
+
raise('expected from to be less than to') unless from.present? && to.present? && (from < to)
|
233
|
+
@changed = true
|
234
|
+
(to - from).times { lines.delete_at(from) }
|
235
|
+
end
|
236
|
+
|
231
237
|
def replace(index, content)
|
232
238
|
@changed = true
|
233
239
|
lines[index].replace(content.to_s)
|
234
240
|
end
|
235
241
|
|
236
|
-
private
|
237
|
-
|
238
242
|
def write!
|
239
243
|
return false unless changed?
|
240
244
|
|
@@ -245,6 +249,8 @@ module Effective
|
|
245
249
|
true
|
246
250
|
end
|
247
251
|
|
252
|
+
private
|
253
|
+
|
248
254
|
def open?(content)
|
249
255
|
stripped = ss(content)
|
250
256
|
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# bundle exec rake annotate
|
2
|
+
# bundle exec rake annotate[user]
|
3
|
+
|
4
|
+
desc 'Adds an effective_resources do .. end block to all ActiveRecord model files'
|
5
|
+
task :annotate, [:resource] => :environment do |t, args|
|
6
|
+
args.with_defaults(resource: 'all')
|
7
|
+
Effective::Annotator.new(resource: args.resource).annotate!
|
8
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_developer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- MIT-LICENSE
|
49
49
|
- README.md
|
50
50
|
- Rakefile
|
51
|
+
- app/models/effective/annotator.rb
|
51
52
|
- app/models/effective/code_writer.rb
|
52
53
|
- app/models/effective/csv_importer.rb
|
53
54
|
- app/models/effective/live_generator.rb
|
@@ -95,6 +96,7 @@ files:
|
|
95
96
|
- lib/scaffolds/importers/csv_importer.rb
|
96
97
|
- lib/scaffolds/models/model.rb
|
97
98
|
- lib/scaffolds/views/_resource.html.haml
|
99
|
+
- lib/tasks/annotate.rake
|
98
100
|
- lib/tasks/effective_csv_importer.rake
|
99
101
|
- lib/tasks/pg_pull.rake
|
100
102
|
- lib/tasks/rename_class.rake
|