arma_fixer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a09ab5b4665de2f3e31bb39cc5767ded2c79ce76
4
+ data.tar.gz: 1926b5464ae286f739ac5f6d9920e5ffb74f9661
5
+ SHA512:
6
+ metadata.gz: 40afc47969507acef1ef8a5444f8d2408cd11584501981ba9f7b286d8b17e666466e1042f86317ede86a377576154c8bd1691252a8c8337670b9fb61a0fd84a4
7
+ data.tar.gz: 9df60ad0aba67b5d7cb24fb2b7c52384f677ee9da12d4d146b2e60594bf54cc2a12dacdb882cff78190ec82d1edc691dbf14653e27427e5b645d3b5dc7f69035
@@ -0,0 +1,21 @@
1
+ require_relative './arma_fixer/version'
2
+ require_relative './arma_fixer/validation'
3
+ require_relative './arma_fixer/execute'
4
+ require_relative './arma_fixer/railtie' if defined?(Rails)
5
+
6
+ module ArmaFixer
7
+ mattr_accessor :root_path
8
+
9
+ module_function
10
+
11
+ def run!
12
+ start_time = Time.now
13
+ puts 'Started ArmaFixer task...'
14
+
15
+ ArmaFixer::Validation.run!
16
+ ArmaFixer::Execute.run!
17
+
18
+ time_taken = (Time.now - start_time).round(2)
19
+ puts "Completed ArmaFixer task in #{time_taken} seconds."
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ module ArmaFixer
2
+ module Execute
3
+ module_function
4
+
5
+ def run!
6
+ ArmaFixer::MODELS_HASH.each do |model_name, columns|
7
+ model = ArmaFixer::APP.const_get(model_name)
8
+
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)
14
+
15
+ save_record = true
16
+ method = "#{attr}="
17
+ image = image_path ? File.open(image_path) : default_image
18
+
19
+ value = record.public_send(method, image)
20
+ end
21
+
22
+ if save_record
23
+ record.save
24
+ puts "Saved #{model_name} ID #{record.id}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ module_function
33
+
34
+ def default_image
35
+ @default_image ||= File.open(ArmaFixer::DEFAULT_IMAGE_PATH) if defined?(ArmaFixer::DEFAULT_IMAGE_PATH)
36
+ end
37
+
38
+ def checker
39
+ @checker ||= begin
40
+ if defined?(ArmaFixer::ATTR_CHECKER)
41
+ ArmaFixer::ATTR_CHECKER
42
+ else
43
+ -> (v) { !v.nil? }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ require 'arma_fixer'
2
+ require 'rails'
3
+
4
+ module ArmaFixer
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :arma_fixer
7
+
8
+ rake_tasks do
9
+ load File.expand_path('../tasks/run.rake', __FILE__)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ desc 'Repair missing attachments in ActiveRecord models'
2
+ namespace :armaf do
3
+ task run: :environment do
4
+ ArmaFixer.run!
5
+ end
6
+ end
@@ -0,0 +1,76 @@
1
+ require 'active_record'
2
+
3
+ module ArmaFixer
4
+ module Validation
5
+ module_function
6
+
7
+ def run!
8
+ validate_models_hash!
9
+ validate_default_image_path!
10
+ validate_attribute_checker!
11
+ end
12
+
13
+ private
14
+
15
+ module_function
16
+
17
+ def validate_models_hash!
18
+ validate_models_hash_presence!
19
+ validate_models_hash_contents!
20
+ end
21
+
22
+ def validate_models_hash_presence!
23
+ return if defined?(ArmaFixer::MODELS_HASH) &&
24
+ ArmaFixer::MODELS_HASH &&
25
+ !ArmaFixer::MODELS_HASH.empty?
26
+
27
+ fail 'ArmaFixer::MODELS_HASH cannot be undefined or empty.'
28
+ end
29
+
30
+ def validate_models_hash_contents!
31
+ ArmaFixer::MODELS_HASH.each_value do |attributes|
32
+ case attributes
33
+ when Array
34
+ if attributes.empty?
35
+ fail 'attributes array cannot be empty'
36
+ elsif !default_image_path_defined?
37
+ fail 'ArmaFixer::DEFAULT_IMAGE_PATH must exist in case of array attributes'
38
+ end
39
+ when Hash
40
+ if attributes.empty?
41
+ fail 'attributes hash cannot be empty'
42
+ else
43
+ attributes.each_value do |path|
44
+ fail "file not found at #{path}" unless File.exist?(path)
45
+ end
46
+ end
47
+ else
48
+ fail 'attributes must be in form of hash or array'
49
+ end
50
+ end
51
+ end
52
+
53
+ def validate_default_image_path!
54
+ return unless default_image_path_defined?
55
+ path = ArmaFixer::DEFAULT_IMAGE_PATH
56
+ fail 'ArmaFixer::DEFAULT_IMAGE_PATH must be a string' unless path.is_a?(String)
57
+ fail "file not found at '#{path}'" unless File.exist?(path)
58
+ end
59
+
60
+ def default_image
61
+ ArmaFixer::DEFAULT_IMAGE_PATH if default_image_path_defined?
62
+ end
63
+
64
+ def default_image_path_defined?
65
+ defined?(ArmaFixer::DEFAULT_IMAGE_PATH)
66
+ end
67
+
68
+ def validate_attribute_checker!
69
+ if defined?(ArmaFixer::ATTR_CHECKER)
70
+ unless ArmaFixer::ATTR_CHECKER.is_a?(Proc)
71
+ fail 'ArmaFixer::ATTR_CHECKER must be a lambda'
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module ArmaFixer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ require 'rake'
3
+
4
+ describe Rake do
5
+ subject(:run_task) { dc_task['armaf:run'].execute }
6
+
7
+ let(:this_file) { File.expand_path(__FILE__) }
8
+ let(:dc) { described_class }
9
+ let(:dc_task) { dc::Task }
10
+
11
+ let(:app) do
12
+ module TestingThis
13
+ end
14
+
15
+ TestingThis
16
+ end
17
+
18
+ let(:user_model) do
19
+ klass = Class.new
20
+ klass.class_eval do
21
+ attr_accessor :code_file, :id
22
+ def save
23
+ true
24
+ end
25
+
26
+ def self.ancestors
27
+ [ActiveRecord::Base, *super]
28
+ end
29
+ end
30
+
31
+ app.const_set(:User, klass) unless defined?(app::User)
32
+ app::User
33
+ end
34
+
35
+ 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
41
+ let(:users_list) { [user_one, user_two] }
42
+
43
+ before do
44
+ allow(ArmaFixer).to receive(:puts).with(any_args) # suppressing output
45
+
46
+ expect(user_one).to receive(:save)
47
+ expect(user_two).to_not receive(:save)
48
+
49
+ dc.load_rakefile('Rakefile')
50
+ dc_task.define_task(:environment)
51
+ allow(user_model).to receive(:where).with('').and_return(users_list)
52
+
53
+ stub_const('ArmaFixer::APP', app)
54
+ stub_const('ArmaFixer::MODELS_HASH', models_hash)
55
+ end
56
+
57
+ describe 'rake armaf:run' do
58
+ after do
59
+ expect(user_one.code_file).to be_a File
60
+ expect(user_two.code_file).to eq 123
61
+ end
62
+
63
+ context 'MODELS_HASH is an array' do
64
+ before { stub_const('ArmaFixer::DEFAULT_IMAGE_PATH', this_file) }
65
+ let(:models_hash) { { user_model.to_s => [:code_file] } }
66
+ it { run_task }
67
+ end
68
+
69
+ context 'MODELS_HASH is a hash' do
70
+ let(:models_hash) { { user_model.to_s => { code_file: this_file } } }
71
+ it { run_task }
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arma_fixer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Syed Humza Shah
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.34.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.34.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ description: Replace missing image files in your models with defaults.
98
+ email:
99
+ - humzashah@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - lib/arma_fixer/tasks/run.rake
105
+ - lib/arma_fixer/execute.rb
106
+ - lib/arma_fixer/railtie.rb
107
+ - lib/arma_fixer/validation.rb
108
+ - lib/arma_fixer/version.rb
109
+ - lib/arma_fixer.rb
110
+ - spec/lib/tasks/rake_spec.rb
111
+ homepage: http://github.com/humzashah/arma_fixer
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message: Thanks for installing ArmaFixer!
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: 2.0.0
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: 2.0.0
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.0.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: ActiveRecord Model Image Fixer
135
+ test_files:
136
+ - spec/lib/tasks/rake_spec.rb