arma_fixer 0.1.0 → 0.1.1

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: a09ab5b4665de2f3e31bb39cc5767ded2c79ce76
4
- data.tar.gz: 1926b5464ae286f739ac5f6d9920e5ffb74f9661
3
+ metadata.gz: 1e2bdf6561c52f18e15c6b76151cf34cefaecb85
4
+ data.tar.gz: 0dcb794c30751289f6734a39c40366788b875b4b
5
5
  SHA512:
6
- metadata.gz: 40afc47969507acef1ef8a5444f8d2408cd11584501981ba9f7b286d8b17e666466e1042f86317ede86a377576154c8bd1691252a8c8337670b9fb61a0fd84a4
7
- data.tar.gz: 9df60ad0aba67b5d7cb24fb2b7c52384f677ee9da12d4d146b2e60594bf54cc2a12dacdb882cff78190ec82d1edc691dbf14653e27427e5b645d3b5dc7f69035
6
+ metadata.gz: 05d6173c50949a96c3ef0fab29e098b729658670c761176aaea4600ad7a821b1f9440cb0d0b0c8e063dff408875976f7d4eec44e8218b92109d4cb77159875f2
7
+ data.tar.gz: 716861dbe6324fe61377eee929af7f89378d433c86ef6800b64bca3f98e875bf0b71a0466c25b27aab4e8132b9d794d60e7893cf3b435dc71d089f866493cb7f
@@ -4,18 +4,18 @@ require_relative './arma_fixer/execute'
4
4
  require_relative './arma_fixer/railtie' if defined?(Rails)
5
5
 
6
6
  module ArmaFixer
7
- mattr_accessor :root_path
7
+ self.class.delegate :show, to: Execute
8
8
 
9
9
  module_function
10
10
 
11
11
  def run!
12
12
  start_time = Time.now
13
- puts 'Started ArmaFixer task...'
13
+ show 'Started ArmaFixer task...'
14
14
 
15
15
  ArmaFixer::Validation.run!
16
16
  ArmaFixer::Execute.run!
17
17
 
18
18
  time_taken = (Time.now - start_time).round(2)
19
- puts "Completed ArmaFixer task in #{time_taken} seconds."
19
+ show "Completed ArmaFixer task in #{time_taken} seconds."
20
20
  end
21
21
  end
@@ -4,45 +4,83 @@ module ArmaFixer
4
4
 
5
5
  def run!
6
6
  ArmaFixer::MODELS_HASH.each do |model_name, columns|
7
- model = ArmaFixer::APP.const_get(model_name)
7
+ records = ArmaFixer::APP.const_get(model_name).where('')
8
+ count = records.count
9
+ if proceed?(model_name, count)
10
+ show "Processing #{count} #{model_name} records. Please wait..."
11
+ saved_record_ids = []
12
+ process_records(records, columns, saved_record_ids)
13
+ show "Saved #{model_name} IDs: #{saved_record_ids.sort!}"
14
+ close_files
15
+ else
16
+ show "Skipped processing #{model_name} records."
17
+ next
18
+ end
19
+ end
20
+ end
21
+
22
+ def show(*args)
23
+ puts(*args)
24
+ end
8
25
 
9
- model.where('').each do |record|
10
- save_record = false
11
- columns.each do |attr, image_path|
12
- value = record.public_send(attr)
13
- next if checker.call(value)
26
+ private
14
27
 
15
- save_record = true
16
- method = "#{attr}="
17
- image = image_path ? File.open(image_path) : default_image
28
+ module_function
18
29
 
19
- value = record.public_send(method, image)
20
- end
30
+ def process_records(records, columns, saved_record_ids)
31
+ records.find_each do |record|
32
+ save_record = false
21
33
 
22
- if save_record
23
- record.save
24
- puts "Saved #{model_name} ID #{record.id}"
25
- end
34
+ columns.each do |attr, path|
35
+ next if valid_value?(record, attr)
36
+ file = fetch_file(path)
37
+ record.public_send("#{attr}=", file)
38
+ save_record = true
26
39
  end
40
+
41
+ save_id(record, saved_record_ids) if save_record
27
42
  end
28
43
  end
29
44
 
30
- private
31
-
32
- module_function
45
+ def proceed?(model_name, count)
46
+ if ENV['ARMAF_CONFIRMATION'] == 'true'
47
+ show "#{model_name} has #{count} records. Do you want to continue? (yes(y) / anything-else)"
48
+ input = STDIN.gets.chomp
49
+ %w(y yes).include?(input)
50
+ else
51
+ true
52
+ end
53
+ end
33
54
 
34
- def default_image
35
- @default_image ||= File.open(ArmaFixer::DEFAULT_IMAGE_PATH) if defined?(ArmaFixer::DEFAULT_IMAGE_PATH)
55
+ def valid_value?(record, attr)
56
+ value = record.public_send(attr)
57
+ checker.call(value)
36
58
  end
37
59
 
38
60
  def checker
39
- @checker ||= begin
40
- if defined?(ArmaFixer::ATTR_CHECKER)
41
- ArmaFixer::ATTR_CHECKER
42
- else
43
- -> (v) { !v.nil? }
44
- end
61
+ if defined?(ArmaFixer::ATTR_CHECKER)
62
+ ArmaFixer::ATTR_CHECKER
63
+ else
64
+ -> (v) { !v.nil? }
45
65
  end
46
66
  end
67
+
68
+ def fetch_file(path)
69
+ file_path = path || ArmaFixer::DEFAULT_IMAGE_PATH
70
+ files_hash[path] ||= File.open(file_path)
71
+ end
72
+
73
+ def files_hash
74
+ @files_hash ||= {}
75
+ end
76
+
77
+ def close_files
78
+ files_hash.each_value.reject(&:autoclose?).each(&:close)
79
+ end
80
+
81
+ def save_id(record, ids)
82
+ record.save(validate: false)
83
+ ids << record.id
84
+ end
47
85
  end
48
86
  end
@@ -1,3 +1,3 @@
1
1
  module ArmaFixer
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -16,8 +16,9 @@ describe Rake do
16
16
  end
17
17
 
18
18
  let(:user_model) do
19
- klass = Class.new
20
- klass.class_eval do
19
+ next app::User if defined?(app::User)
20
+
21
+ app.const_set(:User, Class.new do
21
22
  attr_accessor :code_file, :id
22
23
  def save
23
24
  true
@@ -26,22 +27,15 @@ describe Rake do
26
27
  def self.ancestors
27
28
  [ActiveRecord::Base, *super]
28
29
  end
29
- end
30
-
31
- app.const_set(:User, klass) unless defined?(app::User)
32
- app::User
30
+ end)
33
31
  end
34
32
 
35
33
  let(:user_one) { user_model.new }
36
- let(:user_two) do
37
- user_model.new.tap do |user|
38
- user.code_file = 123
39
- end
40
- end
34
+ let(:user_two) { user_model.new.tap { |u| u.code_file = 123 } }
41
35
  let(:users_list) { [user_one, user_two] }
42
36
 
43
37
  before do
44
- allow(ArmaFixer).to receive(:puts).with(any_args) # suppressing output
38
+ allow(ArmaFixer::Execute).to receive(:show).with(any_args).and_return(nil)
45
39
 
46
40
  expect(user_one).to receive(:save)
47
41
  expect(user_two).to_not receive(:save)
@@ -50,6 +44,10 @@ describe Rake do
50
44
  dc_task.define_task(:environment)
51
45
  allow(user_model).to receive(:where).with('').and_return(users_list)
52
46
 
47
+ users_list.class_eval do
48
+ alias_method :find_each, :each
49
+ end
50
+
53
51
  stub_const('ArmaFixer::APP', app)
54
52
  stub_const('ArmaFixer::MODELS_HASH', models_hash)
55
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arma_fixer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Humza Shah
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.0
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: activerecord
85
99
  requirement: !ruby/object:Gem::Requirement