arfy 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,9 +6,99 @@
6
6
  # rails/generators/active_modle: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/active_model.rb
7
7
 
8
8
  #require 'rails/generators/active_record'
9
+ #
10
+ #
11
+
12
+ # heavily inspired/copyied from: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/migration.rb
9
13
 
10
- require 'rubygems'
11
14
  require 'erb'
15
+
16
+ module StringPimped
17
+ #code from: http://sequel.rubyforge.org/rdoc-plugins/classes/String.html
18
+ def underscore
19
+ gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
20
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
21
+ end
22
+
23
+ def camelize(first_letter_in_uppercase =:upper)
24
+ s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.
25
+ gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
26
+ s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
27
+ s
28
+ end
29
+
30
+ def classify
31
+ str = sub(/.*\./, '')
32
+ class << str; include StringPimped; end
33
+ str.camelize
34
+ end
35
+
36
+ def respond_to? method
37
+ @i_know ||= [:underscore, :camelize, :classify]
38
+ if @i_know.include? method
39
+ return true
40
+ end
41
+ super
42
+ end
43
+ end
44
+
45
+ module Arfy
46
+ class Generator
47
+ def timestamp
48
+ Time.new.strftime("%Y%m%d%H%M%S")
49
+ end
50
+
51
+ def code_for_migration(migration_name)
52
+ migration_name = String.new(migration_name)
53
+ unless migration_name.respond_to? :classify
54
+ class << migration_name
55
+ include StringPimped
56
+ end
57
+ end
58
+ klass = migration_name.classify
59
+ b = binding
60
+ template.result b
61
+ end
62
+
63
+ def new_migration_file_name_for(migration_name)
64
+ migration_name = String.new(migration_name)
65
+ unless migration_name.respond_to? :underscore
66
+ class << migration_name
67
+ include StringPimped
68
+ end
69
+ end
70
+ migration_name = migration_name.underscore
71
+ "#{timestamp}_#{migration_name}.rb"
72
+ end
73
+
74
+ def read_template_file
75
+ unless @template_content
76
+ @template_content = ''
77
+ template_path = File.expand_path("../migration_template", __FILE__)
78
+ File.open(template_path, 'r') do |file|
79
+ while line = file.gets
80
+ @template_content << line
81
+ end
82
+ end
83
+ end
84
+ @template_content
85
+ end
86
+
87
+ def template
88
+ ERB.new read_template_file
89
+ end
90
+
91
+ def generate_migration_file(migration_name, dir)
92
+ migration_path = File.join(dir, new_migration_file_name_for(migration_name))
93
+ File.open(migration_path, "w") do |file|
94
+ file << code_for_migration(migration_name)
95
+ end
96
+ File.exists? migration_path
97
+ end
98
+ end
99
+ end
100
+
101
+ =begin
12
102
  klass = "teste"
13
103
  template = ERB.new <<EOF
14
104
  <%= "class #{klass} < ActiveRecord::Migration" %>
@@ -21,3 +111,4 @@ template = ERB.new <<EOF
21
111
  EOF
22
112
 
23
113
  puts template.result
114
+ =end
@@ -0,0 +1,7 @@
1
+ <%= "class #{klass} < ActiveRecord::Migration" %>
2
+ <%= " def self.up" %>
3
+ <%= " end" %>
4
+ <%= "" %>
5
+ <%= " def self.down" %>
6
+ <%= " end" %>
7
+ <%= "end" %>
data/lib/arfy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arfy
2
- VERSION = "0.1.1" unless defined?(::Arfy::VERSION)
2
+ VERSION = "0.1.3" unless defined?(::Arfy::VERSION)
3
3
  end
data/lib/arfy.rb CHANGED
@@ -5,6 +5,7 @@ require 'arfy/config_faker'
5
5
  require 'arfy/application_faker'
6
6
  require 'arfy/environment_faker'
7
7
  require 'arfy/rails_faker'
8
+ require 'arfy/generator'
8
9
 
9
10
  def configured_paths
10
11
  paths = {}
data/lib/tasks/all.rb CHANGED
@@ -18,8 +18,14 @@ end
18
18
  namespace :generate do
19
19
  desc "Generate migration (options NAME=migration_file_name, OUTDIR=db/migrate, TARGET=environment)"
20
20
  task :migration do
21
- ENV['RAILS_ENV'] = ENV["TARGET"] || (ENV["RAILS_ENV"] || "development")
22
- dir = ENV['OUTDIR'] || Rails.application.config.paths['db/migrate']
21
+ raise "missing migration name, use the option NAME=migration_file_name" unless ENV["NAME"]
22
+
23
+ name = ENV["NAME"]
24
+ outdir = ENV["OUTDIR"] || Rails.application.config.paths['db/migrate']
25
+ ENV["RAILS_ENV"] = ENV["TARGET"] || ENV["RAILS_ENV"]
26
+
27
+ generator = Arfy::Generator.new
28
+ generator.generate_migration_file ENV["NAME"], outdir
23
29
 
24
30
  puts 'not yet implemented'
25
31
  end
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.1.1
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70171151577780 !ruby/object:Gem::Requirement
16
+ requirement: &70118870495940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70171151577780
24
+ version_requirements: *70118870495940
25
25
  description: ! 'Arfy is just a snippet: some rake tasks and environment configuration
26
26
  to allow you use Rails migrations, without all the rails stack.'
27
27
  email:
@@ -34,6 +34,7 @@ files:
34
34
  - lib/arfy/config_faker.rb
35
35
  - lib/arfy/environment_faker.rb
36
36
  - lib/arfy/generator.rb
37
+ - lib/arfy/migration_template
37
38
  - lib/arfy/rails_faker.rb
38
39
  - lib/arfy/version.rb
39
40
  - lib/arfy.rb