Class: Filesystem
- Inherits:
-
Object
- Object
- Filesystem
- Defined in:
- lib/generators/ranker/misc/filesystem.rb
Constant Summary collapse
- ORIGIN =
Serves migration file generation.
'migration_origin.rb'
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #clear_dir! ⇒ Object
- #filename ⇒ Object
-
#initialize(options) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #migration_origin ⇒ Object
- #migration_target ⇒ Object
- #store(source) ⇒ Object
- #unique? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Filesystem
Returns a new instance of Filesystem.
10 11 12 13 14 15 16 17 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 10 def initialize() @timestamp = @model_name = .model_name @table_name = .table_name @collection_name = .collection_name clear_dir! end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 8 def errors @errors end |
Instance Method Details
#clear_dir! ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 62 def clear_dir! # Delete all temporary files from templetes. files = Dir.glob(File.join(templates_dir, '*')) files.delete_if { |f| f =~ /\/#{ORIGIN}$/ } FileUtils.rm_rf(files) true end |
#filename ⇒ Object
30 31 32 33 34 35 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 30 def filename # Name of migration file # Instance: 20150827193501_add_category_rank_to_products.rb @filename ||= "#{@timestamp}_add_#{@collection_name}_rank_to_#{@table_name}.rb" end |
#migration_origin ⇒ Object
37 38 39 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 37 def migration_origin @migration_origin ||= File.join(templates_dir, ORIGIN) end |
#migration_target ⇒ Object
41 42 43 44 45 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 41 def migration_target # Resulting file @migration_target ||= File.join(templates_dir, filename) end |
#store(source) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 19 def store(source) begin File.open(migration_target, 'w') { |file| file.write(source) } true rescue @errors ||= [] @errors << "Can't write file: #{migration_target}. Check permissions." false end end |
#unique? ⇒ Boolean
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/generators/ranker/misc/filesystem.rb', line 47 def unique? # Check that project doesn't use similar file. # E.g. there are NO ~/rails_project/db/migrate/*add_SAME_COLLECTION_rank_to_SAME_TABLE.rb* files = Dir.glob(File.join(migrate_dir, '*')) if files.any? { |file| file =~ /#{filename[15..-1]}$/} @errors ||= [] @errors << "Such migration already exists. Try `rake db:migrate`." false else true end end |