backflip 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/backflip/version.rb +3 -0
  2. data/lib/backflip.rb +81 -0
  3. metadata +47 -0
@@ -0,0 +1,3 @@
1
+ module Backflip
2
+ VERSION = "0.1.1"
3
+ end
data/lib/backflip.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'thor'
2
+ require 'active_support/inflector'
3
+
4
+ # insane, i know, but we do this to avoid arel chains (at el.) evaluated at load time from throwing NoMethodError exceptions.
5
+ class NilClass
6
+ # this helps with things like 'has_friendly_id', etc. etc.
7
+ def self.method_missing(meth, *args, &block)
8
+ end
9
+ def method_missing(meth, *args, &block)
10
+ end
11
+ end
12
+
13
+ module ActiveRecord
14
+ class Base
15
+
16
+ module FriendlyId
17
+ end
18
+
19
+ @@attributes = []
20
+ @@supporting_attributes = []
21
+ @@migrations = []
22
+
23
+ def self.belongs_to(model, *args)
24
+ @@attributes << "#{model}_id:integer"
25
+ end
26
+
27
+ def self.has_many(model, *args)
28
+ @@migrations << "rails g migration add_#{self.name.underscore}_to_#{model} #{self.name.underscore}_id:integer"
29
+ end
30
+
31
+ def self.has_one(model, *args)
32
+ @@migrations << "rails g migration add_#{self.name.underscore}_to_#{model} #{self.name.underscore}_id:integer"
33
+ end
34
+
35
+ def self.has_attached_file(file, *args)
36
+ @@supporting_attributes << "#{file}_file_name:string"
37
+ @@supporting_attributes << "#{file}_file_size:integer"
38
+ @@supporting_attributes << "#{file}_file_content_type:string"
39
+ @@supporting_attributes << "#{file}_file_updated_at:datetime"
40
+ end
41
+
42
+ def self.friendly_id(name, parameters = {}, *args)
43
+ if parameters.has_key?(:use)
44
+ @@supporting_attributes << "#{parameters[:use].to_s}:string"
45
+ end
46
+ end
47
+
48
+ # this helps with things like 'has_friendly_id', etc. etc.
49
+ def self.method_missing(meth, *args, &block)
50
+ end
51
+ def method_missing(meth, *args, &block)
52
+ end
53
+
54
+ def self.generate_migrations
55
+ puts "rails g migration create_#{self.name.underscore} #{@@attributes.join(' ')} #{@@supporting_attributes.join(' ')}"
56
+ @@migrations.each do |migration|
57
+ puts migration
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ module Backflip
64
+
65
+ class CLI < Thor
66
+ map "g" => :generate
67
+
68
+ desc "generate FILE", "generate migrations required for a given model file"
69
+ def generate(filename)
70
+ begin
71
+ require filename
72
+ filename.split(/\//).last.gsub(/\.rb/, '').classify.constantize.generate_migrations
73
+ rescue NameError
74
+ $stderr.print "Ah, there is a dependancy in this model that Backflip won't try to load.\nTry generating the migration before adding in all the goodies.\n\nHere's a hint for what the problem is:\n\n"
75
+ raise
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backflip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Culver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Migrations are awesome, but I'm still tired of generating so many of
15
+ them.
16
+ email: andrew.culver@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/backflip.rb
22
+ - lib/backflip/version.rb
23
+ homepage: https://github.com/wearetitans/backflip
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project: ! '[none]'
43
+ rubygems_version: 1.8.15
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Generate migrations from your models.
47
+ test_files: []