maiha-include_for 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,108 @@
1
+ IncludeFor
2
+ ==========
3
+
4
+ create include or link tags automatically according to the given file names
5
+
6
+
7
+ Usage
8
+ =====
9
+
10
+ include_for "prototype.js"
11
+ # => '<script type="text/javascript" src="/javascripts/prototype.js"></script>'
12
+
13
+ include_for "application.css"
14
+ # => '<link type="text/css" rel="Stylesheet" media="screen" href="/stylesheets/application.css"/>'
15
+
16
+
17
+ Example
18
+ =======
19
+
20
+ We can simpliy use the replacement of existing methods.
21
+
22
+ 1. includes one file
23
+
24
+ <%= include_for "prototype.js" %>
25
+ # generates
26
+ <script type="text/javascript" src="/javascripts/prototype.js"></script>
27
+
28
+ 2. plural files
29
+
30
+ <%= include_for "prototype.js", "controls.js" %>
31
+ # generates
32
+ <script type="text/javascript" src="/javascripts/prototype.js"></script>
33
+ <script type="text/javascript" src="/javascripts/controls.js"></script>
34
+
35
+ 3. and css files
36
+
37
+ <%= include_for "prototype.js", "controls.js", "application.css" %>
38
+ # generates
39
+ <script type="text/javascript" src="/javascripts/prototype.js"></script>
40
+ <script type="text/javascript" src="/javascripts/controls.js"></script>
41
+ <link type="text/css" rel="Stylesheet" media="screen" href="/stylesheets/application.css"/>
42
+
43
+ 4. avoid from including same file twice
44
+
45
+ <%= include_for "prototype.js", "prototype.js", "prototype.js" %>
46
+ # generates
47
+ <script type="text/javascript" src="/javascripts/prototype.js"></script>
48
+
49
+ 5. pass an option to the functions (:to is reserved key)
50
+
51
+ <%= include_for "application.js", :charset=>"UTF-8" %>
52
+ # generates
53
+ <script charset="UTF-8" type="text/javascript" src="/javascripts/application.js"></script>
54
+
55
+ Advanced Usage
56
+ ==============
57
+
58
+ And this plugin can also separate declaration and inclusion
59
+ by using '<%' rather than '<%=' for declaration purpose.
60
+
61
+ * declaration for using some scripts
62
+ <% include_for "prototype.js" %>
63
+ # or in some your helpers
64
+ def auto_complete_zipcode(record, ...)
65
+ include_for "prototype.js"
66
+
67
+ * inclusion by using content_for mechanism
68
+
69
+ # in your layout file
70
+ <head>
71
+ <%= yield :header %>
72
+ </head>
73
+
74
+ We'll get following code only in the case that we use "auto_complete_zipcode" method.
75
+
76
+ <head>
77
+ <script type="text/javascript" src="/javascripts/prototype.js"></script>
78
+ </head>
79
+
80
+ And, We can avoid to force wasted javascripts when that method is not used.
81
+
82
+ <head>
83
+ </head>
84
+
85
+ Of course, you can modify the grouping name in +to+ option. (default :header)
86
+
87
+ include_for "prototype.js" # stored to @content_for_header
88
+ include_for "powered-by.js", :to=>:footer # stored to @content_for_footer
89
+
90
+ # in your layout file
91
+ <head>
92
+ <%= yield :header %>
93
+ </head>
94
+ <body>
95
+ <%= yield %>
96
+ <%= yield :footer %>
97
+ </body>
98
+
99
+
100
+ Install
101
+ =======
102
+
103
+ git://github.com/maiha/include_for.git
104
+
105
+
106
+ Author
107
+ ======
108
+ Maiha <maiha@wota.jp>
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the include_for plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the include_for plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'IncludeFor'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # Include hook code here
2
+
3
+ ActionView::Base.class_eval do
4
+ include IncludeFor
5
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,33 @@
1
+ # IncludeFor
2
+ module IncludeFor
3
+ def include_for(*sources)
4
+ sources = sources.flatten
5
+ options = sources.last.is_a?(Hash) ? sources.pop.symbolize_keys : {}
6
+ group = options.delete(:to) || :header
7
+ global_cache = eval("@_include_for_#{options[:to]}_ ||= {}")
8
+ private_cache = {}
9
+
10
+ loadeds = sources.map do |source|
11
+ include_for_cache_or_find(group, source, options, global_cache, private_cache)
12
+ end
13
+ loadeds.compact
14
+ end
15
+
16
+ private
17
+ def include_for_source(source, options)
18
+ case source
19
+ when :defaults, /\.js$/ then javascript_include_tag(source, options)
20
+ when /\.css$/ then stylesheet_link_tag(source, options)
21
+ else source.to_s
22
+ end
23
+ end
24
+
25
+ def include_for_cache_or_find(group, source, options, global_cache, private_cache)
26
+ if private_cache[source]
27
+ nil
28
+ else
29
+ private_cache[source] ||= global_cache[source] ||=
30
+ returning(erb = include_for_source(source, options)) {content_for(group){erb}}
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ class Application
2
+ end
3
+ class ApplicationController
4
+ end
@@ -0,0 +1,111 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+
4
+ describe IncludeFor, 'ファイルはデフォルトで :header グループ' do
5
+ before(:each) do
6
+ @helper = Helper.new
7
+ end
8
+
9
+ it '1ファイルの指定' do
10
+ @helper.include_for('prototype.js')
11
+ @helper.instance_variable_get('@content_for_header').should ==
12
+ 'javascript_include_tag:"prototype.js"'
13
+ end
14
+
15
+ it '複数のファイルを指定した場合' do
16
+ @helper.include_for('prototype.js', 'controls.js')
17
+ @helper.instance_variable_get('@content_for_header').should ==
18
+ ['javascript_include_tag:"prototype.js"',
19
+ 'javascript_include_tag:"controls.js"'].join
20
+ end
21
+
22
+ it '複数のファイルを指定した場合(JSとCSS混在)' do
23
+ @helper.include_for('prototype.js', 'application.css', 'controls.js')
24
+ @helper.instance_variable_get('@content_for_header').should ==
25
+ ['javascript_include_tag:"prototype.js"',
26
+ 'stylesheet_link_tag:"application.css"',
27
+ 'javascript_include_tag:"controls.js"'].join
28
+ end
29
+
30
+ it '重複分は無視される' do
31
+ @helper.include_for('prototype.js', 'application.css', 'controls.js', 'application.css', 'prototype.js')
32
+ @helper.instance_variable_get('@content_for_header').should ==
33
+ ['javascript_include_tag:"prototype.js"',
34
+ 'stylesheet_link_tag:"application.css"',
35
+ 'javascript_include_tag:"controls.js"'].join
36
+ end
37
+ end
38
+
39
+
40
+ describe IncludeFor, ':header グループの明示' do
41
+ before(:each) do
42
+ @helper = Helper.new
43
+ end
44
+
45
+ it '1ファイルの指定' do
46
+ @helper.include_for('prototype.js', :to=>:header)
47
+ @helper.instance_variable_get('@content_for_header').should ==
48
+ 'javascript_include_tag:"prototype.js"'
49
+ end
50
+
51
+ it '複数のファイルを指定した場合' do
52
+ @helper.include_for('prototype.js', 'controls.js', :to=>:header)
53
+ @helper.instance_variable_get('@content_for_header').should ==
54
+ ['javascript_include_tag:"prototype.js"',
55
+ 'javascript_include_tag:"controls.js"'].join
56
+ end
57
+
58
+ it '複数のファイルを指定した場合(JSとCSS混在)' do
59
+ @helper.include_for('prototype.js', 'application.css', 'controls.js', :to=>:header)
60
+ @helper.instance_variable_get('@content_for_header').should ==
61
+ ['javascript_include_tag:"prototype.js"',
62
+ 'stylesheet_link_tag:"application.css"',
63
+ 'javascript_include_tag:"controls.js"'].join
64
+ end
65
+
66
+ it '重複分は無視される' do
67
+ @helper.include_for('prototype.js', 'application.css', 'controls.js', 'application.css', 'prototype.js', :to=>:header)
68
+ @helper.instance_variable_get('@content_for_header').should ==
69
+ ['javascript_include_tag:"prototype.js"',
70
+ 'stylesheet_link_tag:"application.css"',
71
+ 'javascript_include_tag:"controls.js"'].join
72
+ end
73
+ end
74
+
75
+ describe IncludeFor, '別のグループ名の場合' do
76
+ before(:each) do
77
+ @helper = Helper.new
78
+ end
79
+
80
+ it ':footerに追加しても同じ出力を得る' do
81
+ @helper.include_for('prototype.js', :to=>:footer).should ==
82
+ ['javascript_include_tag:"prototype.js"']
83
+ end
84
+
85
+ it ':footerに追加したものは:footerに入る' do
86
+ @helper.include_for('prototype.js', :to=>:footer)
87
+ @helper.instance_variable_get('@content_for_footer').should ==
88
+ ['javascript_include_tag:"prototype.js"'].join
89
+ end
90
+
91
+ it ':footerに追加したものは:headerには影響を与えない' do
92
+ @helper.include_for('prototype.js', :to=>:footer)
93
+ @helper.instance_variable_get('@content_for_header').should ==
94
+ nil
95
+ end
96
+
97
+ it ':headerと:footerにそれぞれ追加しても出力は独立している' do
98
+ @helper.include_for('prototype.js', :to=>:header)
99
+ @helper.include_for('controls.js', :to=>:footer)
100
+ @helper.include_for('application.css', :to=>:header)
101
+ @helper.include_for('defaults.css', :to=>:footer)
102
+
103
+ @helper.instance_variable_get('@content_for_header').should ==
104
+ ['javascript_include_tag:"prototype.js"',
105
+ 'stylesheet_link_tag:"application.css"'].join
106
+
107
+ @helper.instance_variable_get('@content_for_footer').should ==
108
+ ['javascript_include_tag:"controls.js"',
109
+ 'stylesheet_link_tag:"defaults.css"'].join
110
+ end
111
+ end
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe IncludeFor, '直接JSファイル名を指定すると' do
4
+ before(:each) do
5
+ @helper = Helper.new
6
+ end
7
+
8
+ it 'javascript_include_tag の実行結果が配列で返る' do
9
+ @result = @helper.include_for('application.js')
10
+ @result.should ==
11
+ ['javascript_include_tag:"application.js"']
12
+ end
13
+
14
+ it '複数のファイルを指定できる' do
15
+ @result = @helper.include_for('prototype.js', 'controls.js')
16
+ @result.should ==
17
+ ['javascript_include_tag:"prototype.js"',
18
+ 'javascript_include_tag:"controls.js"']
19
+ end
20
+
21
+ it '複数のファイルの順序も保存される' do
22
+ @result = @helper.include_for('controls.js', 'prototype.js')
23
+ @result.should ==
24
+ ['javascript_include_tag:"controls.js"',
25
+ 'javascript_include_tag:"prototype.js"']
26
+ end
27
+
28
+ it '重複するファイルは最初の1つだけが有効になる' do
29
+ @result = @helper.include_for('controls.js', 'prototype.js', 'prototype.js')
30
+ @result.should ==
31
+ ['javascript_include_tag:"controls.js"',
32
+ 'javascript_include_tag:"prototype.js"']
33
+ end
34
+
35
+ it '複数回に分割して実行すると最後だけが有効' do
36
+ @result = @helper.include_for('controls.js')
37
+ @result = @helper.include_for('prototype.js')
38
+ @result.should ==
39
+ ['javascript_include_tag:"prototype.js"']
40
+ end
41
+
42
+ it '複数回に分割して実行して最後が重複している場合でも返される' do
43
+ @result = @helper.include_for('prototype.js')
44
+ @result = @helper.include_for('controls.js')
45
+ @result = @helper.include_for('prototype.js')
46
+ @result.should ==
47
+ ['javascript_include_tag:"prototype.js"']
48
+ end
49
+ end
50
+
51
+
52
+ describe IncludeFor, '直接CSSファイル名を指定すると' do
53
+ before(:each) do
54
+ @helper = Helper.new
55
+ end
56
+
57
+ it 'stylesheet_link_tag の実行結果が配列で返る' do
58
+ @result = @helper.include_for('application.css')
59
+ @result.should ==
60
+ ['stylesheet_link_tag:"application.css"']
61
+ end
62
+
63
+ it '複数のファイルを指定できる' do
64
+ @result = @helper.include_for('prototype.css', 'controls.css')
65
+ @result.should ==
66
+ ['stylesheet_link_tag:"prototype.css"',
67
+ 'stylesheet_link_tag:"controls.css"']
68
+ end
69
+
70
+ it '複数のファイルの順序も保存される' do
71
+ @result = @helper.include_for('controls.css', 'prototype.css')
72
+ @result.should ==
73
+ ['stylesheet_link_tag:"controls.css"',
74
+ 'stylesheet_link_tag:"prototype.css"']
75
+ end
76
+
77
+ it '重複するファイルは最初の1つだけが有効になる' do
78
+ @result = @helper.include_for('controls.css', 'prototype.css', 'prototype.css')
79
+ @result.should ==
80
+ ['stylesheet_link_tag:"controls.css"',
81
+ 'stylesheet_link_tag:"prototype.css"']
82
+ end
83
+ end
84
+
85
+
86
+ describe IncludeFor, '直接JSとCSSファイル名を指定すると' do
87
+ before(:each) do
88
+ @helper = Helper.new
89
+ end
90
+
91
+ it 'それぞれの結果が配列で返る' do
92
+ @result = @helper.include_for('prototype.js', 'application.css', 'controls.js', 'application.js')
93
+ @result.should ==
94
+ ['javascript_include_tag:"prototype.js"',
95
+ 'stylesheet_link_tag:"application.css"',
96
+ 'javascript_include_tag:"controls.js"',
97
+ 'javascript_include_tag:"application.js"']
98
+ end
99
+ end
100
+
101
+
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+
4
+ describe IncludeFor, 'option付きのjs呼び出し' do
5
+ before(:each) do
6
+ @helper = Helper.new
7
+ end
8
+
9
+ it '1属性を指定' do
10
+ @result = @helper.include_for('application.js', :language=>"JavaScript")
11
+ @result.should == ['javascript_include_tag:"application.js",{:language=>"JavaScript"}']
12
+ end
13
+
14
+ it '2属性を指定' do
15
+ @result = @helper.include_for('application.js', :language=>"JavaScript", :charset=>"UTF-8")
16
+ @result.should == ['javascript_include_tag:"application.js",{:charset=>"UTF-8", :language=>"JavaScript"}']
17
+ end
18
+ end
19
+
20
+
21
+ describe IncludeFor, 'option付きのcss呼び出し' do
22
+ before(:each) do
23
+ @helper = Helper.new
24
+ end
25
+
26
+ it '1属性を指定' do
27
+ @result = @helper.include_for('application.css', :media=>"all")
28
+ @result.should == ['stylesheet_link_tag:"application.css",{:media=>"all"}']
29
+ end
30
+
31
+ it '2属性を指定' do
32
+ @result = @helper.include_for('application.css', :media=>"all", :charset=>"UTF-8")
33
+ @result.should == ['stylesheet_link_tag:"application.css",{:charset=>"UTF-8", :media=>"all"}']
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+
4
+ describe IncludeFor, '例外的な指定' do
5
+ before(:each) do
6
+ @helper = Helper.new
7
+ end
8
+
9
+ it ':defaults はシンボルとして受理される' do
10
+ @result = @helper.include_for(:defaults)
11
+ @result.should ==
12
+ ['javascript_include_tag::defaults']
13
+ end
14
+
15
+ it '末尾が js,css でない文字列はそのまま出力される' do
16
+ @result = @helper.include_for("alert('ok')")
17
+ @result.should ==
18
+ ["alert('ok')"]
19
+ end
20
+ end
21
+
data/spec/schema.rb ADDED
@@ -0,0 +1,30 @@
1
+ # This file is autogenerated. Instead of editing this file, please use the
2
+ # migrations feature of ActiveRecord to incrementally modify your database, and
3
+ # then regenerate this schema definition.
4
+
5
+ ActiveRecord::Schema.define(:version => 2) do
6
+
7
+ create_table "cards", :force => true do |t|
8
+ t.column "type", :string
9
+ t.column "no", :integer
10
+ t.column "name", :string
11
+ t.column "cost", :integer
12
+ t.column "kind", :string
13
+ t.column "hp", :integer
14
+ t.column "power", :integer
15
+ t.column "block", :integer
16
+ t.column "counter", :integer
17
+ t.column "capacity", :integer
18
+ t.column "skill_name", :string
19
+ t.column "skill_cost", :integer
20
+ t.column "member", :string
21
+ t.column "help", :string
22
+ end
23
+
24
+ create_table "decks", :force => true do |t|
25
+ t.column "name", :string
26
+ t.column "kind", :string
27
+ t.column "word", :string
28
+ end
29
+
30
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,65 @@
1
+
2
+ # This file is copied to ~/spec when you run 'ruby script/generate rspec'
3
+ # from the project root directory.
4
+
5
+ require 'rubygems'
6
+
7
+ ENV["RAILS_ENV"] = "test"
8
+ # require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
9
+ gem 'activesupport'
10
+ require 'active_support'
11
+
12
+ gem 'actionpack'
13
+ require 'action_controller'
14
+ require 'action_view'
15
+
16
+ gem 'rspec'
17
+ # require 'autotest'
18
+ # require 'autotest/redgreen'
19
+
20
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/include_for")
21
+ $: << File.expand_path(File.dirname(__FILE__) + "/models")
22
+
23
+
24
+ class Helper
25
+ include IncludeFor
26
+ include ActionView::Helpers::CaptureHelper
27
+
28
+ def initialize
29
+ @counts = Hash.new(){0}
30
+ end
31
+
32
+ def count_for(type)
33
+ @counts[type]
34
+ end
35
+
36
+ ######################################################################
37
+ ### Mocks
38
+
39
+ def javascript_include_tag(*args)
40
+ @counts[:js] += 1
41
+ "javascript_include_tag:" + inspect_args(args)
42
+ end
43
+
44
+ def stylesheet_link_tag(*args)
45
+ @counts[:css] += 1
46
+ "stylesheet_link_tag:" + inspect_args(args)
47
+ end
48
+
49
+ private
50
+ def inspect_args(args)
51
+ options = args.last
52
+ if options.is_a?(Hash)
53
+ if options.blank?
54
+ # strip an empty hash argument
55
+ args.pop
56
+ else
57
+ # sort hash keys in inspect (dirty hack)
58
+ sorted = options.inspect.sub(/^\{/, '').sub(/\}$/, '').split(/, /).sort.join(', ')
59
+ return (args[0..-2].map(&:inspect) + ["{%s}" % sorted]).join(',')
60
+ end
61
+ end
62
+ args.map(&:inspect).join(',')
63
+ end
64
+ end
65
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :include_for do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class IncludeForTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maiha-include_for
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - maiha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: maiha@wota.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - init.rb
28
+ - install.rb
29
+ - lib/include_for.rb
30
+ - spec/models/application.rb
31
+ - spec/models/grouping.rb
32
+ - spec/models/include_for.rb
33
+ - spec/models/option.rb
34
+ - spec/models/special.rb
35
+ - spec/schema.rb
36
+ - spec/spec.opts
37
+ - spec/spec_helper.rb
38
+ - tasks/include_for_tasks.rake
39
+ - test/include_for_test.rb
40
+ - uninstall.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/maiha/include_for
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.0.1
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: ""
67
+ test_files: []
68
+