namelessjon-dm-gen 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module DMGen
2
- class OneFileGenerator < Templater::Generator
2
+ class OneFile < Templater::Generator
3
3
  first_argument :name, :required => true
4
4
  second_argument :model
5
5
  third_argument :attributes, :as => :hash, :default => {}
@@ -7,6 +7,13 @@ module DMGen
7
7
  desc <<-eos
8
8
  Generates a 'one file test' for DataMapper, including debug logging and sqlite3 memory DB
9
9
  Use like 'dm-gen one_file filename [model] [model,attributes,here]'
10
+
11
+ For example:
12
+ dm-gen one_file validation_test Post title:string,body:text
13
+
14
+ generates a one file test called 'validation_test.rb' which will have a class
15
+ called Post in it, with 3 properties: a Serial id (added for free), and a title
16
+ and a body.
10
17
  eos
11
18
 
12
19
  def self.source_root
@@ -15,14 +22,30 @@ module DMGen
15
22
 
16
23
  template :one_file do |t|
17
24
  t.source = 'one_file.rb'
18
- t.destination = "#{self.name}.rb"
25
+ t.destination = "#{self.filename}.rb"
19
26
  end
20
27
 
21
28
  def model_name
22
29
  (model) ? model.camel_case : "TestModel"
23
30
  end
24
31
 
32
+ def filename
33
+ name.sub(/\.rb\z/,'')
34
+ end
35
+
36
+ def properties
37
+ h = {}
38
+ attributes.each do |k, v|
39
+ # skip if the property is named id
40
+ next if k == "id"
41
+
42
+ # convert to snake/camel case
43
+ h[k.snake_case] = v.camel_case
44
+ end
45
+ h
46
+ end
47
+
25
48
  end
26
49
 
27
- add :one_file, OneFileGenerator
50
+ add :one_file, OneFile
28
51
  end
@@ -3,7 +3,7 @@
3
3
  # A one file test to show ...
4
4
  require 'rubygems'
5
5
 
6
- gem('dm-core', '~> 0.9.7')
6
+ gem('dm-core', '~> 0.9.8')
7
7
  require 'dm-core'
8
8
 
9
9
 
@@ -18,8 +18,8 @@ class <%= model_name %>
18
18
 
19
19
  # properties
20
20
  property :id, Serial
21
- <% attributes.each do |name, type| -%>
22
- property :<%= name.snake_case %>, <%= type.camel_case %>
21
+ <% properties.each do |name, type| -%>
22
+ property :<%= name %>, <%= type %>
23
23
  <% end %>
24
24
  end
25
25
 
data/spec/dm_gen_spec.rb CHANGED
@@ -1,7 +1,2 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
- describe "DmGen" do
4
- it "fails" do
5
- should.flunk "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end
@@ -0,0 +1,111 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+
4
+ shared "one file generator" do
5
+ before do
6
+ @result = @generator.render!.first
7
+ end
8
+
9
+ it "successfully renders" do
10
+ lambda { @generator.render! }.should.not.raise
11
+ end
12
+
13
+ it "makes a file named 'validation_test.rb' when passed 'validation_test'" do
14
+ @generator.should create('/tmp/validation_test.rb')
15
+ end
16
+
17
+ it "makes a file named 'validation_test.rb' when passed 'validation_test.rb'" do
18
+ @generator = DMGen::OneFile.new('/tmp', {}, 'validation_test.rb')
19
+ @generator.should create('/tmp/validation_test.rb')
20
+ end
21
+
22
+ # model stuff
23
+ it "includes DataMapper::Resource" do
24
+ @result.should.match(/include DataMapper::Resource/)
25
+ end
26
+
27
+ it "has a serial id property" do
28
+ @result.should.match(/property :id, Serial/)
29
+ end
30
+
31
+ # requires
32
+ it "requires rubygems" do
33
+ @result.should.match(/require 'rubygems'/)
34
+ end
35
+
36
+ it "requires dm-core" do
37
+ @result.should.match(/require 'dm-core'/)
38
+ end
39
+
40
+ # other boilerplate
41
+ it "sets up a logger" do
42
+ @result.should.match(/DataMapper::Logger\.new\(STDOUT, :debug\)/)
43
+ end
44
+
45
+ it "sets up a connection to an sqlite3 in-memory db" do
46
+ @result.should.match(/DataMapper\.setup\(:default, 'sqlite3::memory:'\)/)
47
+ end
48
+
49
+ it "automigrates the db!" do
50
+ @result.should.match(/DataMapper\.auto_migrate!/)
51
+ end
52
+ end
53
+
54
+
55
+
56
+ describe "DMGen::OneFile" do
57
+ describe "with name option only" do
58
+ before do
59
+ @generator = DMGen::OneFile.new('/tmp', {}, 'validation_test')
60
+ end
61
+
62
+ behaves_like "one file generator"
63
+
64
+ it "makes a Model called TestModel" do
65
+ @result.should.match(/class TestModel/)
66
+ end
67
+ end
68
+
69
+
70
+ describe "with a model name too!" do
71
+ before do
72
+ @generator = DMGen::OneFile.new('/tmp', {}, 'validation_test', 'post')
73
+ end
74
+
75
+ behaves_like "one file generator"
76
+
77
+ it "makes a Model called Post" do
78
+ @result.should.match(/class Post/)
79
+ end
80
+ end
81
+
82
+
83
+ describe "and attributes!" do
84
+ before do
85
+ @generator = DMGen::OneFile.new('/tmp', {}, 'validation_test', 'post',
86
+ 'title:string,PostBody:text,created_at:date_time,id:serial')
87
+ end
88
+
89
+ behaves_like "one file generator"
90
+
91
+ it "makes a Model called Post" do
92
+ @result.should.match(/class Post/)
93
+ end
94
+
95
+ it "includes the title property as a string" do
96
+ @result.should.match(/property :title, String/)
97
+ end
98
+
99
+ it "snake cases the property name" do
100
+ @result.should.match(/property :post_body, Text/)
101
+ end
102
+
103
+ it "camel cases the property type" do
104
+ @result.should.match(/property :created_at, DateTime/)
105
+ end
106
+
107
+ it "only includes a serial property once" do
108
+ @result.should.not.match(/property :id, Serial.*?property :id, Serial/m)
109
+ end
110
+ end
111
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  require 'rubygems'
2
+ gem('bacon')
2
3
  require 'bacon'
3
4
 
5
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
6
+ require 'dm_gen'
7
+
4
8
  # get a summary of errors raised and such
5
9
  Bacon.summary_on_exit
10
+
11
+ def create(file)
12
+ lambda { |obj| obj.all_actions.map{|t| t.destination }.include?(file) }
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: namelessjon-dm-gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Stott
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-01 00:00:00 -08:00
12
+ date: 2008-12-13 00:00:00 -08:00
13
13
  default_executable: dm-gen
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -36,8 +36,9 @@ files:
36
36
  - lib/generators
37
37
  - lib/generators/one_file.rb
38
38
  - lib/dm_gen.rb
39
- - spec/spec_helper.rb
40
39
  - spec/dm_gen_spec.rb
40
+ - spec/spec_helper.rb
41
+ - spec/one_file_spec.rb
41
42
  has_rdoc: false
42
43
  homepage: http://github.com/namelessjon/dm-gen
43
44
  post_install_message: