rails_artifactor 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/DESIGN_NOTES.textile +33 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +71 -0
  7. data/Rakefile +53 -0
  8. data/VERSION +1 -0
  9. data/lib/rails_artifactor/artifact/base.rb +59 -0
  10. data/lib/rails_artifactor/artifact/crud/create.rb +12 -0
  11. data/lib/rails_artifactor/artifact/crud/delete.rb +34 -0
  12. data/lib/rails_artifactor/artifact/crud/read.rb +47 -0
  13. data/lib/rails_artifactor/artifact/crud/update.rb +25 -0
  14. data/lib/rails_artifactor/artifact/crud.rb +32 -0
  15. data/lib/rails_artifactor/artifact/file_name/artifacts.rb +21 -0
  16. data/lib/rails_artifactor/artifact/file_name/migration.rb +54 -0
  17. data/lib/rails_artifactor/artifact/file_name/view.rb +172 -0
  18. data/lib/rails_artifactor/artifact/markers.rb +73 -0
  19. data/lib/rails_artifactor/artifact/migration.rb +11 -0
  20. data/lib/rails_artifactor/artifact/orm/active_record.rb +14 -0
  21. data/lib/rails_artifactor/artifact/orm/data_mapper.rb +22 -0
  22. data/lib/rails_artifactor/artifact/orm/mongo_mapper.rb +18 -0
  23. data/lib/rails_artifactor/artifact/orm/mongoid.rb +23 -0
  24. data/lib/rails_artifactor/artifact/orm/none.rb +16 -0
  25. data/lib/rails_artifactor/artifact/orm.rb +55 -0
  26. data/lib/rails_artifactor/artifact/view_artifact.rb +94 -0
  27. data/lib/rails_artifactor/artifact.rb +1 -0
  28. data/lib/rails_artifactor/base/crud/create.rb +62 -0
  29. data/lib/rails_artifactor/base/crud/delete.rb +35 -0
  30. data/lib/rails_artifactor/base/crud/read.rb +13 -0
  31. data/lib/rails_artifactor/base/crud/update.rb +66 -0
  32. data/lib/rails_artifactor/base/crud.rb +6 -0
  33. data/lib/rails_artifactor/base/file_name.rb +41 -0
  34. data/lib/rails_artifactor/base.rb +1 -0
  35. data/lib/rails_artifactor/macro.rb +2 -0
  36. data/lib/rails_artifactor/namespaces.rb +10 -0
  37. data/lib/rails_artifactor/rspec/configure.rb +7 -0
  38. data/lib/rails_artifactor/rspec.rb +1 -0
  39. data/lib/rails_artifactor/ruby_mutator.rb +11 -0
  40. data/lib/rails_artifactor/template_language/base.rb +14 -0
  41. data/lib/rails_artifactor/template_language/erb.rb +24 -0
  42. data/lib/rails_artifactor/template_language/haml.rb +32 -0
  43. data/lib/rails_artifactor/template_language/slim.rb +15 -0
  44. data/lib/rails_artifactor.rb +12 -0
  45. data/rails_artifactor.gemspec +163 -0
  46. data/spec/fixtures/app/views/account/edit.erb.html +3 -0
  47. data/spec/fixtures/app/views/account/edit.html.erb +3 -0
  48. data/spec/fixtures/app/views/layouts/application.erb.html +3 -0
  49. data/spec/fixtures/app/views/layouts/application.html.erb +16 -0
  50. data/spec/fixtures.rb +3 -0
  51. data/spec/rails_artifactor/artifact/base_spec.rb +17 -0
  52. data/spec/rails_artifactor/artifact/crud/controller_spec.rb +65 -0
  53. data/spec/rails_artifactor/artifact/crud/helper_spec.rb +64 -0
  54. data/spec/rails_artifactor/artifact/crud/mailer_spec.rb +62 -0
  55. data/spec/rails_artifactor/artifact/crud/migration_spec.rb +86 -0
  56. data/spec/rails_artifactor/artifact/crud/model_active_record_spec.rb +68 -0
  57. data/spec/rails_artifactor/artifact/crud/model_spec.rb +68 -0
  58. data/spec/rails_artifactor/artifact/crud/observer_spec.rb +65 -0
  59. data/spec/rails_artifactor/artifact/crud/permit_spec.rb +71 -0
  60. data/spec/rails_artifactor/artifact/crud/view_controller_action_spec.rb +92 -0
  61. data/spec/rails_artifactor/artifact/crud/view_file_spec.rb +31 -0
  62. data/spec/rails_artifactor/artifact/file_name/artifacts_spec.rb +33 -0
  63. data/spec/rails_artifactor/artifact/file_name/migration_spec.rb +32 -0
  64. data/spec/rails_artifactor/artifact/file_name/view_spec.rb +38 -0
  65. data/spec/rails_artifactor/artifact/markers_spec.rb +93 -0
  66. data/spec/rails_artifactor/artifact/migration_spec.rb +0 -0
  67. data/spec/rails_artifactor/artifact/orm/active_record_spec.rb +37 -0
  68. data/spec/rails_artifactor/artifact/orm/data_mapper_spec.rb +37 -0
  69. data/spec/rails_artifactor/artifact/orm/mongo_mapper_spec.rb +67 -0
  70. data/spec/rails_artifactor/artifact/orm/mongoid_spec.rb +67 -0
  71. data/spec/rails_artifactor/artifact/orm/none_spec.rb +36 -0
  72. data/spec/rails_artifactor/base/crud/create_spec.rb +31 -0
  73. data/spec/rails_artifactor/base/crud/delete_spec.rb +49 -0
  74. data/spec/rails_artifactor/base/crud/read_spec.rb +80 -0
  75. data/spec/rails_artifactor/base/crud/update_spec.rb +74 -0
  76. data/spec/rails_artifactor/base/file_name_spec.rb +23 -0
  77. data/spec/spec_helper.rb +9 -0
  78. metadata +234 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format nested --color
@@ -0,0 +1,33 @@
1
+ h1. Version 2
2
+
3
+ h2. Orm
4
+
5
+ The macros should allow setting a default :orm, but always allow to specify the orm to use for a particular context.
6
+
7
+ h2. Template language
8
+
9
+ The macros should allow setting a default :template language (Tlang), but always allow to specify the Tlang to use for a particular context.
10
+
11
+ h2. Contexts
12
+
13
+ <pre>
14
+ with :orm => :mongoid, :template_lang => :erb do |app|
15
+ app.create_model 'User::Guest' do |model|
16
+ model.add_field :name, :type => :string, :required => true
17
+ model.add_method :get_stuff, :params => [:a, :b] do
18
+ '@a = a'
19
+ end
20
+ model.add_code do
21
+ %q{
22
+ def hello options = {}
23
+ puts "hi"
24
+ end
25
+ }
26
+ end
27
+ end
28
+
29
+ app.create_model 'User::Guest' do |model|
30
+ model.add_field :name, :type => :string, :required => true
31
+ end
32
+ end
33
+ </pre>
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source :rubygems
2
+ source 'http://gems.github.com/'
3
+
4
+ # group :default do
5
+ # gem "require_all", "~> 1.2.0"
6
+ # gem "sugar-high", "~> 0.3.6"
7
+ # gem "activesupport", "~> 3.0.4"
8
+ # gem 'rails_assist', ">= 0.3.7"
9
+ # end
10
+
11
+ # Add dependencies to develop your gem here.
12
+ # Include everything needed to run rake, tests, features, etc.
13
+ group :development do
14
+ gem "rspec", ">= 2.3.0"
15
+ gem "bundler", "~> 1.0.0"
16
+ gem "jeweler", "~> 1.5.2"
17
+ gem "rcov", ">= 0"
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
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.markdown ADDED
@@ -0,0 +1,71 @@
1
+ # Rails3 Artifactor
2
+
3
+ DSL for operating on various types of Rails 3 application artifacts. The DSL mostly allows performing common CRUD operations etc.
4
+ This is particularly useful when creating Rails 3 generators, plugins etc. that perform code checks and mutations of the app.
5
+ The artifactor has in part been designed for use with the *please me* tool. (see my github account)
6
+
7
+ ## Install
8
+
9
+ <code>gem install rails_artifactor</code>
10
+
11
+ ## Status
12
+
13
+ * Specs pass
14
+ * Still needs some refactoring to tidy it up!
15
+
16
+ ## Usage
17
+
18
+ Introduces the ability to easily load one or more artifact helper modules into your class or module of choice.
19
+ This will introduce various helper methods geared toward operating a that particular kind of artifact, such as CRUD operations etc.
20
+
21
+ <pre>
22
+ module MyCoolModule
23
+ use_helpers :model, :controller
24
+ use_orm :mongoid
25
+
26
+ def make_hello_model
27
+ # this is a method available after loading the :model artifact helper
28
+ # since orm used is set to :mongoid, it will create a mongoid model file
29
+ create_model :hello do
30
+ '# Hello'
31
+ end
32
+
33
+ remove_model :hello if has_model? :hello
34
+
35
+ create_controller :hello do
36
+ ...
37
+ end
38
+ end
39
+ end
40
+ </pre>
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
+
46
+ ## TODO
47
+
48
+ Make DSL even better, fx:
49
+ <pre>
50
+ with_model :account do |m|
51
+ m.create! do
52
+ '# hello'
53
+ end
54
+
55
+ m.remove!
56
+ end
57
+ </pre>
58
+
59
+ ## Note on Patches/Pull Requests
60
+
61
+ * Fork the project.
62
+ * Make your feature addition or bug fix.
63
+ * Add tests for it. This is important so I don't break it in a
64
+ future version unintentionally.
65
+ * Commit, do not mess with rakefile, version, or history.
66
+ (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)
67
+ * Send me a pull request. Bonus points for topic branches.
68
+
69
+ ## Copyright
70
+
71
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "rails_artifactor"
16
+ gem.summary = %Q{Helpers for handling Rails 3 artifacts}
17
+ gem.description = %Q{Helpers for handling Rails 3 artifacts in general, such as CRUD operations etc.}
18
+ gem.email = "kmandrup@gmail.com"
19
+ gem.homepage = "http://github.com/kristianmandrup/rails_artifactor"
20
+ gem.authors = ["Kristian Mandrup"]
21
+
22
+ gem.add_development_dependency "generator-spec", '>= 0.7.3'
23
+
24
+ # gem.add_dependency 'migration_assist', "~> 0.1.7"
25
+ gem.add_dependency "sugar-high", "~> 0.4.0"
26
+ gem.add_dependency 'rails_assist', "~> 0.4.1"
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "rails_assist #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
52
+
53
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.3
@@ -0,0 +1,59 @@
1
+ module RailsAssist
2
+ module BaseHelper
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.class_eval do
6
+ include RailsAssist::App
7
+ # include ::Thor::Actions
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def multi_aliases_for name
13
+ multi_alias :_after_ => name, :create => :new, :insert_into => [:inject_into, :update], :read => :X_content, :remove => :delete
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def get_type options = {}
20
+ case options
21
+ when Hash
22
+ raise ArgumentError, "No artifact type specified #{options}" if !options[:type]
23
+ options[:type].to_sym
24
+ when String, Symbol
25
+ options.to_sym
26
+ else
27
+ raise ArgumentError, "Bad artifact type specified #{options}"
28
+ end
29
+ end
30
+
31
+ def debug?
32
+ RailsAssist.debug_on
33
+ end
34
+
35
+ def debug msg
36
+ puts msg if debug?
37
+ end
38
+
39
+ def set options, type
40
+ options.merge!(:type => type)
41
+ options
42
+ end
43
+
44
+ def orm_notify
45
+ ''
46
+ end
47
+
48
+ def marker_option name, options={}
49
+ type = last_option(options)[:type]
50
+ marker_content = if type
51
+ method = :"#{type}_marker"
52
+ orm_marker = send method, name, options if respond_to? method
53
+ else
54
+ name.to_s.camelize
55
+ end
56
+ options[:before] ? {:before => marker_content} : {:after => marker_content}
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,12 @@
1
+ module RailsAssist::Artifact
2
+ RailsAssist.artifacts.each do |name|
3
+ plural_name = name.to_s.pluralize
4
+ class_eval %{
5
+ module #{name.to_s.camelize}
6
+ def create_#{name} name, options={}, &block
7
+ create_artifact(name, set(options, :#{name}), &block)
8
+ end
9
+ end
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ module RailsAssist::Artifact
2
+ RailsAssist.artifacts.each do |name|
3
+ plural_name = name.to_s.pluralize
4
+ class_eval %{
5
+ module #{name.to_s.camelize}
6
+ def remove_#{name} name
7
+ remove_artifact name, :#{name}
8
+ end
9
+
10
+ def remove_#{plural_name} *names
11
+ remove_artifacts :#{name}, *names
12
+ end
13
+
14
+ def remove_all_#{plural_name}
15
+ RailsAssist::Artifact::Files.#{name}_files.each do |file_name|
16
+ ::File.delete_file! file_name if ::File.file?(file_name)
17
+ end
18
+ end
19
+ alias_method :delete_all_#{plural_name}, :remove_all_#{plural_name}
20
+
21
+ def remove_#{plural_name} *names
22
+ return remove_all_#{plural_name} if names.empty?
23
+ names.to_strings.each do |name|
24
+ file_name = #{name}_file(name)
25
+ ::File.delete!(file_name) if file_name && ::File.file?(file_name)
26
+ end
27
+ end
28
+ alias_method :delete_#{plural_name}, :remove_#{plural_name}
29
+ alias_method :remove_#{name}, :remove_#{plural_name}
30
+ alias_method :delete_#{name}, :remove_#{plural_name}
31
+ end
32
+ }
33
+ end
34
+ end
@@ -0,0 +1,47 @@
1
+ module RailsAssist::Artifact
2
+ RailsAssist.artifacts.each do |name|
3
+ plural_name = name.to_s.pluralize
4
+ class_eval %{
5
+ module #{name.to_s.camelize}
6
+ def has_#{plural_name}? *names
7
+ names.to_strings.each do |name|
8
+ return false if !has_#{name}? name
9
+ end
10
+ true
11
+ end
12
+ alias_method :#{plural_name}_files?, :has_#{plural_name}?
13
+
14
+ def has_#{name}? name, &block
15
+ begin
16
+ found = existing_file_name(name, :#{name}).path.file?
17
+ rescue IOError
18
+ found = false
19
+ end
20
+ yield if block && found
21
+ found
22
+ end
23
+ alias_method :has_#{name}_file?, :has_#{name}?
24
+ alias_method :#{name}_file?, :has_#{name}?
25
+
26
+
27
+ def read_#{name}(name, options={}, &block)
28
+ begin
29
+ read_artifact(name, set(options, :#{name}), &block)
30
+ rescue
31
+ nil
32
+ end
33
+ end
34
+
35
+ def #{name}_file name, &block
36
+ begin
37
+ found = existing_file_name(name, :#{name}).path
38
+ yield found if block && found
39
+ found
40
+ rescue
41
+ nil
42
+ end
43
+ end
44
+ end
45
+ }
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ module RailsAssist::Artifact
2
+ RailsAssist.artifacts.each do |name|
3
+ plural_name = name.to_s.pluralize
4
+ class_eval %{
5
+ module #{name.to_s.camelize}
6
+ def replace_in_#{name} name, options={}, &block
7
+ replace_in_artifact(name, set(options, :#{name}), &block)
8
+ end
9
+
10
+ def replace_in_#{plural_name} *names, &block
11
+ replace_in_artifacts *names, &block
12
+ end
13
+
14
+ def insert_into_#{name}(name, options={}, &block)
15
+ begin
16
+ insert_into_artifact(name, set(options, :#{name}), &block)
17
+ true
18
+ rescue
19
+ nil
20
+ end
21
+ end
22
+ end
23
+ }
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ require 'sugar-high/file'
2
+ require_all File.dirname(__FILE__) + '/crud'
3
+
4
+ module RailsAssist::Artifact
5
+ (RailsAssist.artifacts - [:model]).each do |name|
6
+ class_eval %{
7
+ module #{name.to_s.camelize}
8
+ def new_#{name}_content name, content=nil, options = {}, &block
9
+ options = options.merge(:type => :#{name}, :content => content)
10
+ new_artifact_content name, options, &block
11
+ end
12
+ end
13
+ }
14
+ end
15
+
16
+ RailsAssist.artifacts.each do |name|
17
+ plural_name = name.to_s.pluralize
18
+ class_eval %{
19
+ module #{name.to_s.camelize}
20
+ include RailsAssist::BaseHelper
21
+
22
+ def self.included base
23
+ base.class_eval do
24
+ include RailsAssist::Artifact::CRUD
25
+ end
26
+ end
27
+
28
+ multi_aliases_for :#{name}
29
+ end
30
+ }
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ require 'migration_assist'
2
+ require 'rails_artifactor/base/file_name'
3
+
4
+ module RailsAssist::Artifact
5
+ (RailsAssist.artifacts - [:migration, :view]).each do |name|
6
+ class_eval %{
7
+ module #{name.to_s.camelize}
8
+ module FileName
9
+ include RailsAssist::Artifact::FileName
10
+
11
+ def #{name}_file_name name, options=nil
12
+ artifact_path name, :#{name}, options
13
+ end
14
+ end
15
+
16
+ extend FileName
17
+ include FileName
18
+ end
19
+ }
20
+ end
21
+ end
@@ -0,0 +1,54 @@
1
+ require 'migration_assist'
2
+ require 'sugar-high/regexp'
3
+
4
+ module RailsAssist::Artifact
5
+ module Migration
6
+ module FileName
7
+ include ::RailsAssist::Migration::ClassMethods
8
+
9
+ DIR = RailsAssist::Artifact::Directory
10
+
11
+ class FindError
12
+ attr_accessor :find_expr
13
+
14
+ def initialize find_expr
15
+ self.find_expr = find_expr
16
+ end
17
+ end
18
+
19
+ def migration_file_name name, options={}
20
+ number = options[:number]
21
+
22
+ migration_dir_name = File.expand_path(DIR.migration_dir(options))
23
+
24
+ number = next_migration_number(migration_dir_name) if !number
25
+ File.join(migration_dir_name, "#{number}_#{name}.rb")
26
+ end
27
+
28
+ def find_migration name, options={}
29
+ root_path = options[:root_path]
30
+
31
+ migration_dir_name = File.expand_path(DIR.migration_dir options)
32
+
33
+ migration_find_expr = "#{migration_dir_name}/[0-9]*_*.rb"
34
+ migrations = Dir.glob(migration_find_expr)
35
+
36
+ find_err = FindError.new migration_find_expr
37
+
38
+ return find_err if migrations.empty?
39
+
40
+ migration_find_expr = /\d+_#{name}\.rb$/
41
+ find_err.find_expr = migration_find_expr
42
+
43
+ matching_migrations = migrations.grep_it(migration_find_expr)
44
+
45
+ return find_err if matching_migrations.empty?
46
+
47
+ migration_file = (options[:last]) ? matching_migrations.last : matching_migrations.first
48
+ end
49
+ end
50
+
51
+ include FileName
52
+ extend FileName
53
+ end
54
+ end
@@ -0,0 +1,172 @@
1
+ module RailsAssist::Artifact
2
+ module View
3
+ module FileName
4
+ DIR = RailsAssist::Artifact::Directory
5
+
6
+ module Helper
7
+ def default_template_lang
8
+ get_type :erb
9
+ end
10
+
11
+ def get_type type
12
+ case type.to_s
13
+ when 'erb'
14
+ 'html.erb'
15
+ when 'haml'
16
+ 'html.haml'
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
+ def view_file_name *args
36
+ folder, action, type = get_view_args(args)
37
+ options = last_option args
38
+ root_path = options[:root_path]
39
+ views_path = options[:views_path]
40
+ views_path ||= File.join(root_path, 'app/views') if root_path
41
+ # puts "views: #{views_path}"
42
+ File.expand_path File.join(views_path || DIR.view_dir, folder.to_s, "#{action}.#{type}")
43
+ end
44
+
45
+ def get_view_args *args
46
+ args = args.flatten
47
+ raise ArgumentError, "view_file_name must be called with one or more arguments to return a view file" if args.size == 0
48
+ case args.size
49
+ when 1
50
+ SingleArg.get_view_args *args
51
+ else
52
+ TwoArgs.get_view_args *args
53
+ end
54
+ end
55
+ end
56
+
57
+ module SingleArg
58
+ def self.get_view_args *args
59
+ args = args.flatten
60
+ arg = args.first
61
+ case arg
62
+ when Hash
63
+ # view_file(:person => :show).should == /views\/person\/show\.html\.erb/
64
+ return HashArg.get_view_args arg if arg.keys.size == 1
65
+ # view_file(:folder => 'person', :type => :show).should == /views\/person\/show\.html\.erb/
66
+ HashArgs.get_view_args *args
67
+ when Symbol, String
68
+ TwoArgs.get_view_args *args
69
+ end
70
+ end
71
+
72
+ module HashArg
73
+ extend RailsAssist::Artifact::View::FileName::Helper
74
+
75
+ # view_file(:person => :show).should == /views\/person\/show\.html\.erb/
76
+ def self.get_view_args one_hash
77
+ folder = one_hash.keys.first.to_s
78
+ filename = one_hash.values.first.to_s
79
+ action = filename_name filename
80
+ type = get_view_type(filename_type filename)
81
+ [folder, action, type]
82
+ end
83
+ end
84
+
85
+ module HashArgs
86
+ extend RailsAssist::Artifact::View::FileName::Helper
87
+
88
+ DIR = RailsAssist::Artifact::Directory
89
+
90
+ # view_file(:folder => 'person', :action => :show, :type => :erb).should == /views\/person\/show\.html\.erb/
91
+ def self.get_view_args hash
92
+ try_folder = hash.keys.first
93
+ try_view_folder = File.expand_path(File.join(DIR.view_dir, try_folder.to_s))
94
+ if File.directory? try_view_folder
95
+ folder = try_folder
96
+ action = hash.values.first
97
+ else
98
+ folder = hash[:folder]
99
+ action = hash[:action]
100
+ end
101
+ type = get_view_type(hash[:type])
102
+ [folder, action, type]
103
+ end
104
+ end
105
+
106
+ module StringArg
107
+ extend RailsAssist::Artifact::View::FileName::Helper
108
+
109
+ # view_file('person/show').should == /views\/person\/show\.html\.erb/
110
+ def self.get_view_args string
111
+ path_lvs = string.split('/')
112
+ raise ArgumentError, "view must be in a subfolder #{args}" if path_lvs.size < 2
113
+ folder = path_lvs[0..-2].join('/')
114
+ filename = path_lvs.last
115
+ action = filename_name filename
116
+ type = get_view_type(filename_type filename)
117
+ [folder, action, type]
118
+ end
119
+ end
120
+ end
121
+
122
+ module TwoArgs
123
+ def self.get_view_args *args
124
+ args = args.flatten
125
+ arg2 = args[1]
126
+ res = case arg2
127
+ when String, Symbol
128
+ # view_file(:person, :show).should == /views\/person\/show\.html\.erb/
129
+ # view_file('person/admin', :show, :type => :erb).should == /views\/person\/show\.html\.erb/
130
+ TwoLabels.get_view_args args
131
+ when Hash
132
+ # view_file(:show, :folder => 'person', :type => :erb).should == /views\/person\/show\.html\.erb/
133
+ ActionAndHash.get_view_args args
134
+ end
135
+ end
136
+
137
+ module TwoLabels
138
+ extend RailsAssist::Artifact::View::FileName::Helper
139
+
140
+ # view_file(:person, :show).should == /views\/person\/show\.html\.erb/
141
+ # view_file('person/admin', :show, :type => :erb).should == /views\/person\/show\.html\.erb/
142
+ def self.get_view_args *args
143
+ args = args.flatten
144
+ folder = args.first.to_s
145
+ action = args[1].to_s
146
+ hash = args[2] if args.size > 2
147
+ type = get_view_type(hash ? hash[:type] : nil)
148
+ [folder, action, type]
149
+ end
150
+ end
151
+
152
+ module ActionAndHash
153
+ extend RailsAssist::Artifact::View::FileName::Helper
154
+
155
+ # view_file(:show, :folder => 'person', :type => :erb).should == /views\/person\/show\.html\.erb/
156
+ def self.get_view_args *args
157
+ args = args.flatten
158
+ action = args.first.to_s
159
+
160
+ hash = args.last
161
+ folder = hash[:folder]
162
+ type = get_view_type(hash[:type])
163
+
164
+ [folder, action, type]
165
+ end
166
+ end
167
+ end
168
+
169
+ include FileName
170
+ extend FileName
171
+ end
172
+ end