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.
@@ -0,0 +1,44 @@
1
+ require 'arfy/migration_builder/builders/column.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class RemoveIndex < ColumnMigration
6
+
7
+ def initialize(table_name)
8
+ super(table_name, nil)
9
+ end
10
+
11
+ def index=(index)
12
+ @index_name = index
13
+ end
14
+
15
+ def column=(column)
16
+ @column_name = column
17
+ end
18
+
19
+ private
20
+ def vars_for_direction(direction)
21
+ vars = super
22
+ if direction == :up
23
+ vars[:index_name] = @index_name
24
+ vars[:column_name] = @column_name
25
+ end
26
+ vars
27
+ end
28
+
29
+ def option
30
+ if @index_name
31
+ result = ":name => :#{@index_name}"
32
+ else @column_name
33
+ result = ":column => :#{@column_name}"
34
+ end
35
+ result
36
+ end
37
+
38
+ def template_up_code
39
+ %{ remove_index :<%= table_name %>, #{option}}
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,26 @@
1
+ require 'arfy/migration_builder/builders/column.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class RenameColumn < ColumnMigration
6
+ include Reversible
7
+
8
+ def initialize(table, column_name, new_column_name)
9
+ super(table, column_name)
10
+ @new_column_name = new_column_name
11
+ end
12
+
13
+ def vars_for_direction(direction)
14
+ vars = super
15
+ vars[:new_column_name] = @new_column_name
16
+ vars
17
+ end
18
+
19
+ private
20
+ def template_up_code
21
+ %{ rename_column :<%= table_name %>, :<%= column_name %>, :<%= new_column_name %>}
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,28 @@
1
+ require 'arfy/migration_builder/generic_migration.rb'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class RenameTable < GenericMigration
6
+ include Reversible
7
+
8
+ def initialize(table_name, new_table_name)
9
+ @new_table_name = new_table_name
10
+ super table_name
11
+ end
12
+
13
+ private
14
+ def vars_for_direction(direction)
15
+ vars = super
16
+ if direction == :up
17
+ vars[:new_table_name] = @new_table_name
18
+ end
19
+ vars
20
+ end
21
+
22
+ def template_up_code
23
+ %{ rename_table :<%= table_name %>, :<%= new_table_name %>}
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,98 @@
1
+ module Arfy
2
+ module MigrationBuilder
3
+ class MigrationTemplateNotFoundError < Exception
4
+ end
5
+
6
+ module Reversible
7
+ private
8
+ def change_code
9
+ code_for_direction :up, :template => template_up_code
10
+ end
11
+ end
12
+
13
+ class GenericMigration
14
+ def initialize(table_name)
15
+ @table_name = table_name
16
+ end
17
+
18
+ def respond_to?(method, include_private=false)
19
+ if COLUMN_VALID_TYPES.include? method
20
+ return true
21
+ end
22
+ super
23
+ end
24
+
25
+ def code_for_template
26
+ code = {:change_code => "", :up_code => "", :down_code => ""}
27
+
28
+ if im_reversible?
29
+ code[:change_code] = change_code
30
+ else
31
+ code[:up_code] = up_code
32
+ code[:down_code] = down_code
33
+ end
34
+
35
+ code
36
+ end
37
+
38
+ private
39
+ def vars_for_direction(direction)
40
+ {:table_name => @table_name}
41
+ end
42
+
43
+ def code_for_direction(direction, opts = {})
44
+ default_opts = {:template => template_up_code}
45
+ options = default_opts.merge opts
46
+
47
+ vars = vars_for_direction direction
48
+ process(options[:template], vars)
49
+ end
50
+
51
+ def up_code
52
+ code_for_direction :up, :template => template_up_code
53
+ end
54
+
55
+ def down_code
56
+ code_for_direction :down, :template => template_down_code
57
+ end
58
+
59
+ def process(template, vars)
60
+ template_handle(template, vars)
61
+ end
62
+
63
+ def im_reversible?
64
+ return respond_to? :change_code, true
65
+ end
66
+
67
+ def template_down_code
68
+ %{ raise ActiveRecord::IrreversibleMigration}
69
+ end
70
+
71
+ def template_handle(template, vars)
72
+ TemplateHandler.new(template, vars).render
73
+ end
74
+
75
+ def add_column(name, type, column_options)
76
+ @columns ||= []
77
+ begin
78
+ column_options ||= {}
79
+ column = Column.new(name, type)
80
+ column_options.each do |option, value|
81
+ column.add_option(Option.new(option, value))
82
+ end
83
+ @columns << column
84
+ return column
85
+ rescue InvalidColumnTypeError
86
+ end
87
+ return nil
88
+ end
89
+
90
+ def method_missing(method, *args)
91
+ column_name = args[0]
92
+ column_type = method
93
+ column_options = args[1]
94
+ return super unless add_column(column_name, column_type, column_options)
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,47 @@
1
+ require 'ostruct'
2
+
3
+ module Arfy
4
+ module MigrationBuilder
5
+ class ERBBinder < OpenStruct
6
+ def bind
7
+ binding
8
+ end
9
+ end
10
+
11
+ class TemplateHandler
12
+ def initialize(template_body, vars)
13
+ @vars = vars || {}
14
+ if(File.exists?(template_body))
15
+ @template_path = template_body if File.exists?(template_body)
16
+ else
17
+ @template_body = template_body
18
+ end
19
+ end
20
+
21
+ def template_body
22
+ if @template_body.nil?
23
+ @template_body = read_template_file(@template_path)
24
+ end
25
+ @template_body
26
+ end
27
+
28
+ def read_template_file(template_path)
29
+ template_body = ''
30
+ File.open(template_path, 'r') do |file|
31
+ while line = file.gets
32
+ template_body << line
33
+ end
34
+ end
35
+ template_body
36
+ end
37
+
38
+ def template
39
+ ERB.new(template_body, 0, "<>")
40
+ end
41
+
42
+ def render
43
+ template.result(ERBBinder.new(@vars).send(:bind))
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ module StringNamefyExtension
2
+ #code from: http://sequel.rubyforge.org/rdoc-plugins/classes/String.html
3
+ def underscore
4
+ gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
6
+ end
7
+
8
+ def camelize(first_letter_in_uppercase =:upper)
9
+ s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.
10
+ gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
11
+ s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
12
+ s
13
+ end
14
+
15
+ def classify
16
+ str = sub(/.*\./, '').tr("-", "")
17
+ class << str; include StringNamefyExtension; end
18
+ str.camelize
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Arfy
2
- VERSION = "0.2" unless defined?(::Arfy::VERSION)
2
+ VERSION = "0.2.1" unless defined?(::Arfy::VERSION)
3
3
  end
@@ -0,0 +1,2 @@
1
+ require "arfy"
2
+ require "tasks/all"
@@ -1,42 +1,46 @@
1
1
  task :environment do
2
2
  # requiring config/environment
3
+ env = Arfy::Environment.new
4
+ env.database_connect
5
+ env.prepare_active_record_module
6
+ end
3
7
 
4
- ActiveRecord::Base.establish_connection(Rails.application.config.database_configuration[Rails.env])
5
-
6
- module ActiveRecord
7
- class Base
8
- def self.establish_connection(configuration = nil)
9
- #forget about it, we already have a connection XD
10
- end
8
+ task :rails_env => "arfy:rails_env"
11
9
 
12
- def self.configurations
13
- Rails.application.config.database_configuration
14
- end
15
- end
10
+ namespace :arfy do
11
+ desc "Changes the environment (options TARGET=development) - use your's configuration name on database.yml"
12
+ task :rails_env do
13
+ env = Arfy::Environment.new
14
+ env.set_rails_env(ENV["TARGET"])
16
15
  end
17
16
 
18
- end
19
-
20
- desc "Changes the environment (options TARGET=development) - use your's configuration name on database.yml"
21
- task :rails_env do
22
- ENV['RAILS_ENV'] = (ENV["TARGET"] || ENV["RAILS_ENV"]) || "development"
23
- unless defined? RAILS_ENV
24
- RAILS_ENV = ENV['RAILS_ENV']
17
+ namespace :generate do
18
+ desc "Generate migration (options NAME=migration_file_name, OUTDIR=db/migrate, TARGET=environment)"
19
+ task :migration => :rails_env do
20
+ raise "missing migration name, use the option NAME=migration_file_name" unless ENV["NAME"]
21
+ generator = Arfy::Generator.new
22
+ file = generator.generate_migration_file ENV["NAME"], ENV["OUTDIR"]
23
+ puts "migration generated on: #{file}"
24
+ end
25
25
  end
26
- puts "working on environment: #{ENV['RAILS_ENV']}" if ENV["RAILS_ENV"]
27
- end
28
-
29
- namespace :generate do
30
- desc "Generate migration (options NAME=migration_file_name, OUTDIR=db/migrate, TARGET=environment)"
31
- task :migration => :rails_env do
32
- raise "missing migration name, use the option NAME=migration_file_name" unless ENV["NAME"]
33
26
 
34
- name = ENV["NAME"]
35
- outdir = ENV["OUTDIR"] || Rails.application.config.paths['db/migrate']
36
-
37
- generator = Arfy::Generator.new
38
- file = generator.generate_migration_file ENV["NAME"], outdir
27
+ desc "Change dir where arfy searches for db and config dir (options DBDIR=db, MIGRATIONDIR=DBDIR/migrate, CONFIGDIR=db/config, CONFIGNAME=database.yml, SEEDSDIR=DBDIR, SEEDSNAME=seeds.rb, SCHEMANAME=schema.rb)"
28
+ task :workdir => "arfy:workdir:default"
29
+
30
+ namespace :workdir do
31
+ task :default do
32
+ ENV["db"] = ENV["DBDIR"]
33
+ ENV["db/migrate"] = ENV["MIGRATIONDIR"]
34
+ ENV["db/config"] = ENV["CONFIGDIR"]
35
+ ENV["db/seeds_dir"] = ENV["SEEDSDIR"]
36
+ ENV["db/seeds.rb"] = ENV["SEEDSNAME"]
37
+ ENV["db/config/database.yml"] = ENV["CONFIGNAME"]
38
+ ENV["db/schema.rb"] = ENV["SCHEMANAME"]
39
+ arfy_env = Arfy::Environment.new
40
+ arfy_env.create_config(arfy_env.configured_paths)
41
+ end
39
42
  end
40
43
  end
41
44
 
42
45
  load 'active_record/railties/databases.rake'
46
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arfy
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-22 00:00:00.000000000Z
12
+ date: 2011-10-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70304467191940 !ruby/object:Gem::Requirement
16
+ requirement: &70252486829380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,27 +21,52 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70304467191940
24
+ version_requirements: *70252486829380
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70252486829000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70252486829000
25
36
  description: ! 'Arfy is just a snippet: some rake tasks and environment configuration
26
37
  to allow you use Rails migrations, without all the rails stack.'
27
38
  email:
28
39
  - ricardo@backslashes.net
29
- executables: []
40
+ executables:
41
+ - arfy
30
42
  extensions: []
31
43
  extra_rdoc_files: []
32
44
  files:
33
- - lib/arfy/application_faker.rb
34
- - lib/arfy/config_faker.rb
35
- - lib/arfy/environment_faker.rb
45
+ - lib/arfy/environment.rb
36
46
  - lib/arfy/generator.rb
37
- - lib/arfy/migration_template
38
- - lib/arfy/rails_faker.rb
47
+ - lib/arfy/migration_builder/builders/add_column.rb
48
+ - lib/arfy/migration_builder/builders/add_index.rb
49
+ - lib/arfy/migration_builder/builders/change_column.rb
50
+ - lib/arfy/migration_builder/builders/column.rb
51
+ - lib/arfy/migration_builder/builders/create_table.rb
52
+ - lib/arfy/migration_builder/builders/drop_table.rb
53
+ - lib/arfy/migration_builder/builders/remove_column.rb
54
+ - lib/arfy/migration_builder/builders/remove_index.rb
55
+ - lib/arfy/migration_builder/builders/rename_column.rb
56
+ - lib/arfy/migration_builder/builders/rename_table.rb
57
+ - lib/arfy/migration_builder/generic_migration.rb
58
+ - lib/arfy/migration_builder/template_handler.rb
59
+ - lib/arfy/migration_builder.rb
60
+ - lib/arfy/string_extensions.rb
39
61
  - lib/arfy/version.rb
40
62
  - lib/arfy.rb
63
+ - lib/arfy_tasks.rb
41
64
  - lib/tasks/all.rb
42
- - README.md
65
+ - Readme.md
66
+ - Readme.pt_br.md
43
67
  - LICENSE
44
68
  - Changelog.md
69
+ - bin/arfy
45
70
  homepage: http://github.com/ricardovaleriano/arfy
46
71
  licenses: []
47
72
  post_install_message:
data/README.md DELETED
@@ -1,64 +0,0 @@
1
- # Arfy
2
- ## Allow use ActiveRecord and Migration from Rails
3
- (even if you don't use all Rails stack)
4
-
5
- This is a very early implementation, so any comments are wellcome, but
6
- we are developing this on a daily basis. Huge changes in few days are
7
- expected.
8
-
9
- gem install arfy
10
-
11
- On your project's folder, put a Rakefile.rb with:
12
-
13
- require 'arfy'
14
-
15
- Check if you are ready to go:
16
-
17
- rake -T
18
-
19
- You should see some familiar tasks:
20
-
21
- rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
22
- rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
23
- rake db:fixtures:load # Load fixtures into the current environment's database.
24
- rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
25
- rake db:migrate:status # Display status of migrations
26
- rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
27
- rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
28
- rake db:schema:load # Load a schema.rb file into the database
29
- rake db:seed # Load the seed data from db/seeds.rb
30
- rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
31
- rake db:structure:dump # Dump the database structure to an SQL file
32
- rake db:version # Retrieves the current schema version number
33
- rake generate:migration # Generate migration (options NAME=migration_file_name, OUTDIR=db/migrate, TARGET=environment)
34
- rake rails_env # Changes the environment (options TARGET=development) - use your's configuration name on database.yml
35
-
36
- ---
37
-
38
- The goal here is to use the Migrations. You can use the Rails conventions and create two dirs on your project:
39
- your_project/
40
- db/migrate/
41
- config/
42
-
43
- To generate a skeleton for a migration, use the task:
44
-
45
- rake generate:migration NAME=my-MigrationName
46
-
47
- (The awkward name here is just to illustrate that the task will create a file with a pretty name for you, something like:
48
- 20110920000101_my_migration_name.rb)
49
-
50
- A new file, with the timestamp as a prefix on name, will be generated on
51
- db/migrate or on the path pointed by the OUTDIR option.
52
-
53
- Create your migration files, following the [Rails docs](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html).
54
-
55
- Inside config, create your database.yml, just like you can do with
56
- [Rails](http://guides.rubyonrails.org/getting_started.html). On
57
- db/migrate, put your migration files.
58
-
59
- Now you can do:
60
-
61
- rake db:create:all
62
- rake db:migrate
63
-
64
- Enjoy and, if you can, help me improve this tool.