conventional_models 0.1.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source :gemcutter
2
+
3
+ group :features do
4
+ gem "cucumber", "0.6.4"
5
+ gem "aruba", "0.1.7"
6
+ end
7
+
8
+ group :specs do
9
+ gem "rspec", "1.3.0"
10
+ end
11
+
12
+ group :development do
13
+ gem "jeweler"
14
+ gem "rake"
15
+ end
16
+
17
+ gem "activerecord", "2.3.5"
18
+ gem "sqlite3-ruby"
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ ---
2
+ dependencies:
3
+ activerecord:
4
+ group:
5
+ - :default
6
+ version: = 2.3.5
7
+ sqlite3-ruby:
8
+ group:
9
+ - :default
10
+ version: ">= 0"
11
+ rake:
12
+ group:
13
+ - :development
14
+ version: ">= 0"
15
+ rspec:
16
+ group:
17
+ - :specs
18
+ version: = 1.3.0
19
+ aruba:
20
+ group:
21
+ - :features
22
+ version: = 0.1.7
23
+ jeweler:
24
+ group:
25
+ - :development
26
+ version: ">= 0"
27
+ cucumber:
28
+ group:
29
+ - :features
30
+ version: = 0.6.4
31
+ specs:
32
+ - rake:
33
+ version: 0.8.7
34
+ - activesupport:
35
+ version: 2.3.5
36
+ - activerecord:
37
+ version: 2.3.5
38
+ - aruba:
39
+ version: 0.1.7
40
+ - builder:
41
+ version: 2.1.2
42
+ - diff-lcs:
43
+ version: 1.1.2
44
+ - json_pure:
45
+ version: 1.2.4
46
+ - polyglot:
47
+ version: 0.3.1
48
+ - term-ansicolor:
49
+ version: 1.0.5
50
+ - treetop:
51
+ version: 1.4.5
52
+ - cucumber:
53
+ version: 0.6.4
54
+ - gemcutter:
55
+ version: 0.5.0
56
+ - git:
57
+ version: 1.2.5
58
+ - rubyforge:
59
+ version: 2.0.4
60
+ - jeweler:
61
+ version: 1.4.0
62
+ - rspec:
63
+ version: 1.3.0
64
+ - sqlite3-ruby:
65
+ version: 1.2.5
66
+ hash: e54d068ab5b54822142a3392faafd4850696a431
67
+ sources:
68
+ - Rubygems:
69
+ uri: http://gemcutter.org
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steve Hodgkiss
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = ConventionalModels
2
+
3
+ Generate ActiveRecord models with basic relationships based on conventions.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Steve Hodgkiss. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,85 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ require 'rake'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "conventional_models"
10
+ gem.description = %Q{Generate ActiveRecord models automatically with basic relationships based on conventions.}
11
+ gem.summary = %Q{Generate ActiveRecord models. For lazy people.}
12
+ gem.email = "steve@hodgkiss.me.uk"
13
+ gem.homepage = "http://github.com/stevehodgkiss/conventional_models"
14
+ gem.authors = ["Steve Hodgkiss"]
15
+ gem.add_development_dependency "rspec", ">= 1.3.0"
16
+ gem.add_development_dependency "cucumber", ">= 0.6.4"
17
+ gem.add_development_dependency "aruba", ">= 0.1.7"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ begin
40
+ require 'cucumber/rake/task'
41
+ Cucumber::Rake::Task.new(:features)
42
+
43
+ task :features => :check_dependencies
44
+ rescue LoadError
45
+ task :features do
46
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
47
+ end
48
+ end
49
+
50
+ begin
51
+ require 'reek/adapters/rake_task'
52
+ Reek::RakeTask.new do |t|
53
+ t.fail_on_error = true
54
+ t.verbose = false
55
+ t.source_files = 'lib/**/*.rb'
56
+ end
57
+ rescue LoadError
58
+ task :reek do
59
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
60
+ end
61
+ end
62
+
63
+ begin
64
+ require 'roodi'
65
+ require 'roodi_task'
66
+ RoodiTask.new do |t|
67
+ t.verbose = false
68
+ end
69
+ rescue LoadError
70
+ task :roodi do
71
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
72
+ end
73
+ end
74
+
75
+ task :default => :spec
76
+
77
+ require 'rake/rdoctask'
78
+ Rake::RDocTask.new do |rdoc|
79
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
80
+
81
+ rdoc.rdoc_dir = 'rdoc'
82
+ rdoc.title = "conventional_models #{version}"
83
+ rdoc.rdoc_files.include('README*')
84
+ rdoc.rdoc_files.include('lib/**/*.rb')
85
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,80 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{conventional_models}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Steve Hodgkiss"]
12
+ s.date = %q{2010-04-10}
13
+ s.description = %q{Generate ActiveRecord models automatically with basic relationships based on conventions.}
14
+ s.email = %q{steve@hodgkiss.me.uk}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "conventional_models.gemspec",
29
+ "features/conventional_models.feature",
30
+ "features/output_model_code.feature",
31
+ "features/step_definitions/conventional_models_steps.rb",
32
+ "features/step_definitions/output_model_code_steps.rb",
33
+ "features/support/env.rb",
34
+ "lib/conventional_models.rb",
35
+ "lib/conventional_models/active_record_base_model_for.rb",
36
+ "lib/conventional_models/column.rb",
37
+ "lib/conventional_models/conventions.rb",
38
+ "lib/conventional_models/database.rb",
39
+ "lib/conventional_models/table.rb",
40
+ "lib/conventional_models/version.rb",
41
+ "spec/conventional_models/conventions_spec.rb",
42
+ "spec/conventional_models/database_spec.rb",
43
+ "spec/conventional_models/table_spec.rb",
44
+ "spec/conventional_models_spec.rb",
45
+ "spec/spec.opts",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+ s.homepage = %q{http://github.com/stevehodgkiss/conventional_models}
49
+ s.rdoc_options = ["--charset=UTF-8"]
50
+ s.require_paths = ["lib"]
51
+ s.rubygems_version = %q{1.3.6}
52
+ s.summary = %q{Generate ActiveRecord models. For lazy people.}
53
+ s.test_files = [
54
+ "spec/conventional_models/conventions_spec.rb",
55
+ "spec/conventional_models/database_spec.rb",
56
+ "spec/conventional_models/table_spec.rb",
57
+ "spec/conventional_models_spec.rb",
58
+ "spec/spec_helper.rb"
59
+ ]
60
+
61
+ if s.respond_to? :specification_version then
62
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
+ s.specification_version = 3
64
+
65
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
66
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
67
+ s.add_development_dependency(%q<cucumber>, [">= 0.6.4"])
68
+ s.add_development_dependency(%q<aruba>, [">= 0.1.7"])
69
+ else
70
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
71
+ s.add_dependency(%q<cucumber>, [">= 0.6.4"])
72
+ s.add_dependency(%q<aruba>, [">= 0.1.7"])
73
+ end
74
+ else
75
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
76
+ s.add_dependency(%q<cucumber>, [">= 0.6.4"])
77
+ s.add_dependency(%q<aruba>, [">= 0.1.7"])
78
+ end
79
+ end
80
+
@@ -0,0 +1,80 @@
1
+ Feature: ConventionalModels
2
+ In order play with my database
3
+ As a lazy fuck
4
+ I want conventional ActiveRecord models for my database
5
+
6
+ Scenario Outline: One table, no rows
7
+ Given a table "<table_name>"
8
+ And a file named "my_script.rb" with:
9
+ """
10
+ $:.unshift("../../lib")
11
+ require 'rubygems'
12
+ require 'active_record'
13
+ ActiveRecord::Base.establish_connection(:database => '../test.sqlite', :adapter => 'sqlite3')
14
+ require 'conventional_models'
15
+ ConventionalModels.configure
16
+ puts "Number of records: #{<model_name>.count}"
17
+ """
18
+ When I run "ruby my_script.rb"
19
+ Then I should see "Number of records: 0"
20
+
21
+ Examples:
22
+ | table_name | model_name |
23
+ | pages | Page |
24
+ | content_items | ContentItem |
25
+
26
+ Scenario Outline: One table, no rows, legacy database
27
+ Given a table "<table_name>"
28
+ And a file named "my_script.rb" with:
29
+ """
30
+ $:.unshift("../../lib")
31
+ require 'rubygems'
32
+ require 'active_record'
33
+ ActiveRecord::Base.establish_connection(:database => '../test.sqlite', :adapter => 'sqlite3')
34
+ require 'conventional_models'
35
+ ConventionalModels.configure do
36
+ table_name do |table|
37
+ table
38
+ end
39
+ end
40
+ puts "Number of records: #{<model_name>.count}"
41
+ """
42
+ When I run "ruby my_script.rb"
43
+ Then I should see "Number of records: 0"
44
+
45
+ Examples:
46
+ | table_name | model_name |
47
+ | Page | Page |
48
+ | ContentItem | ContentItem |
49
+
50
+ Scenario: Two tables with belongs_to and has_many relationship
51
+ Given a table "Page" with the following columns
52
+ | name | type |
53
+ | Id | integer |
54
+ | Name | string |
55
+ And a table "ContentItems" with the following columns
56
+ | name | type |
57
+ | Id | INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL |
58
+ | Name | string |
59
+ | Page_id | integer |
60
+ And a file named "my_script.rb" with:
61
+ """
62
+ $:.unshift("../../lib")
63
+ require 'rubygems'
64
+ require 'active_record'
65
+ ActiveRecord::Base.establish_connection(:database => '../test.sqlite', :adapter => 'sqlite3')
66
+ require 'conventional_models'
67
+ ConventionalModels.configure do
68
+ primary_key_name "Id"
69
+ table_name do |table|
70
+ table
71
+ end
72
+ end
73
+ page = Page.create :Name => 'test'
74
+ page.content_items.create :Name => 'content1'
75
+ puts "My page's content item name: #{page.content_items.first.Name}"
76
+ """
77
+ When I run "ruby my_script.rb"
78
+ Then I should see "My page's content item name: content1"
79
+
80
+
@@ -0,0 +1,42 @@
1
+ Feature: Output model code
2
+ In order to see what model code has been generated
3
+ As a lazy fuck
4
+ I want to be able to output the evaluated model code
5
+
6
+ Scenario: A page with many content items
7
+ Given a table "Page" with the following columns
8
+ | name | type |
9
+ | Id | integer |
10
+ | Name | string |
11
+ And a table "ContentItem" with the following columns
12
+ | name | type |
13
+ | Id | INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL |
14
+ | Name | string |
15
+ | Page_id | integer |
16
+ And a file named "my_script.rb" with:
17
+ """
18
+ $:.unshift("../../lib")
19
+ require 'rubygems'
20
+ require 'active_record'
21
+ ActiveRecord::Base.establish_connection(:database => '../test.sqlite', :adapter => 'sqlite3')
22
+ require 'conventional_models'
23
+ ConventionalModels.configure do
24
+ primary_key_name "Id"
25
+ table_name do |table|
26
+ table
27
+ end
28
+ end
29
+ puts "ContentItem code:<<\n#{ContentItem.model_code}>>"
30
+ puts "Page code:<<\n#{Page.model_code}>>"
31
+ """
32
+ When I run "ruby my_script.rb"
33
+ And within "ContentItem code" I should see:
34
+ | line |
35
+ | set_table_name "ContentItem" |
36
+ | set_primary_key "Id" |
37
+ | belongs_to :page |
38
+ Then within "Page code" I should see:
39
+ | line |
40
+ | set_table_name "Page" |
41
+ | set_primary_key "Id" |
42
+ | has_many :content_items |
@@ -0,0 +1,12 @@
1
+ Given /^a table "([^\"]*)"$/ do |name|
2
+ ActiveRecord::Base.connection.execute "CREATE TABLE \"#{name}\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)"
3
+ end
4
+
5
+ Given /^a table "([^\"]*)" with the following columns$/ do |name, table|
6
+ columns = []
7
+ table.hashes.each do |column|
8
+ columns << "#{column["name"]} #{column["type"]}"
9
+ end
10
+ sql = "CREATE TABLE \"#{name}\" (#{columns.join(", ")})"
11
+ ActiveRecord::Base.connection.execute sql
12
+ end
@@ -0,0 +1,7 @@
1
+ Then /^within "([^\"]*)" I should see:$/ do |within, table|
2
+ puts combined_output
3
+ output = combined_output.scan(/#{within}:\<\<(.*)\>\>/m).to_s
4
+ table.hashes.each do |row|
5
+ output.should =~ /#{row["line"]}/
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'conventional_models'
3
+
4
+ require 'spec/expectations'
5
+
6
+ require 'aruba'
7
+ require 'active_record'
8
+
9
+ Before do
10
+ config = {
11
+ :development => {
12
+ :database => 'tmp/test.sqlite',
13
+ :adapter => 'sqlite3'
14
+ }
15
+ }
16
+
17
+ ActiveRecord::Base.configurations = config
18
+
19
+ system "mkdir -p tmp"
20
+ system 'rm -f tmp/test.sqlite'
21
+ ActiveRecord::Base.establish_connection(config[:development])
22
+ end
@@ -0,0 +1,5 @@
1
+ class ActiveRecord::Base
2
+ def self.model_code
3
+ ::ConventionalModels.model_code_for(table_name)
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record/connection_adapters/abstract/schema_definitions'
2
+
3
+ module ConventionalModels
4
+ class Column < ActiveRecord::ConnectionAdapters::Column
5
+
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ module ConventionalModels
2
+ class Conventions
3
+ def initialize(&block)
4
+ builder = Builder.new(self)
5
+ builder.instance_eval(&block) if block
6
+ end
7
+
8
+ attr_accessor :ignored_tables, :primary_key_name, :class_name,
9
+ :table_name, :belongs_to_matcher, :belongs_to_name
10
+
11
+ class Builder
12
+ def initialize(config)
13
+ @config = config
14
+ belongs_to_matcher {|column| column.name.end_with? "_id"}
15
+ belongs_to_name {|column| column.name.gsub(/_id$/, "")}
16
+ primary_key_name "id"
17
+ table_name {|table_name| table_name.pluralize}
18
+ class_name {|table_name| table_name.singularize.camelize}
19
+ ignore_tables "schema_migrations", "sqlite_sequence", "sysdiagrams"
20
+ end
21
+
22
+ def ignore_tables(*tables)
23
+ @config.ignored_tables = tables.map{|t|t.to_s}
24
+ end
25
+
26
+ def primary_key_name(name)
27
+ @config.primary_key_name = name
28
+ end
29
+
30
+ def table_name(&block)
31
+ @config.table_name = block
32
+ end
33
+
34
+ def belongs_to_matcher(&block)
35
+ @config.belongs_to_matcher = block
36
+ end
37
+
38
+ def belongs_to_name(&block)
39
+ @config.belongs_to_name = block
40
+ end
41
+
42
+ def class_name(&block)
43
+ @config.class_name = block
44
+ end
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,44 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module ConventionalModels
4
+ class Database
5
+ attr_accessor :tables
6
+
7
+ def initialize(connection)
8
+ @connection = connection
9
+ @tables = []
10
+ end
11
+
12
+ def apply_conventions(conventions)
13
+ @connection.tables.select{|table| !conventions.ignored_tables.include? table}.each do |table|
14
+ @tables << Table.new(table, @connection.columns(table))
15
+ end
16
+
17
+ @tables.each{|table| table.apply_conventions(conventions)}
18
+
19
+ @tables.each do |table|
20
+ table.belongs_to_names.each do |belongs_to|
21
+ name = conventions.belongs_to_name.call(belongs_to)
22
+ table_name = conventions.table_name.call(name)
23
+ has_many_table = @tables.select{|t| t.name == table_name}.first
24
+ if has_many_table
25
+ has_many_table.lines << "has_many :#{table.name.tableize}, :class_name => '#{table.class_name}', :primary_key => '#{conventions.primary_key_name}', :foreign_key => '#{belongs_to.name}'"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def code
32
+ @tables.map{|t| t.code}.join("\n")
33
+ end
34
+
35
+ def code_for(table_name)
36
+ table = @tables.select{|t| t.name == table_name}.first
37
+ if table
38
+ table.code
39
+ else
40
+ "#{table_name} not found"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,37 @@
1
+ module ConventionalModels
2
+ class Table
3
+ attr_accessor :name, :columns, :lines, :belongs_to_names, :class_name
4
+
5
+ def initialize(name, columns)
6
+ @name = name
7
+ @columns = columns
8
+ @lines = []
9
+ @belongs_to_names = []
10
+ end
11
+
12
+ def ==(other)
13
+ @name == other.name
14
+ end
15
+
16
+ def apply_conventions(conventions)
17
+ @class_name = conventions.class_name.call(@name)
18
+
19
+ @lines << "set_primary_key \"#{conventions.primary_key_name}\""
20
+
21
+ table_name = conventions.table_name.call(name)
22
+ @lines << "set_table_name \"#{table_name}\""
23
+
24
+ @columns.each do |column|
25
+ if conventions.belongs_to_matcher.call(column)
26
+ name = conventions.belongs_to_name.call(column)
27
+ @belongs_to_names << column
28
+ @lines << "belongs_to :#{name.underscore}, :class_name => '#{conventions.class_name.call(name)}'"
29
+ end
30
+ end
31
+ end
32
+
33
+ def code
34
+ "class ::#{@name.singularize.camelize} < ::ActiveRecord::Base\n#{@lines.join("\n")}\nend"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module ConventionalModels #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ BUILD = 1
6
+
7
+ STRING = [MAJOR, MINOR, BUILD].join('.')
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'active_support'
4
+ require 'conventional_models/version'
5
+ require 'conventional_models/conventions'
6
+ require 'conventional_models/database'
7
+ require 'conventional_models/table'
8
+ require 'conventional_models/column'
9
+ require 'conventional_models/active_record_base_model_for'
10
+
11
+ module ConventionalModels
12
+ def self.configure(&block)
13
+ @@conventions = Conventions.new(&block)
14
+ @@database = Database.new(::ActiveRecord::Base.connection)
15
+ @@database.apply_conventions(@@conventions)
16
+ run_code @@database.code
17
+ end
18
+
19
+ def self.run_code(code)
20
+ eval code
21
+ end
22
+
23
+ def self.model_code
24
+ @@database.code
25
+ end
26
+
27
+ def self.model_code_for(table)
28
+ @@database.code_for(table)
29
+ end
30
+ end
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module ConventionalModels
4
+ describe Conventions do
5
+ it "should set ignored tables" do
6
+ config = Conventions.new do
7
+ ignore_tables "slc", "abc"
8
+ end
9
+ config.ignored_tables.should == ["slc", "abc"]
10
+ end
11
+
12
+ it "should set primary key name" do
13
+ config = Conventions.new do
14
+ primary_key_name "Id"
15
+ end
16
+ config.primary_key_name.should == "Id"
17
+ end
18
+
19
+ it "should set table name" do
20
+ config = Conventions.new do
21
+ table_name do |table_name|
22
+ table_name
23
+ end
24
+ end
25
+ config.table_name.call("test").should == "test"
26
+ end
27
+
28
+ it "should set belongs to matcher" do
29
+ config = Conventions.new do
30
+ belongs_to_matcher do |column|
31
+ true
32
+ end
33
+ end
34
+ config.belongs_to_matcher.call("test_id").should be_true
35
+ end
36
+
37
+ it "should set belongs to name" do
38
+ config = Conventions.new do
39
+ belongs_to_name do |column|
40
+ column
41
+ end
42
+ end
43
+ config.belongs_to_name.call("test").should == "test"
44
+ end
45
+
46
+ it "should set class name" do
47
+ config = Conventions.new do
48
+ class_name do |column|
49
+ "test"
50
+ end
51
+ end
52
+ config.class_name.call("").should == "test"
53
+ end
54
+
55
+ it "should have default settings" do
56
+ @site_id_column = Column.new("site_id", nil, "integer")
57
+
58
+ config = Conventions.new
59
+ config.belongs_to_name.call(@site_id_column).should == "site"
60
+ config.belongs_to_matcher.call(@site_id_column).should be_true
61
+ config.primary_key_name.should == "id"
62
+ config.table_name.call("pages").should == "pages"
63
+ config.class_name.call("pages").should == "Page"
64
+ config.ignored_tables.should == %w{schema_migrations sqlite_sequence sysdiagrams}
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module ConventionalModels
4
+ describe Database do
5
+ before(:each) do
6
+ @conventions = mock(Conventions)
7
+ @conventions.stub(:ignored_tables).and_return([])
8
+
9
+ @connection = mock(::ActiveRecord::ConnectionAdapters::AbstractAdapter)
10
+ @columns = [mock(Column)]
11
+
12
+ @connection.stub(:tables).and_return(["Test"])
13
+ @connection.stub(:columns).with("Test").and_return(@columns)
14
+
15
+ @table = mock(Table)
16
+ @table.stub(:apply_conventions)
17
+ @table.stub(:belongs_to_names).and_return([])
18
+ @table.stub(:name).and_return("Test")
19
+ Table.stub(:new => @table)
20
+ end
21
+
22
+ describe "#apply_conventions" do
23
+ before(:each) do
24
+ @database = Database.new(@connection)
25
+ end
26
+
27
+ it "creates a table with the table name and the column definitions" do
28
+ Table.should_receive(:new).with("Test", @columns).and_return(@table)
29
+ @database.apply_conventions(@conventions)
30
+ @database.tables.first.should == @table
31
+ end
32
+
33
+ it "applies conventions to each table" do
34
+ @table.should_receive(:apply_conventions).with(@conventions)
35
+ @database.apply_conventions(@conventions)
36
+ end
37
+
38
+ describe "has many relationships" do
39
+ before(:each) do
40
+ @connection.stub(:tables).and_return(["sites", "pages"])
41
+
42
+ Table.unstub!(:new)
43
+ @site_columns = [Column.new("name", nil, "string")]
44
+ @pages_columns = [Column.new("site_id", nil, "integer")]
45
+
46
+ @connection.stub(:columns).with("sites").and_return(@site_columns)
47
+ @connection.stub(:columns).with("pages").and_return(@pages_columns)
48
+ end
49
+
50
+ it "sets site to have many pages" do
51
+ @database.apply_conventions(Conventions.new)
52
+ @database.tables.first.name.should == "sites"
53
+ @database.tables.first.lines.last.should == "has_many :pages, :class_name => 'Page', :primary_key => 'id', :foreign_key => 'site_id'"
54
+ end
55
+ end
56
+
57
+ it "ignores tables" do
58
+ @conventions = Conventions.new do
59
+ ignore_tables "Test"
60
+ end
61
+ @table.should_not_receive(:apply_conventions)
62
+ @database.apply_conventions(@conventions)
63
+ end
64
+ end
65
+
66
+ describe "code outputting" do
67
+ before(:each) do
68
+ @table.stub(:name).and_return("Test")
69
+ @database = Database.new(@connection)
70
+ @database.apply_conventions(@conventions)
71
+ end
72
+ describe "#code" do
73
+ it "should return the code for each table" do
74
+ @table.should_receive(:code).and_return("test")
75
+ @database.code.should == "test"
76
+ end
77
+ end
78
+
79
+ describe "#code_for" do
80
+ it "should return the model code for a specific table" do
81
+ @table.should_receive(:code).and_return("test")
82
+ @database.code_for("Test").should == "test"
83
+ end
84
+ it "should return not found for unknown tables" do
85
+ @database.code_for("SomeTable").should == "SomeTable not found"
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,89 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module ConventionalModels
4
+ describe Table do
5
+ before(:each) do
6
+ @columns = [mock(Column, :name => "test")]
7
+ end
8
+
9
+ describe ".new" do
10
+ it "sets the name" do
11
+ Table.new("test", @columns).name.should == "test"
12
+ end
13
+
14
+ it "sets the columns" do
15
+ Table.new("test", @columns).columns.should == @columns
16
+ end
17
+ end
18
+
19
+ describe "#==" do
20
+ it "is true for tables with the same name" do
21
+ Table.new("Page", @columns).should == Table.new("Page", @columns)
22
+ end
23
+
24
+ it "is false for tables with different names" do
25
+ Table.new("Page", @columns).should_not == Table.new("Bar", @columns)
26
+ end
27
+ end
28
+
29
+ describe "#apply_conventions" do
30
+ it "sets the primary key" do
31
+ @conventions = Conventions.new do
32
+ primary_key_name "ID"
33
+ end
34
+ @table = Table.new("Page", @columns)
35
+ @table.apply_conventions(@conventions)
36
+ @table.lines[0].should == "set_primary_key \"ID\""
37
+ end
38
+
39
+ it "sets the table name" do
40
+ @conventions = Conventions.new do
41
+ table_name do |table|
42
+ "Test"
43
+ end
44
+ end
45
+ @table = Table.new("Page", @columns)
46
+ @table.apply_conventions(@conventions)
47
+ @table.lines[1].should == "set_table_name \"Test\""
48
+ end
49
+
50
+ it "sets the class name" do
51
+ @conventions = Conventions.new do
52
+ class_name do |table|
53
+ "BOO"
54
+ end
55
+ end
56
+ @table = Table.new("Page", @columns)
57
+ @table.apply_conventions(@conventions)
58
+ @table.class_name.should == "BOO"
59
+ end
60
+
61
+ it "sets belongs to columns" do
62
+ @conventions = Conventions.new
63
+ @columns = [Column.new("Site_id", nil, "integer")]
64
+ @table = Table.new("Page", @columns)
65
+ @table.apply_conventions(@conventions)
66
+ @table.lines[2].should == "belongs_to :site, :class_name => 'Site'"
67
+ @table.belongs_to_names.first.name.should == "Site_id"
68
+ end
69
+
70
+ end
71
+
72
+ describe "#code" do
73
+ before(:each) do
74
+ @table = Table.new("pages", @columns)
75
+ end
76
+
77
+ it "returns an empty activerecord class with no columns" do
78
+ @model_code = @table.code
79
+ @model_code.should == %Q{class ::Page < ::ActiveRecord::Base\n\nend}
80
+ end
81
+
82
+ it "returns lines in the model definition" do
83
+ @table.lines << "test"
84
+ @model_code = @table.code
85
+ @model_code.split("\n")[1].should == "test"
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module ConventionalModels
4
+ describe ConventionalModels do
5
+ before(:each) do
6
+ @conventions = mock(Conventions)
7
+ Conventions.stub(:new => @conventions)
8
+
9
+ ConventionalModels.stub(:run_code)
10
+
11
+ @connection = mock(Column)
12
+ ::ActiveRecord::Base.stub(:connection).and_return(@connection)
13
+
14
+ @generated_code = mock(String)
15
+ @database = mock(Database, :code => @generated_code)
16
+ @database.stub(:apply_conventions).with(@conventions)
17
+ Database.stub(:new => @database)
18
+ end
19
+
20
+ describe ".configure" do
21
+ describe "with no args" do
22
+ it "creates a database object with the connection and conventions" do
23
+ Database.should_receive(:new).with(@connection).and_return(@database)
24
+ ConventionalModels.configure
25
+ end
26
+
27
+ it "called apply_conventions on the database object" do
28
+ @database.should_receive(:apply_conventions).with(@conventions)
29
+ ConventionalModels.configure
30
+ end
31
+
32
+ it "run the generated code" do
33
+ ConventionalModels.should_receive(:run_code).with(@generated_code)
34
+ ConventionalModels.configure
35
+ end
36
+ end
37
+ end
38
+
39
+ describe ".run_code" do
40
+ it "evaluates the code (probably using Kernel#eval...)" do
41
+ ConventionalModels.unstub!(:run_code)
42
+ ConventionalModels.run_code("1 + 1").should == 2
43
+ ConventionalModels.run_code("'banana'.reverse").should == "ananab"
44
+ end
45
+ end
46
+
47
+ describe ".model_code" do
48
+ it "returns the database code" do
49
+ ConventionalModels.configure
50
+ ConventionalModels.model_code.should == @generated_code
51
+ end
52
+ end
53
+
54
+ describe ".model_code_for" do
55
+ it "returns the model code for a specific model" do
56
+ @database.should_receive(:code_for).with("Test").and_return("test")
57
+ ConventionalModels.configure
58
+ ConventionalModels.model_code_for("Test").should == "test"
59
+ end
60
+ end
61
+ end
62
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup(:specs)
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'conventional_models'
8
+ require 'spec'
9
+ require 'spec/autorun'
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conventional_models
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Steve Hodgkiss
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-10 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ type: :development
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ prerelease: false
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: cucumber
36
+ type: :development
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 6
44
+ - 4
45
+ version: 0.6.4
46
+ prerelease: false
47
+ requirement: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: aruba
50
+ type: :development
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ - 1
58
+ - 7
59
+ version: 0.1.7
60
+ prerelease: false
61
+ requirement: *id003
62
+ description: Generate ActiveRecord models automatically with basic relationships based on conventions.
63
+ email: steve@hodgkiss.me.uk
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files:
69
+ - LICENSE
70
+ - README.rdoc
71
+ files:
72
+ - .document
73
+ - .gitignore
74
+ - Gemfile
75
+ - Gemfile.lock
76
+ - LICENSE
77
+ - README.rdoc
78
+ - Rakefile
79
+ - VERSION
80
+ - conventional_models.gemspec
81
+ - features/conventional_models.feature
82
+ - features/output_model_code.feature
83
+ - features/step_definitions/conventional_models_steps.rb
84
+ - features/step_definitions/output_model_code_steps.rb
85
+ - features/support/env.rb
86
+ - lib/conventional_models.rb
87
+ - lib/conventional_models/active_record_base_model_for.rb
88
+ - lib/conventional_models/column.rb
89
+ - lib/conventional_models/conventions.rb
90
+ - lib/conventional_models/database.rb
91
+ - lib/conventional_models/table.rb
92
+ - lib/conventional_models/version.rb
93
+ - spec/conventional_models/conventions_spec.rb
94
+ - spec/conventional_models/database_spec.rb
95
+ - spec/conventional_models/table_spec.rb
96
+ - spec/conventional_models_spec.rb
97
+ - spec/spec.opts
98
+ - spec/spec_helper.rb
99
+ has_rdoc: true
100
+ homepage: http://github.com/stevehodgkiss/conventional_models
101
+ licenses: []
102
+
103
+ post_install_message:
104
+ rdoc_options:
105
+ - --charset=UTF-8
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project:
125
+ rubygems_version: 1.3.6
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Generate ActiveRecord models. For lazy people.
129
+ test_files:
130
+ - spec/conventional_models/conventions_spec.rb
131
+ - spec/conventional_models/database_spec.rb
132
+ - spec/conventional_models/table_spec.rb
133
+ - spec/conventional_models_spec.rb
134
+ - spec/spec_helper.rb