rails_artifactor 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/VERSION +1 -1
  2. data/lib/rails_artifactor/artifact/asset_artifact.rb +120 -0
  3. data/lib/rails_artifactor/artifact/base.rb +9 -9
  4. data/lib/rails_artifactor/artifact/crud.rb +6 -6
  5. data/lib/rails_artifactor/artifact/crud/create.rb +2 -2
  6. data/lib/rails_artifactor/artifact/crud/delete.rb +13 -13
  7. data/lib/rails_artifactor/artifact/crud/update.rb +5 -5
  8. data/lib/rails_artifactor/artifact/file_name/artifacts.rb +4 -4
  9. data/lib/rails_artifactor/artifact/file_name/asset.rb +176 -0
  10. data/lib/rails_artifactor/artifact/file_name/view.rb +42 -42
  11. data/lib/rails_artifactor/artifact/markers.rb +24 -25
  12. data/lib/rails_artifactor/artifact/orm.rb +5 -5
  13. data/lib/rails_artifactor/artifact/orm/active_record.rb +2 -2
  14. data/lib/rails_artifactor/artifact/orm/data_mapper.rb +2 -2
  15. data/lib/rails_artifactor/artifact/orm/mongo_mapper.rb +4 -4
  16. data/lib/rails_artifactor/artifact/orm/mongoid.rb +2 -2
  17. data/lib/rails_artifactor/artifact/orm/none.rb +1 -1
  18. data/lib/rails_artifactor/artifact/view_artifact.rb +11 -11
  19. data/lib/rails_artifactor/base/crud/create.rb +14 -14
  20. data/lib/rails_artifactor/base/crud/delete.rb +10 -10
  21. data/lib/rails_artifactor/base/crud/update.rb +21 -21
  22. data/lib/rails_artifactor/base/file_name.rb +12 -12
  23. data/lib/rails_artifactor/template_language/base.rb +3 -3
  24. data/lib/rails_artifactor/template_language/erb.rb +4 -4
  25. data/lib/rails_artifactor/template_language/haml.rb +5 -6
  26. data/lib/rails_artifactor/template_language/slim.rb +5 -5
  27. data/rails_artifactor.gemspec +13 -10
  28. data/spec/rails_artifactor/artifact/crud/asset_spec.rb +56 -0
  29. data/spec/rails_artifactor/artifact/crud/view_controller_action_spec.rb +12 -12
  30. data/spec/rails_artifactor/artifact/crud/view_file_spec.rb +6 -6
  31. metadata +17 -14
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -0,0 +1,120 @@
1
+ module RailsAssist::Artifact
2
+ module Asset
3
+ include RailsAssist::BaseHelper
4
+ include RailsAssist::Artifact::CRUD
5
+
6
+ def has_asset? name, *args, &block
7
+ file_name = asset_file_name(name, args)
8
+ file_name.path.file?
9
+ end
10
+
11
+ def has_assets? *args, &block
12
+ options = last_option args
13
+ args.to_strings.each do |name|
14
+ return false if !has_asset? name, options
15
+ end
16
+ true
17
+ end
18
+
19
+ def asset_file *args
20
+ asset_file_name(args)
21
+ end
22
+
23
+ # CREATE
24
+ def create_asset *args, &block
25
+ file_name = asset_file_name(args)
26
+ dir = File.dirname(file_name)
27
+ FileUtils.mkdir_p dir if !File.directory?(dir)
28
+
29
+ content = get_asset_content(args) || yield if block
30
+
31
+ # abort if no content given
32
+ debug "Warning: Content must be passed in either as a :content hash or a block" if !content
33
+ return nil if !content
34
+
35
+ debug "Writing asset file: #{file_name}"
36
+ # write file content of asset
37
+ File.open(file_name, 'w') do |f|
38
+ f.puts content
39
+ end
40
+ end
41
+
42
+ # READ
43
+ def read_asset *args, &block
44
+ file_name = asset_file_name(args)
45
+ debug "reading from: #{file_name}"
46
+ begin
47
+ file = File.new(file_name)
48
+ content = file.read
49
+ debug "read content: #{content}"
50
+ yield content if block
51
+ content
52
+ rescue
53
+ nil
54
+ end
55
+ end
56
+
57
+ # UPDATE
58
+ def insert_into_asset *args, &block
59
+ begin
60
+ file_name = asset_file_name(args)
61
+ options = last_option args
62
+ raise ArgumentError, ":before or :after option must be specified for insertion" if !(options[:before] || options[:after])
63
+ File.insert_into file_name, options, &block
64
+ true
65
+ rescue
66
+ nil
67
+ end
68
+ end
69
+
70
+ # DELETE
71
+ def remove_asset *args
72
+ file = asset_file_name(args)
73
+ FileUtils.rm_f(file) if File.exist?(file)
74
+ end
75
+
76
+ # remove_assets :edit, :show, :folder => :javascripts
77
+ def remove_assets *args
78
+ options = last_option args
79
+ raise ArgumentError, "Missing :folder option in the last argument which must be a Hash" if !options && !options[:folder]
80
+ args.to_symbols.each{|name| remove_asset name, options}
81
+ end
82
+
83
+ [:javascript, :coffeescript, :stylesheet].each do |type|
84
+ class_eval %{
85
+ def create_#{type}_asset *args, &block
86
+ options = last_option args
87
+ args.last = options.merge :folder => :#{type}
88
+ create_asset *args, &block
89
+ end
90
+
91
+ def read_#{type}_asset *args, &block
92
+ options = last_option args
93
+ args.last = options.merge :folder => :#{type}
94
+ read_asset *args, &block
95
+ end
96
+
97
+ def remove_#{type}_asset *args, &block
98
+ options = last_option args
99
+ args.last = options.merge :folder => :#{type}
100
+ remove_asset *args, &block
101
+ end
102
+
103
+ def remove_#{type}_assets *args
104
+ remove_assets *args, :folder => :#{type.to_s.pluralize}
105
+ end
106
+ }
107
+ end
108
+
109
+ def get_asset_content args
110
+ args = args.flatten
111
+ case args.first
112
+ when Hash
113
+ args.first[:content]
114
+ end
115
+ end
116
+
117
+ multi_aliases_for :asset
118
+ end
119
+ end
120
+
@@ -13,7 +13,7 @@ module RailsAssist
13
13
  multi_alias :_after_ => name, :create => :new, :insert_into => [:inject_into, :update], :read => :X_content, :remove => :delete
14
14
  end
15
15
  end
16
-
16
+
17
17
  protected
18
18
 
19
19
  def get_type options = {}
@@ -24,13 +24,13 @@ module RailsAssist
24
24
  when String, Symbol
25
25
  options.to_sym
26
26
  else
27
- raise ArgumentError, "Bad artifact type specified #{options}"
27
+ raise ArgumentError, "Bad artifact type specified #{options}"
28
28
  end
29
29
  end
30
-
31
- def debug?
30
+
31
+ def debug?
32
32
  RailsAssist.debug_on
33
- end
33
+ end
34
34
 
35
35
  def debug msg
36
36
  puts msg if debug?
@@ -45,15 +45,15 @@ module RailsAssist
45
45
  ''
46
46
  end
47
47
 
48
- def marker_option name, options={}
48
+ def marker_option name, options={}
49
49
  type = last_option(options)[:type]
50
50
  marker_content = if type
51
51
  method = :"#{type}_marker"
52
52
  orm_marker = send method, name, options if respond_to? method
53
53
  else
54
54
  name.to_s.camelize
55
- end
56
- options[:before] ? {:before => marker_content} : {:after => marker_content}
55
+ end
56
+ options[:before] ? {:before => marker_content} : {:after => marker_content}
57
57
  end
58
58
  end
59
- end
59
+ end
@@ -18,15 +18,15 @@ module RailsAssist::Artifact
18
18
  class_eval %{
19
19
  module #{name.to_s.camelize}
20
20
  include RailsAssist::BaseHelper
21
-
21
+
22
22
  def self.included base
23
- base.class_eval do
24
- include RailsAssist::Artifact::CRUD
23
+ base.class_eval do
24
+ include RailsAssist::Artifact::CRUD
25
25
  end
26
26
  end
27
-
28
- multi_aliases_for :#{name}
27
+
28
+ multi_aliases_for :#{name}
29
29
  end
30
30
  }
31
- end
31
+ end
32
32
  end
@@ -5,8 +5,8 @@ module RailsAssist::Artifact
5
5
  module #{name.to_s.camelize}
6
6
  def create_#{name} name, options={}, &block
7
7
  create_artifact(name, set(options, :#{name}), &block)
8
- end
8
+ end
9
9
  end
10
10
  }
11
11
  end
12
- end
12
+ end
@@ -2,33 +2,33 @@ module RailsAssist::Artifact
2
2
  RailsAssist.artifacts.each do |name|
3
3
  plural_name = name.to_s.pluralize
4
4
  class_eval %{
5
- module #{name.to_s.camelize}
5
+ module #{name.to_s.camelize}
6
6
  def remove_#{name} name
7
7
  remove_artifact name, :#{name}
8
8
  end
9
-
10
- def remove_#{plural_name} *names
9
+
10
+ def remove_#{plural_name} *names
11
11
  remove_artifacts :#{name}, *names
12
- end
13
-
12
+ end
13
+
14
14
  def remove_all_#{plural_name}
15
- RailsAssist::Artifact::Files.#{name}_files.each do |file_name|
15
+ RailsAssist::Artifact::Files.#{name}_files.each do |file_name|
16
16
  ::File.delete_file! file_name if ::File.file?(file_name)
17
17
  end
18
18
  end
19
- alias_method :delete_all_#{plural_name}, :remove_all_#{plural_name}
20
-
19
+ alias_method :delete_all_#{plural_name}, :remove_all_#{plural_name}
20
+
21
21
  def remove_#{plural_name} *names
22
- return remove_all_#{plural_name} if names.empty?
22
+ return remove_all_#{plural_name} if names.empty?
23
23
  names.to_strings.each do |name|
24
24
  file_name = #{name}_file(name)
25
25
  ::File.delete!(file_name) if file_name && ::File.file?(file_name)
26
26
  end
27
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}
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
31
  end
32
32
  }
33
33
  end
34
- end
34
+ end
@@ -2,18 +2,18 @@ module RailsAssist::Artifact
2
2
  RailsAssist.artifacts.each do |name|
3
3
  plural_name = name.to_s.pluralize
4
4
  class_eval %{
5
- module #{name.to_s.camelize}
5
+ module #{name.to_s.camelize}
6
6
  def replace_in_#{name} name, options={}, &block
7
7
  replace_in_artifact(name, set(options, :#{name}), &block)
8
- end
8
+ end
9
9
 
10
10
  def replace_in_#{plural_name} *names, &block
11
11
  replace_in_artifacts *names, &block
12
12
  end
13
-
13
+
14
14
  def insert_into_#{name}(name, options={}, &block)
15
15
  begin
16
- insert_into_artifact(name, set(options, :#{name}), &block)
16
+ insert_into_artifact(name, set(options, :#{name}), &block)
17
17
  true
18
18
  rescue
19
19
  nil
@@ -22,4 +22,4 @@ module RailsAssist::Artifact
22
22
  end
23
23
  }
24
24
  end
25
- end
25
+ end
@@ -2,15 +2,15 @@
2
2
  require 'rails_artifactor/base/file_name'
3
3
 
4
4
  module RailsAssist::Artifact
5
- (RailsAssist.artifacts - [:migration, :view]).each do |name|
5
+ (RailsAssist.artifacts - [:migration, :view, :asset]).each do |name|
6
6
  class_eval %{
7
7
  module #{name.to_s.camelize}
8
8
  module FileName
9
9
  include RailsAssist::Artifact::FileName
10
-
10
+
11
11
  def #{name}_file_name name, options=nil
12
12
  artifact_path name, :#{name}, options
13
- end
13
+ end
14
14
  end
15
15
 
16
16
  extend FileName
@@ -18,4 +18,4 @@ module RailsAssist::Artifact
18
18
  end
19
19
  }
20
20
  end
21
- end
21
+ end
@@ -0,0 +1,176 @@
1
+ module RailsAssist::Artifact
2
+ module Asset
3
+ module FileName
4
+ DIR = RailsAssist::Artifact::Directory
5
+
6
+ module Helper
7
+ def type_from folder
8
+ case folder.to_s
9
+ when /javascript/
10
+ :js
11
+ when /style/
12
+ :css
13
+ else
14
+ folder.to_s # raise ArgumentError, "Asset type could not be determined from #{folder}"
15
+ end
16
+ end
17
+
18
+ def get_type type
19
+ case type.to_s
20
+ when 'coffee'
21
+ 'js.coffee'
22
+ when 'scss'
23
+ 'css.scss'
24
+ when 'sass'
25
+ 'css.sass'
26
+ else
27
+ type_from type
28
+ end
29
+ end
30
+
31
+ def get_asset_type type
32
+ raise "No type defined" if !type || type.empty?
33
+ get_type(type)
34
+ end
35
+
36
+ def filename_type str
37
+ str.split('.')[1..-1].join('.')
38
+ end
39
+
40
+ def filename_name str
41
+ str.gsub /\.(.*)/, ''
42
+ end
43
+ end
44
+
45
+ def asset_file_name *args
46
+ folder, name, type = get_asset_args(args)
47
+ options = last_option args
48
+ root_path = options[:root_path]
49
+ assets_path = options[:assets_path]
50
+ assets_path ||= File.join(root_path, 'app/assets') if root_path
51
+ File.expand_path File.join(assets_path || DIR.asset_dirpath, folder.to_s, "#{name}.#{type}")
52
+ end
53
+
54
+ def get_asset_args *args
55
+ args = args.flatten
56
+ raise ArgumentError, "asset_file_name must be called with one or more arguments to return a asset file" if args.size == 0
57
+ case args.size
58
+ when 1
59
+ SingleArg.get_asset_args *args
60
+ else
61
+ TwoArgs.get_asset_args *args
62
+ end
63
+ end
64
+ end
65
+
66
+ module SingleArg
67
+ def self.get_asset_args *args
68
+ args = args.flatten
69
+ arg = args.first
70
+ case arg
71
+ when Hash
72
+ return HashArg.get_asset_args arg if arg.keys.size == 1
73
+ HashArgs.get_asset_args *args
74
+ when Symbol, String
75
+ TwoArgs.get_asset_args *args
76
+ end
77
+ end
78
+
79
+ module HashArg
80
+ extend RailsAssist::Artifact::Asset::FileName::Helper
81
+
82
+ def self.get_asset_args one_hash
83
+ folder = one_hash.keys.first.to_s
84
+ filename = one_hash.values.first.to_s
85
+ name = filename_name filename
86
+ type = get_asset_type(filename_type filename)
87
+ [folder, name, type]
88
+ end
89
+ end
90
+
91
+ module HashArgs
92
+ extend RailsAssist::Artifact::Asset::FileName::Helper
93
+
94
+ DIR = RailsAssist::Artifact::Directory
95
+
96
+ # asset_file(:folder => 'javascripts', :name => 'hello', :type => :js).should == /assets\/javascripts\/hello\.js/
97
+ def self.get_asset_args hash
98
+ try_folder = hash.keys.first
99
+ try_asset_folder = File.expand_path(File.join(DIR.asset_dir, try_folder.to_s))
100
+ if File.directory? try_asset_folder
101
+ folder = try_folder
102
+ name = hash.values.first
103
+ else
104
+ folder = hash[:folder]
105
+ name = hash[:name]
106
+ end
107
+ type = get_asset_type(hash[:type] || folder)
108
+ [folder, name, type]
109
+ end
110
+ end
111
+
112
+ module StringArg
113
+ extend RailsAssist::Artifact::Asset::FileName::Helper
114
+
115
+ # asset_file('stylesheets/localize').should == /assets\/stylesheets\/localize\.css/
116
+ def self.get_asset_args string
117
+ path_lvs = string.split('/')
118
+ raise ArgumentError, "asset must be in a subfolder #{args}" if path_lvs.size < 2
119
+ folder = path_lvs[0..-2].join('/')
120
+ filename = path_lvs.last
121
+ name = filename_name filename
122
+ type = get_asset_type(filename_type filename)
123
+ [folder, name, type]
124
+ end
125
+ end
126
+ end
127
+
128
+ module TwoArgs
129
+ def self.get_asset_args *args
130
+ args = args.flatten
131
+ arg2 = args[1]
132
+ res = case arg2
133
+ when String, Symbol
134
+ # asset_file(:stylesheets, :show).should == /assets\/stylesheets\/show\.css/
135
+ TwoLabels.get_asset_args args
136
+ when Hash
137
+ # asset_file(:show, :folder => 'javascripts', :type => :js).should == /assets\/javascripts\/show\.js/
138
+ NameAndHash.get_asset_args args
139
+ end
140
+ end
141
+
142
+ module TwoLabels
143
+ extend RailsAssist::Artifact::Asset::FileName::Helper
144
+
145
+ # asset_file(:person, :show, :type => 'js').should == /assets\/person\/show\.js/
146
+ def self.get_asset_args *args
147
+ args = args.flatten
148
+ folder = args.first.to_s
149
+ action = args[1].to_s
150
+ hash = args[2] if args.size > 2
151
+ type = get_asset_type(hash[:type] || folder)
152
+ [folder, action, type]
153
+ end
154
+ end
155
+
156
+ module NameAndHash
157
+ extend RailsAssist::Artifact::Asset::FileName::Helper
158
+
159
+ def self.get_asset_args *args
160
+ args = args.flatten
161
+ action = args.first.to_s
162
+
163
+ hash = args.last
164
+ folder = hash[:folder]
165
+ type = get_asset_type(hash[:type] || folder)
166
+
167
+ [folder, action, type]
168
+ end
169
+ end
170
+ end
171
+
172
+ include FileName
173
+ extend FileName
174
+ end
175
+ end
176
+