effective_developer 0.6.2 → 0.6.3

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
  SHA256:
3
- metadata.gz: ccd87397ad97b0591c73b5c42b80db04a906d5f502340f8c988ade6169281da5
4
- data.tar.gz: 8d4538a30eb27098eb5e163749b845083248f9cee9612bbb214a6fa65598e2c0
3
+ metadata.gz: 7e76b8818691ec2238e08a818d209ba7e4d1746734ef3c7c2c2cbe8f4e08aa12
4
+ data.tar.gz: a283fca85ffd1c0dc6d73287bd6c239c898e6b6e2aa8500ebc08642960f7ad5f
5
5
  SHA512:
6
- metadata.gz: 38f2f1875b8b33d856b93fe5ee605c32ddc3b53bc0a93a6edbadabf46ce418db97e2aedbd876cb106d23d9a7fa8c6355cb43a021e86139414cf9f4c3addb4b73
7
- data.tar.gz: bcdfd6185054b04342e8f5621bca8c5791d309f9d5a5726ffd5c6d52521008c57396fb4b3ff3d99612ced3f55218a26d555837258de838e3e313fa4de7d41ae4
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
 
@@ -32,7 +32,5 @@ module Effective
32
32
  retval
33
33
  end
34
34
 
35
-
36
35
  end
37
-
38
36
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveDeveloper
2
- VERSION = '0.6.2'.freeze
2
+ VERSION = '0.6.3'.freeze
3
3
  end
@@ -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.2
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-04-27 00:00:00.000000000 Z
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