rails3_artifactor 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -39,6 +39,10 @@ module MyCoolModule
39
39
  end
40
40
  </pre>
41
41
 
42
+ ## Update, Okt. 5 2010
43
+
44
+ Now includes a very flexible API for specifying view files etc. See *view_file_spec.rb* for usage examples.
45
+
42
46
  ## TODO
43
47
 
44
48
  Make DSL even better, fx:
data/Rakefile CHANGED
@@ -7,9 +7,9 @@ begin
7
7
  gem.email = "kmandrup@gmail.com"
8
8
  gem.homepage = "http://github.com/kristianmandrup/rails3_artifact_helper"
9
9
  gem.authors = ["Kristian Mandrup"]
10
- gem.add_development_dependency "rspec", "~> 2.0.0.beta.22"
10
+ gem.add_development_dependency "rspec", ">= 2.0.0.rc"
11
11
 
12
- gem.add_dependency "rspec", "~> 2.0.0.beta.22"
12
+ gem.add_dependency "rspec", ">= 2.0.0.rc"
13
13
  gem.add_dependency "require_all", "~> 1.2.0"
14
14
  gem.add_dependency "sugar-high", "~> 0.2.10"
15
15
  gem.add_dependency 'rails3_assist', "~> 0.2.10"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.2.8
@@ -1,34 +1,157 @@
1
1
  module Rails3::Assist::Artifact
2
- module View
2
+ module View
3
3
  module FileName
4
- DEFAULT_TEMPLATE_LANG = 'html.erb'
5
- DEFAULT_REST_ACTION = 'show'
6
-
7
4
  DIR = Rails3::Assist::Artifact::Directory
8
-
9
- def view_file_name folder, *args
10
- action, type, args = get_view_args(args)
5
+
6
+ module Helper
7
+ def default_template_lang
8
+ 'erb.html'
9
+ end
10
+
11
+ def get_type type
12
+ case type.to_s
13
+ when 'erb'
14
+ 'erb.html'
15
+ when 'haml'
16
+ 'haml.html'
17
+ else
18
+ type
19
+ end
20
+ end
21
+
22
+ def get_view_type type
23
+ get_type(type.empty? ? default_template_lang : type)
24
+ end
25
+
26
+ def filename_type str
27
+ str.split('.')[1..-1].join('.')
28
+ end
29
+
30
+ def filename_name str
31
+ str.gsub /\.(.*)/, ''
32
+ end
33
+ end
34
+
35
+ # TODO: Refactor all code below to make much more DRY !!!
36
+ def view_file_name *args
37
+ folder, action, type = get_view_args(args)
11
38
  File.expand_path File.join(DIR.view_dir, folder.to_s, "#{action}.#{type}")
12
39
  end
13
40
 
14
- def get_view_args args
41
+ def get_view_args *args
42
+ args = args.flatten
43
+ raise ArgumentError, "view_file_name must be called with one or more arguments to return a view file" if args.size == 0
44
+ case args.size
45
+ when 1
46
+ SingleArg.get_view_args *args
47
+ else
48
+ SingleArg.get_view_args *args
49
+ end
50
+ end
51
+ end
52
+
53
+ module SingleArg
54
+ def self.get_view_args *args
15
55
  args = args.flatten
16
- action = DEFAULT_REST_ACTION
17
- type = DEFAULT_TEMPLATE_LANG
18
- case args.first
56
+ arg = args.first
57
+ case arg
19
58
  when Hash
20
- action = args.first.delete(:action)
21
- type = args.first.delete(:type)
22
- when String, Symbol
23
- action = args.delete_at(0)
59
+ # view_file(:person => :show).should == /views\/person\/show\.html\.erb/
60
+ return HashArg.get_view_args arg if arg.keys.size == 1
61
+ # view_file(:folder => 'person', :type => :show).should == /views\/person\/show\.html\.erb/
62
+ HashArgs.get_view_args *args
63
+ when Symbol, String
64
+ TwoArgs.get_view_args *args
65
+ end
66
+ end
67
+
68
+ module HashArg
69
+ extend Rails3::Assist::Artifact::View::FileName::Helper
70
+
71
+ # view_file(:person => :show).should == /views\/person\/show\.html\.erb/
72
+ def self.get_view_args one_hash
73
+ folder = one_hash.keys.first.to_s
74
+ filename = one_hash.values.first.to_s
75
+ action = filename_name filename
76
+ type = get_view_type(filename_type filename)
77
+ [folder, action, type]
24
78
  end
25
- case args.first
26
- when String, Symbol
27
- type = args.delete_at(0)
28
- end
29
- [action || DEFAULT_REST_ACTION, type || DEFAULT_TEMPLATE_LANG, args]
30
79
  end
31
- end
80
+
81
+ module HashArgs
82
+ extend Rails3::Assist::Artifact::View::FileName::Helper
83
+
84
+ # view_file(:folder => 'person', :action => :show, :type => :erb).should == /views\/person\/show\.html\.erb/
85
+ def self.get_view_args hash
86
+ folder = hash[:folder]
87
+ action = hash[:action]
88
+ type = get_view_type(hash[:type])
89
+ [folder, action, type]
90
+ end
91
+ end
92
+
93
+ module StringArg
94
+ extend Rails3::Assist::Artifact::View::FileName::Helper
95
+
96
+ # view_file('person/show').should == /views\/person\/show\.html\.erb/
97
+ def self.get_view_args string
98
+ path_lvs = string.split('/')
99
+ raise ArgumentError, "view must be in a subfolder #{args}" if path_lvs.size < 2
100
+ folder = path_lvs[0..-2].join('/')
101
+ filename = path_lvs.last
102
+ action = filename_name filename
103
+ type = get_view_type(filename_type filename)
104
+ [folder, action, type]
105
+ end
106
+ end
107
+ end
108
+
109
+ module TwoArgs
110
+ def self.get_view_args *args
111
+ args = args.flatten
112
+ arg2 = args[1]
113
+ case arg2
114
+ when String, Symbol
115
+ # view_file(:person, :show).should == /views\/person\/show\.html\.erb/
116
+ # view_file('person/admin', :show, :type => :erb).should == /views\/person\/show\.html\.erb/
117
+ TwoLabels.get_view_args args
118
+ when Hash
119
+ # view_file(:show, :folder => 'person', :type => :erb).should == /views\/person\/show\.html\.erb/
120
+ ActionAndHash.get_view_args args
121
+ end
122
+ end
123
+
124
+ module TwoLabels
125
+ extend Rails3::Assist::Artifact::View::FileName::Helper
126
+
127
+ # view_file(:person, :show).should == /views\/person\/show\.html\.erb/
128
+ # view_file('person/admin', :show, :type => :erb).should == /views\/person\/show\.html\.erb/
129
+ def self.get_view_args *args
130
+ args = args.flatten
131
+ folder = args.first.to_s
132
+ action = args[1].to_s
133
+ hash = args[2] if args.size > 2
134
+ type = get_view_type(hash ? hash[:type] : nil)
135
+ [folder, action, type]
136
+ end
137
+ end
138
+
139
+ module ActionAndHash
140
+ extend Rails3::Assist::Artifact::View::FileName::Helper
141
+
142
+ # view_file(:show, :folder => 'person', :type => :erb).should == /views\/person\/show\.html\.erb/
143
+ def self.get_view_args *args
144
+ args = args.flatten
145
+ action = args.first.to_s
146
+
147
+ hash = args.last
148
+ folder = hash[:folder]
149
+ type = get_view_type(hash[:type])
150
+
151
+ [folder, action, type]
152
+ end
153
+ end
154
+ end
32
155
 
33
156
  include FileName
34
157
  extend FileName
@@ -7,6 +7,18 @@ module Rails3::Assist::Artifact
7
7
  file_name = view_file_name(name, args)
8
8
  file_name.path.file?
9
9
  end
10
+
11
+ def has_views? *args, &block
12
+ options = last_option args
13
+ args.to_strings.each do |name|
14
+ return false if !has_view? name, options
15
+ end
16
+ true
17
+ end
18
+
19
+ def view_file name, *args
20
+ view_file_name(name, args)
21
+ end
10
22
 
11
23
  # CREATE
12
24
  def create_view name, *args, &block
@@ -31,9 +43,8 @@ module Rails3::Assist::Artifact
31
43
  def read_view name, *args, &block
32
44
  file_name = view_file_name(name, args)
33
45
  debug "reading from: #{file_name}"
34
- file = File.new(file_name)
35
- raise "The view file: #{file} could not be found" if !file
36
46
  begin
47
+ file = File.new(file_name)
37
48
  content = file.read
38
49
  debug "read content: #{content}"
39
50
  yield content if block
@@ -44,13 +55,17 @@ module Rails3::Assist::Artifact
44
55
  end
45
56
 
46
57
  # UPDATE
47
- def insert_into_view name, *args, &block
48
- file_name = view_file_name(name, args)
49
- debug "file insertion (view): #{file_name}"
50
- options = last_option args
51
- raise ArgumentError, ":before or :after option must be specified for insertion" if !(options[:before] || options[:after])
52
-
53
- File.insert_into file_name, options, &block
58
+ def insert_into_view *args, &block
59
+ begin
60
+ file_name = view_file_name(args)
61
+ debug "file insertion (view): #{file_name}"
62
+ options = last_option args
63
+ raise ArgumentError, ":before or :after option must be specified for insertion" if !(options[:before] || options[:after])
64
+ File.insert_into file_name, options, &block
65
+ true
66
+ rescue
67
+ nil
68
+ end
54
69
  end
55
70
 
56
71
  # DELETE
@@ -1,8 +1,18 @@
1
1
  require 'sugar-high/array'
2
2
 
3
+ require 'rails3_artifactor/artifact/file_name/view'
4
+
3
5
  module Rails3::Assist::Artifact::CRUD
4
6
  module Delete
5
- def remove_artifact name, type
7
+ class ViewHelper
8
+ extend Rails3::Assist::Artifact::View::FileName
9
+ end
10
+
11
+ def remove_artifact name, *args
12
+ file = ViewHelper.view_file_name(name, args)
13
+ return FileUtils.rm_f(file) if File.exist?(file)
14
+
15
+ type = args.first
6
16
  type = type[:type] if type.kind_of? Hash
7
17
  begin
8
18
  file = existing_file_name name, type
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails3_artifactor}
8
- s.version = "0.2.7"
8
+ s.version = "0.2.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-10-05}
12
+ s.date = %q{2010-10-07}
13
13
  s.description = %q{Helpers for handling Rails 3 artifacts in general, such as CRUD operations etc.}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -58,6 +58,7 @@ Gem::Specification.new do |s|
58
58
  "lib/rails3_artifactor/rspec/configure.rb",
59
59
  "rails3_artifactor.gemspec",
60
60
  "spec/fixtures.rb",
61
+ "spec/fixtures/app/views/account/edit.erb.html",
61
62
  "spec/fixtures/app/views/account/edit.html.erb",
62
63
  "spec/rails3_artifactor/artifact/base_spec.rb",
63
64
  "spec/rails3_artifactor/artifact/crud/controller_spec.rb",
@@ -69,7 +70,7 @@ Gem::Specification.new do |s|
69
70
  "spec/rails3_artifactor/artifact/crud/observer_spec.rb",
70
71
  "spec/rails3_artifactor/artifact/crud/permit_spec.rb",
71
72
  "spec/rails3_artifactor/artifact/crud/view_controller_action_spec.rb",
72
- "spec/rails3_artifactor/artifact/crud/view_controller_default_action_spec.rb",
73
+ "spec/rails3_artifactor/artifact/crud/view_file_spec.rb",
73
74
  "spec/rails3_artifactor/artifact/file_name/artifacts_spec.rb",
74
75
  "spec/rails3_artifactor/artifact/file_name/migration_spec.rb",
75
76
  "spec/rails3_artifactor/artifact/file_name/view_spec.rb",
@@ -104,7 +105,7 @@ Gem::Specification.new do |s|
104
105
  "spec/rails3_artifactor/artifact/crud/observer_spec.rb",
105
106
  "spec/rails3_artifactor/artifact/crud/permit_spec.rb",
106
107
  "spec/rails3_artifactor/artifact/crud/view_controller_action_spec.rb",
107
- "spec/rails3_artifactor/artifact/crud/view_controller_default_action_spec.rb",
108
+ "spec/rails3_artifactor/artifact/crud/view_file_spec.rb",
108
109
  "spec/rails3_artifactor/artifact/file_name/artifacts_spec.rb",
109
110
  "spec/rails3_artifactor/artifact/file_name/migration_spec.rb",
110
111
  "spec/rails3_artifactor/artifact/file_name/view_spec.rb",
@@ -128,23 +129,23 @@ Gem::Specification.new do |s|
128
129
  s.specification_version = 3
129
130
 
130
131
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
131
- s.add_development_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
132
- s.add_runtime_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
132
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0.rc"])
133
+ s.add_runtime_dependency(%q<rspec>, [">= 2.0.0.rc"])
133
134
  s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
134
135
  s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.10"])
135
136
  s.add_runtime_dependency(%q<rails3_assist>, ["~> 0.2.10"])
136
137
  s.add_runtime_dependency(%q<migration_assist>, ["~> 0.1.4"])
137
138
  else
138
- s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
139
- s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
139
+ s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
140
+ s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
140
141
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
141
142
  s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
142
143
  s.add_dependency(%q<rails3_assist>, ["~> 0.2.10"])
143
144
  s.add_dependency(%q<migration_assist>, ["~> 0.1.4"])
144
145
  end
145
146
  else
146
- s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
147
- s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
147
+ s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
148
+ s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
148
149
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
149
150
  s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
150
151
  s.add_dependency(%q<rails3_assist>, ["~> 0.2.10"])
@@ -0,0 +1,3 @@
1
+
2
+ <h1><%= title %></h1>
3
+
@@ -21,34 +21,27 @@ describe 'view API - symbols' do
21
21
  context "Non-existant view(s)" do
22
22
 
23
23
  it "should not fail trying to remove non-existant views" do
24
-
25
24
  remove_views :edit, :show, :folder => :person
26
25
  remove_artifacts :view, :edit, :show, :folder => :person
27
26
 
28
27
  remove_view :show, :folder => :person
29
- # remove_artifact :view, :show, :folder => :person
28
+ remove_artifact :view, :show, :folder => :person
30
29
  end
31
30
 
32
31
  it "should not find a non-existant view" do
33
- pending "TODO"
34
-
35
32
  view_file :show, :folder => :person do |person|
36
33
  fail "should not find person view!"
37
34
  end
38
35
 
39
36
  has_view?(:show, :folder => :person).should be_false
40
- has_views?(:show, :folder => :person).should be_false
37
+ has_views?(:show, :edit, :folder => :person).should be_false
41
38
  end
42
39
 
43
40
  it "should not insert into non-existant view" do
44
- pending "TODO"
45
-
46
41
  insert_into_view(:show, :folder => :person, :after => 'Hello', :content => 'Yes').should_not be_true
47
42
  end
48
43
 
49
44
  it "should not read from non-existant view" do
50
- pending "TODO"
51
-
52
45
  read_view :show, :folder => :person do |content|
53
46
  fail "should not find person content!"
54
47
  end.should_not be_true
@@ -62,43 +55,6 @@ describe 'view API - symbols' do
62
55
  end
63
56
  puts read_view(:account, :edit)
64
57
  read_view(:account, :edit).should have_comment 'hello'
65
-
66
- # root_dir.should have_view :account do |view_file|
67
- # view_file.should have_method :index
68
- # view_file.should have_comment 'hello'
69
- # view_file.should have_comment 'goodbye'
70
- # end
71
58
  end
72
59
  end
73
60
 
74
- # describe 'view API - hash' do
75
- # use_helpers :app, :view
76
- #
77
- # before :each do
78
- # remove_view :account, :action => :edit
79
- # create_view :account, :action => :edit do
80
- # %q{
81
- # <h1><%= title %></h1>
82
- # }
83
- # end
84
- # end
85
- #
86
- # after :each do
87
- # # remove_view :account
88
- # end
89
- #
90
- # it "should have an account_view file that contains an index method and two inserted comments" do
91
- # insert_into_view :account, :action => :edit, :content => '# hello', :before => '<h1>'
92
- # insert_into_view :account, :action => :edit, :before => '<h1>' do
93
- # '# goodbye'
94
- # end
95
- # puts read_view(:account, :action => :edit)
96
- # read_view(:account, :action => :edit).should have_comment 'hello'
97
- # puts view_file_name(:account, :edit)
98
- # # root_dir.should have_view :account do |view_file|
99
- # # view_file.should have_method :index
100
- # # view_file.should have_comment 'hello'
101
- # # view_file.should have_comment 'goodbye'
102
- # # end
103
- # end
104
- # end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'view API - symbols' do
4
+ use_helpers :view
5
+
6
+ def simple_path_expr str
7
+ /#{Regexp.escape(str)}/
8
+ end
9
+
10
+ before :each do
11
+ Rails3::Assist::Directory.rails_root = fixtures_dir
12
+ end
13
+
14
+ it "should find view file using args" do
15
+ simple_path = 'views/person/show.erb.html'
16
+ admin_path = 'views/person/admin/show.erb.html'
17
+
18
+ person_show = simple_path_expr(simple_path)
19
+ person_admin_show = simple_path_expr(admin_path)
20
+
21
+ view_file(:person => :show).should match person_show
22
+ view_file(:person => 'show').should match person_show
23
+ view_file('person/admin/' => 'show').should match person_admin_show
24
+
25
+ view_file(:person, :show).should match person_show
26
+ view_file('person/admin', :show).should match person_admin_show
27
+ view_file(:show, :folder => 'person').should match person_show
28
+ view_file(:folder => 'person', :action => :show).should match person_show
29
+ view_file(:folder => 'person', :action => :show, :type => :erb).should match person_show
30
+ end
31
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 7
9
- version: 0.2.7
8
+ - 8
9
+ version: 0.2.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-05 00:00:00 +02:00
17
+ date: 2010-10-07 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -23,15 +23,14 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ~>
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 2
30
30
  - 0
31
31
  - 0
32
- - beta
33
- - 22
34
- version: 2.0.0.beta.22
32
+ - rc
33
+ version: 2.0.0.rc
35
34
  type: :development
36
35
  version_requirements: *id001
37
36
  - !ruby/object:Gem::Dependency
@@ -40,15 +39,14 @@ dependencies:
40
39
  requirement: &id002 !ruby/object:Gem::Requirement
41
40
  none: false
42
41
  requirements:
43
- - - ~>
42
+ - - ">="
44
43
  - !ruby/object:Gem::Version
45
44
  segments:
46
45
  - 2
47
46
  - 0
48
47
  - 0
49
- - beta
50
- - 22
51
- version: 2.0.0.beta.22
48
+ - rc
49
+ version: 2.0.0.rc
52
50
  type: :runtime
53
51
  version_requirements: *id002
54
52
  - !ruby/object:Gem::Dependency
@@ -162,6 +160,7 @@ files:
162
160
  - lib/rails3_artifactor/rspec/configure.rb
163
161
  - rails3_artifactor.gemspec
164
162
  - spec/fixtures.rb
163
+ - spec/fixtures/app/views/account/edit.erb.html
165
164
  - spec/fixtures/app/views/account/edit.html.erb
166
165
  - spec/rails3_artifactor/artifact/base_spec.rb
167
166
  - spec/rails3_artifactor/artifact/crud/controller_spec.rb
@@ -173,7 +172,7 @@ files:
173
172
  - spec/rails3_artifactor/artifact/crud/observer_spec.rb
174
173
  - spec/rails3_artifactor/artifact/crud/permit_spec.rb
175
174
  - spec/rails3_artifactor/artifact/crud/view_controller_action_spec.rb
176
- - spec/rails3_artifactor/artifact/crud/view_controller_default_action_spec.rb
175
+ - spec/rails3_artifactor/artifact/crud/view_file_spec.rb
177
176
  - spec/rails3_artifactor/artifact/file_name/artifacts_spec.rb
178
177
  - spec/rails3_artifactor/artifact/file_name/migration_spec.rb
179
178
  - spec/rails3_artifactor/artifact/file_name/view_spec.rb
@@ -234,7 +233,7 @@ test_files:
234
233
  - spec/rails3_artifactor/artifact/crud/observer_spec.rb
235
234
  - spec/rails3_artifactor/artifact/crud/permit_spec.rb
236
235
  - spec/rails3_artifactor/artifact/crud/view_controller_action_spec.rb
237
- - spec/rails3_artifactor/artifact/crud/view_controller_default_action_spec.rb
236
+ - spec/rails3_artifactor/artifact/crud/view_file_spec.rb
238
237
  - spec/rails3_artifactor/artifact/file_name/artifacts_spec.rb
239
238
  - spec/rails3_artifactor/artifact/file_name/migration_spec.rb
240
239
  - spec/rails3_artifactor/artifact/file_name/view_spec.rb
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'view' do
4
- use_helpers :view
5
-
6
- before :each do
7
- Rails3::Assist::Directory.rails_root = fixtures_dir
8
-
9
- remove_view :account if has_view? :account
10
-
11
- create_view :account do
12
- %q{ def index
13
- end}
14
- end
15
- end
16
-
17
- after :each do
18
- # remove_view :account
19
- end
20
-
21
- it "should have an account_view file that contains an index method and two inserted comments" do
22
- insert_into_view :account, :content => '# hello', :before => 'def'
23
- insert_into_view :account, :before => 'def' do
24
- '# goodbye'
25
- end
26
- puts read_view(:account)
27
- read_view(:account).should have_comment 'hello'
28
-
29
- # root_dir.should have_view :account do |view_file|
30
- # view_file.should have_method :index
31
- # view_file.should have_comment 'hello'
32
- # view_file.should have_comment 'goodbye'
33
- # end
34
- end
35
- end