arfy 0.2 → 0.2.1

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.
@@ -1,49 +1,14 @@
1
1
  require 'erb'
2
2
 
3
- module StringPimped
4
- #code from: http://sequel.rubyforge.org/rdoc-plugins/classes/String.html
5
- def underscore
6
- gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
7
- gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
8
- end
9
-
10
- def camelize(first_letter_in_uppercase =:upper)
11
- s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.
12
- gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
13
- s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
14
- s
15
- end
16
-
17
- def classify
18
- str = sub(/.*\./, '')
19
- class << str; include StringPimped; end
20
- str.camelize
21
- end
22
-
23
- def respond_to? method
24
- @i_know ||= [:underscore, :camelize, :classify]
25
- if @i_know.include? method
26
- return true
27
- end
28
- super
29
- end
30
- end
31
-
32
3
  module Arfy
33
4
  class Generator
34
5
  def generate_migration_file(migration_name, dir)
6
+ dir ||= Rails.application.config.paths['db/migrate']
35
7
  migration_path = File.join(dir, new_migration_file_name_for(migration_name))
36
8
  File.open(migration_path, "w") do |file|
37
9
  file << code_for_migration(migration_name)
38
10
  end
39
-
40
- if migration_path
41
- puts "migration generated on: #{file}"
42
- else
43
- puts "FAIL. Migration not generated."
44
- end
45
-
46
- File.exists? migration_path
11
+ migration_path
47
12
  end
48
13
 
49
14
  private
@@ -56,7 +21,7 @@ module Arfy
56
21
  new_string = String.new(string)
57
22
  unless new_string.respond_to? method
58
23
  class << new_string
59
- include StringPimped
24
+ include StringNamefyExtension
60
25
  end
61
26
  end
62
27
  new_string.send(method)
@@ -92,3 +57,4 @@ module Arfy
92
57
 
93
58
  end
94
59
  end
60
+
@@ -0,0 +1,105 @@
1
+ require 'arfy/migration_builder/template_handler'
2
+ require 'arfy/migration_builder/builders/column'
3
+ require 'arfy/migration_builder/builders/create_table'
4
+ require 'arfy/migration_builder/builders/drop_table'
5
+ require 'arfy/migration_builder/builders/rename_table'
6
+ require 'arfy/migration_builder/builders/add_column'
7
+ require 'arfy/migration_builder/builders/change_column'
8
+ require 'arfy/migration_builder/builders/rename_column'
9
+ require 'arfy/migration_builder/builders/remove_column'
10
+ require 'arfy/migration_builder/builders/add_index'
11
+ require 'arfy/migration_builder/builders/remove_index'
12
+
13
+ module Arfy
14
+ module MigrationBuilder
15
+ class Builder
16
+ def initialize(name, work_dir, id_generator)
17
+ @id_generator = id_generator
18
+ @work_dir = work_dir
19
+ @name = augment_string(name)
20
+ @builder = nil
21
+ end
22
+
23
+ def build(builder = nil)
24
+ @builder = builder
25
+ create_file
26
+ tpl_handler = TemplateHandler.new(migration_template, template_vars)
27
+ tpl_code = tpl_handler.render
28
+
29
+ File.open(final_file_path, "w") do |f|
30
+ f << tpl_code
31
+ end
32
+
33
+ tpl_code
34
+ end
35
+
36
+ def create_table(table_name)
37
+ CreateTable.new(table_name)
38
+ end
39
+
40
+ def drop_table(table_name)
41
+ DropTable.new(table_name)
42
+ end
43
+
44
+ private
45
+ def migration_template
46
+ %{
47
+ class <%= klass %> < ActiveRecord::Migration
48
+ <% if !change_code.nil? && change_code.length > 0 %>
49
+ def change
50
+ <%= change_code %>
51
+ end
52
+ <% else %>
53
+ def up
54
+ <%= up_code %>
55
+ end
56
+
57
+ def down
58
+ <%= down_code %>
59
+ end
60
+ <% end %>
61
+ end
62
+ }
63
+ end
64
+
65
+ def template_vars
66
+ vars = {}
67
+ unless @builder.nil?
68
+ vars.merge! @builder.code_for_template
69
+ end
70
+ vars[:klass] = @name.classify
71
+ vars
72
+ end
73
+
74
+ def augment_string(original_string)
75
+ string = String.new(original_string)
76
+ unless string.respond_to?(:underscore)
77
+ class << string
78
+ include StringNamefyExtension
79
+ end
80
+ end
81
+ string
82
+ end
83
+
84
+ def file_name
85
+ @file_identifier = @id_generator.identify
86
+ "#{@file_identifier}_#{@name.underscore}.rb"
87
+ end
88
+
89
+ def file_path
90
+ @work_dir
91
+ end
92
+
93
+ def final_file_path
94
+ @final_file_path ||= File.join(file_path, file_name)
95
+ end
96
+
97
+ def create_file
98
+ FileUtils.mkdir_p(file_path) unless Dir.exists? file_path
99
+ FileUtils.touch(final_file_path)
100
+ final_file_path
101
+ end
102
+ end
103
+ end
104
+ end
105
+
@@ -0,0 +1,17 @@
1
+ require 'arfy/migration_builder/builders/column.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class AddColumn < ColumnMigration
6
+ include Reversible
7
+
8
+ private
9
+ def template_up_code
10
+ %{
11
+ add_column :<%= table_name %>, :<%= column_name %>, :<%= column_type %><% if options && options.length > 0 %>, <%= options %><% end %>
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,41 @@
1
+ require 'arfy/migration_builder/builders/column.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class AddIndex < ColumnMigration
6
+ include Reversible
7
+
8
+ def initialize(table_name, column_name, options = nil)
9
+ super(table_name, column_name)
10
+ unless options.nil?
11
+ @name = options[:name]
12
+ @unique = options[:unique]
13
+ end
14
+ end
15
+
16
+ private
17
+ def vars_for_direction(direction)
18
+ vars = super
19
+ if direction == :up
20
+ vars[:options] = all_options
21
+ end
22
+ vars
23
+ end
24
+
25
+ def all_options
26
+ options = ""
27
+ options << ":name => \"#{@name}\"" unless @name.nil?
28
+ unless @unique.nil?
29
+ options << ", " if options.length > 0
30
+ options << ":unique => #{@unique ? "true" : "false"}"
31
+ end
32
+ options
33
+ end
34
+
35
+ def template_up_code
36
+ %{ add_index :<%= table_name %>, :<%= column_name %><% if options.length > 0 %>, <%= options %><% end %>}
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,16 @@
1
+ require 'arfy/migration_builder/builders/column.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class ChangeColumn < ColumnMigration
6
+
7
+ private
8
+ def template_up_code
9
+ %{
10
+ change_column :<%= table_name %>, :<%= column_name %>, :<%= column_type %><% if options && options.length > 0 %>, <%= options %><% end %>
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,147 @@
1
+ require 'arfy/migration_builder/generic_migration.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class InvalidColumnTypeError < Exception
6
+ end
7
+
8
+ COLUMN_VALID_TYPES = [:primary_key, :string, :text, :integer, :float, :decimal,
9
+ :datetime, :timestamp, :time, :date, :binary, :boolean].to_enum
10
+
11
+ COLUMN_VALID_OPTIONS = [:limit, :default, :null, :precision, :scale].to_enum
12
+
13
+ class ColumnMigration < GenericMigration
14
+ def initialize(table_name, column, type = nil, options = nil)
15
+ super table_name
16
+ @column = Column.new(column, type)
17
+ unless options.nil?
18
+ options.each do |name, value|
19
+ @column.add_option name, value
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+ def method_missing(method, *args)
26
+ if @column.respond_to? method
27
+ return @column.send method, *args
28
+ end
29
+ super
30
+ end
31
+
32
+ def vars_for_direction(direction)
33
+ vars = {}
34
+ if direction == :up
35
+ vars = {
36
+ :table_name => @table_name,
37
+ :column_name => @column.name,
38
+ :column_type => @column.type,
39
+ :options => @column.options_string
40
+ }
41
+ end
42
+ vars
43
+ end
44
+ end
45
+
46
+ class Option
47
+ attr_reader :name, :value
48
+ def initialize(name, value=nil)
49
+ super
50
+ @name = name
51
+ @value = value
52
+ end
53
+
54
+ def value_string
55
+ value_string = ""
56
+ if @value.class == String
57
+ value_string = "\"#{@value}\""
58
+ elsif @value.class == Symbol
59
+ value_string = ":#{@value}"
60
+ else
61
+ value_string = @value.to_s
62
+ end
63
+ value_string
64
+ end
65
+ end
66
+
67
+ class Column
68
+ attr_reader :name, :type
69
+ def initialize(name, type=nil)
70
+ unless type.nil?
71
+ raise InvalidColumnTypeError, type unless valid_types.include? type
72
+ end
73
+ @name = name
74
+ @type = type
75
+ end
76
+
77
+ def respond_to?(method, includes_private_methods=false)
78
+ valid_option = is_valid_option_getter?(method) || is_valid_option_setter?(method)
79
+ return valid_option || super
80
+ end
81
+
82
+ def options_string
83
+ return "" if @options.nil?
84
+ processed_options.join(", ")
85
+ end
86
+
87
+ def add_option(option_name, value=nil)
88
+ @options ||= []
89
+ if value
90
+ option = Option.new(option_name, value)
91
+ else
92
+ # seria bom conferir se é um option mesmo?
93
+ option = option_name
94
+ end
95
+ @options << option
96
+ option
97
+ end
98
+
99
+ private
100
+ def processed_options
101
+ options = @options.map do |option|
102
+ unless option.value.nil?
103
+ ":#{option.name} => #{option.value_string}"
104
+ end
105
+ end
106
+ options.compact
107
+ end
108
+
109
+ def valid_types
110
+ COLUMN_VALID_TYPES
111
+ end
112
+
113
+ def valid_options
114
+ COLUMN_VALID_OPTIONS
115
+ end
116
+
117
+ def is_valid_option_getter?(option_getter)
118
+ valid_options.include? option_getter
119
+ end
120
+
121
+ def is_valid_option_setter?(option_setter)
122
+ valid_option_setters = valid_options.map {|opt| "#{opt.to_s}=".to_sym}
123
+ valid_option_setters.include? option_setter
124
+ end
125
+
126
+ def option_for(option_name)
127
+ return "" if @options.nil?
128
+ options = @options.select do |item|
129
+ item.name == option_name
130
+ end
131
+ return "" if options.length <= 0
132
+ options[0].value
133
+ end
134
+
135
+ def method_missing(method, *args)
136
+ if is_valid_option_setter? method
137
+ option_name = method.to_s.gsub("=", "").to_sym
138
+ add_option(option_name, args[0])
139
+ elsif is_valid_option_getter? method
140
+ return option_for method
141
+ else
142
+ super
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,30 @@
1
+ require 'arfy/migration_builder/generic_migration.rb'
2
+ module Arfy
3
+ module MigrationBuilder
4
+
5
+ class CreateTable < GenericMigration
6
+ include Reversible
7
+
8
+ private
9
+ def vars_for_direction(direction)
10
+ vars = super
11
+ if direction == :up
12
+ vars[:columns] = @columns || {}
13
+ end
14
+ vars
15
+ end
16
+
17
+ def template_up_code
18
+ %{ create_table :<%= table_name %> do |t|
19
+ <% columns.each do |column| %>
20
+ t.<%= column.type %> :<%= column.name %><% if column.options_string.length > 0 %>, <%= column.options_string %><% end %>
21
+ <% end %>
22
+ end}
23
+ end
24
+
25
+ def template_down_code
26
+ %{ drop_table :<%= table_name %>}
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ require 'arfy/migration_builder/generic_migration.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class DropTable < GenericMigration
6
+
7
+ private
8
+ def template_up_code
9
+ %{
10
+ drop_table :<%= table_name %>
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,14 @@
1
+ require 'arfy/migration_builder/builders/column.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class RemoveColumn < ColumnMigration
6
+ private
7
+ def template_up_code
8
+ %{ remove_column :<%= table_name %>, :<%= column_name %>}
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+