shipyard 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1 @@
1
+ Shipyard
data/bin/shipyard ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib/')
4
+ require 'shipyard'
5
+
6
+ builder = Shipyard::Builder.new('Manifest')
7
+ builder.generate
data/lib/shipyard.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'active_support/inflector'
3
+ require 'shipyard/builder.rb'
4
+ require 'shipyard/context.rb'
5
+ require 'shipyard/manifest.rb'
6
+
@@ -0,0 +1,32 @@
1
+ require 'sequel'
2
+ require 'ftools'
3
+ require 'erb'
4
+
5
+ module Shipyard
6
+ class Builder
7
+ attr_reader :table
8
+ attr_reader :db
9
+
10
+ def initialize(manifest_file)
11
+ @manifest = Manifest.new(manifest_file)
12
+ @context = Context.new(@manifest)
13
+ end
14
+
15
+ def generate
16
+ Dir['*.erb'].each do |filename|
17
+ open(filename) do |file|
18
+ # pass in the context object and render the template
19
+ template = ERB.new(file.read)
20
+ template.filename = filename
21
+ code = template.result(@context.get_binding)
22
+
23
+ # write the rendered file to the associated destination
24
+ output_file = File.join(@manifest.output_dir, @manifest.destination_for(filename))
25
+ File.makedirs(File.dirname(output_file))
26
+ open(output_file, 'w') { |f| f.write(code) }
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ class Context
2
+ include ActiveSupport
3
+
4
+ attr_reader :db, :table, :row, :dataset
5
+
6
+ def initialize(manifest)
7
+ @db = manifest.database
8
+ @table = manifest.table.to_s
9
+ @dataset = @db[manifest.table]
10
+ @row = Inflector::singularize(manifest.table.to_s)
11
+ end
12
+
13
+ def columns
14
+ columns_with_types.keys.map(&:to_s)
15
+ end
16
+
17
+ def columns_with_types
18
+ @db.schema(@table.to_sym).inject({}) { |r, v| r[v[0].to_s] = v[1][:type].to_s; r }
19
+ end
20
+
21
+ def primary_key
22
+ @db.schema(@table.to_sym).select { |col_arr| col_arr[1][:primary_key] == true }.map { |col_arr| col_arr[0].to_s }.first
23
+ end
24
+
25
+ def get_binding
26
+ binding
27
+ end
28
+
29
+ end
@@ -0,0 +1,33 @@
1
+ module Shipyard
2
+ class Manifest
3
+ attr_reader :maps
4
+
5
+ def initialize(filename)
6
+ @maps = {}
7
+ open(filename) { |file| instance_eval(file.read) }
8
+ end
9
+
10
+ def table(name = nil)
11
+ @table = name if !!name
12
+ @table
13
+ end
14
+
15
+ def database(sequel = nil)
16
+ @database = sequel if !!sequel
17
+ @database
18
+ end
19
+
20
+ def output_dir(dir = nil)
21
+ @output_dir = dir if !!dir
22
+ @output_dir
23
+ end
24
+
25
+ def map(template, dst)
26
+ @maps[template.to_sym] = dst
27
+ end
28
+
29
+ def destination_for(filename)
30
+ @maps[filename.gsub(/\.erb/, '').to_sym]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'pp'
3
+
4
+ module Shipyard
5
+ describe Context do
6
+
7
+ before(:each) do
8
+ @test_dir = 'test_manifest'
9
+ Dir.mkdir(@test_dir)
10
+ @test_manifest = File.join(@test_dir, 'Manifest')
11
+ File.open(@test_manifest, 'w') do |file|
12
+ file.write <<-EOF
13
+ table :test_table
14
+
15
+ database Sequel.sqlite
16
+
17
+ map :test_template, 'controllers/test.rb'
18
+ EOF
19
+ end
20
+ @m = Manifest.new(@test_manifest)
21
+ end
22
+
23
+ after(:each) do
24
+ File.unlink(@test_manifest)
25
+ Dir.unlink(@test_dir)
26
+ @m = nil
27
+ end
28
+
29
+ it "should read the table name from the manifest" do
30
+ ctx = Context.new(@m)
31
+ ctx.table.should == 'test_table'
32
+ end
33
+
34
+ it "should have a method to return the columns of the table" do
35
+ ctx = Context.new(@m)
36
+ ctx.db.create_table :test_table do
37
+ primary_key :id
38
+ String :name
39
+ end
40
+ ctx.columns.should == [:id, :name]
41
+ end
42
+
43
+ it "should have a method to return the columns with their types" do
44
+ ctx = Context.new(@m)
45
+ ctx.db.create_table :test_table do
46
+ primary_key :id
47
+ column :name, :text
48
+ column :count, :integer
49
+ end
50
+
51
+ ctx.columns_with_types.should == {:id => :integer, :name => :string, :count => :integer}
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ module Shipyard
4
+ describe Manifest do
5
+
6
+ before(:each) do
7
+ @test_db = 'test.db'
8
+ @test_dir = 'test_manifest'
9
+ Dir.mkdir(@test_dir)
10
+ @test_manifest = File.join(@test_dir, 'Manifest')
11
+ File.open(@test_manifest, 'w') do |file|
12
+ file.write <<-EOF
13
+ table :test_table
14
+
15
+ database Sequel.sqlite
16
+
17
+ map :test_template, 'controllers/test.rb'
18
+ EOF
19
+ end
20
+ @m = Manifest.new(@test_manifest)
21
+ end
22
+
23
+ after(:each) do
24
+ File.unlink(@test_manifest)
25
+ Dir.unlink(@test_dir)
26
+ @m = nil
27
+ end
28
+
29
+ it "should read a table name from the manifest file" do
30
+ @m.table.should == :test_table
31
+ end
32
+
33
+ it "should map template files to their output files" do
34
+ @m.maps[:test_template].should == 'controllers/test.rb'
35
+ end
36
+
37
+ it "should get a Sequel database connection from the manifest file" do
38
+ @m.database.should be_a_kind_of Sequel::SQLite::Database
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib/')
4
+ require 'shipyard'
5
+
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shipyard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Kite
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-07-14 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.8
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: sequel
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.12.0
34
+ version:
35
+ description:
36
+ email:
37
+ executables:
38
+ - shipyard
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - lib/shipyard.rb
45
+ - lib/shipyard/context.rb
46
+ - lib/shipyard/builder.rb
47
+ - lib/shipyard/manifest.rb
48
+ - bin/shipyard
49
+ - README.rdoc
50
+ has_rdoc: true
51
+ homepage: http://www.github.com/chriskite/shipyard
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - -m
57
+ - README.rdoc
58
+ - -t
59
+ - Shipyard
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Code generation tool for database-backed applications
81
+ test_files:
82
+ - spec/manifest_spec.rb
83
+ - spec/context_spec.rb
84
+ - spec/spec_helper.rb