exodus 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ## v1.0.5
2
+
3
+ * Added helpers folder + module to do cleaner and prettier prints
4
+
1
5
  ## v1.0.4
2
6
 
3
7
  * Custom migrations now runs in the given order
data/README.md CHANGED
@@ -64,10 +64,6 @@ Exodus - a migration framework for MongoDb
64
64
  ## To Do when writting your own
65
65
 
66
66
  * Give it a migration_number
67
- class MyMigration < Exodus::Migration
68
- self.migration_number = 1
69
- end
70
-
71
67
  * Initialize it and define status_complete and description
72
68
  * Write the UP method that will be call when migrating the migration
73
69
  * Write the DOWN method that will be call when rolling back the migration
@@ -1,4 +1,5 @@
1
1
  require 'mongo_mapper'
2
+ require File.dirname(__FILE__) + '/exodus/helpers/text_formatter'
2
3
  require File.dirname(__FILE__) + '/exodus/config/migration_info'
3
4
  require File.dirname(__FILE__) + '/exodus/migrations/migration'
4
5
  require File.dirname(__FILE__) + '/exodus/migrations/migration_error'
@@ -0,0 +1,75 @@
1
+ module Exodus
2
+ module TextFormatter
3
+
4
+ # Prints a paragraphes
5
+ def super_print(paragraphes, space_number = 50, title = true)
6
+ puts format_paragraph(space_number, title, *paragraphes)
7
+ end
8
+
9
+ private
10
+
11
+ # Transforms an array of paragraphes to a String using lines and columns
12
+ # Each paragraph is actually an Array of string, where each string is a sentence of a given column
13
+ # if the sentence contains to much caractere the sentence will be splitted (using whitespaces) and written on several lines
14
+ # e,g considering paragraphes = [["id", "type"]["id_1", "test"]]
15
+ # format_paragraph will print:
16
+ # id type
17
+ # id_1 test
18
+ def format_paragraph(space_number, title, *paragraphes)
19
+ column_size = paragraphes.max_by{|paragraph| paragraph.size}.size
20
+ @full_text = Hash[*column_size.times.map {|i| [i,[]]}.flatten(1)]
21
+
22
+ paragraphes.each_with_index do |sentences, paragraph_number|
23
+ sentences.each_with_index do |sentence, column|
24
+ if sentence.size > space_number && (words = sentence.gsub('=>', ' => ').split(' ')).size > 1
25
+ new_sentence = ""
26
+ words.each_with_index do |word, nb_word|
27
+ if new_sentence.size + word.size < space_number
28
+ new_sentence << word << ' '
29
+ else
30
+ insert_line(column, new_sentence) unless new_sentence.empty?
31
+ new_sentence = word << ' '
32
+ end
33
+ end
34
+
35
+ insert_line(column, new_sentence) unless new_sentence == @full_text[column].last
36
+ else
37
+ insert_line(column, sentence)
38
+ end
39
+ end
40
+
41
+ @full_text.each {|column, lines| (@max_lines - lines.size).times { lines << '' }}
42
+ space = paragraph_number == 0 && title ? "/nbspace" : ""
43
+ @full_text.each {|column, lines| lines << space}
44
+ end
45
+
46
+ stringify_paragraph
47
+ end
48
+
49
+ # Creates a String from a Hash of the following format {column_number => [lines]}
50
+ # "/nbspace" is used to define a border
51
+ def stringify_paragraph
52
+ ordered_lines = {}
53
+
54
+ spaces = @full_text.map {|column, lines| lines.max_by {|sentence| sentence.size}.size}
55
+ @full_text.each_with_index do |(column, lines), i|
56
+ lines.each_with_index do |line, line_number|
57
+ if line == "/nbspace"
58
+ (ordered_lines[line_number] ||= "") << line.gsub("/nbspace", "-" * (spaces[i] + 4))
59
+ else
60
+ (ordered_lines[line_number] ||= "") << line.to_s.ljust(spaces[i] + 4)
61
+ end
62
+ end
63
+ end
64
+
65
+ ordered_lines.values.join("\n")
66
+ end
67
+
68
+ # Inserts a line at the correcponding column and re-sets the number of maximum line if the maximum has been increased
69
+ def insert_line(column, line)
70
+ @max_lines ||= 0
71
+ @full_text[column] << line
72
+ @max_lines = @full_text[column].size if @max_lines < @full_text[column].size
73
+ end
74
+ end
75
+ end
@@ -1,6 +1,7 @@
1
1
  module Exodus
2
2
  class Migration
3
3
  include MongoMapper::Document
4
+ extend Exodus::TextFormatter
4
5
  set_collection_name 'migrations'
5
6
 
6
7
  UP = 'up'
@@ -69,27 +70,24 @@ module Exodus
69
70
 
70
71
  # Prints in the console all migrations class with their name and description
71
72
  def list
72
- puts "\n Migration n#: \t\t Name: \t\t\t\t Description:"
73
- puts '-' * 100, "\n"
73
+ last_info = [["Migration n#:", "Name:", "Description:"]]
74
74
 
75
- Migration.sort_all.map do|migration, args|
76
- m = migration.new
77
- puts "\t#{migration.migration_number} \t\t #{migration.name} \t\t #{m.description}"
75
+ last_info |= Migration.sort_all.map do|migration, args|
76
+ [migration.migration_number, migration.name, migration.new.description]
78
77
  end
79
78
 
80
- puts "\n\n"
79
+ super_print(last_info, 70)
81
80
  end
82
81
 
83
82
  # Prints in the console all migrations that has been ran at least once with their name and description
84
83
  def db_status
85
- puts "\n Migration n#: \t Name: \t\t Direction: Arguments: Current Status: \t Last completion Date: \t\t Current Message:"
86
- puts '-' * 175, "\n"
84
+ status_info = [["Migration n#:", "Name:", "Direction:", "Current Status:", "Arguments:", "Last completion Date:", "Current Message:"]]
87
85
 
88
- Migration.all.each do|migration|
89
- puts "\t#{migration.class.migration_number} \t #{migration.class.name} \t #{migration.status.to_string}"
86
+ status_info |= Migration.all.map do|migration|
87
+ [migration.class.migration_number, migration.class.name, *migration.status.to_a_string]
90
88
  end
91
89
 
92
- puts "\n\n"
90
+ super_print(status_info)
93
91
  end
94
92
  end
95
93
 
@@ -25,7 +25,15 @@ module Exodus
25
25
  end
26
26
 
27
27
  def to_string
28
- "\t#{direction}\t\t #{arguments}\t\t #{current_status} \t\t #{last_succesful_completion} \t\t #{message}"
28
+ "\t#{direction}\t\t #{current_status} \t\t #{arguments}\t\t #{last_succesful_completion} \t\t #{message}"
29
+ end
30
+
31
+ def to_a
32
+ [direction, current_status, arguments, last_succesful_completion, message]
33
+ end
34
+
35
+ def to_a_string
36
+ self.to_a.map(&:to_s)
29
37
  end
30
38
 
31
39
  # Resets a status
@@ -1,3 +1,3 @@
1
1
  module Exodus
2
- VERSION = "1.0.4"
2
+ VERSION = "1.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exodus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-08 00:00:00.000000000 Z
12
+ date: 2013-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo_mapper
@@ -95,6 +95,7 @@ files:
95
95
  - exodus.gemspec
96
96
  - lib/exodus.rb
97
97
  - lib/exodus/config/migration_info.rb
98
+ - lib/exodus/helpers/text_formatter.rb
98
99
  - lib/exodus/migrations/migration.rb
99
100
  - lib/exodus/migrations/migration_error.rb
100
101
  - lib/exodus/migrations/migration_status.rb