inline_template_loader 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module InlineTemplateLoader
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,9 +1,17 @@
1
1
  require "inline_template_loader/version"
2
2
 
3
3
  module InlineTemplateLoader
4
- def self.load(file = nil)
5
- if file.nil?
6
- file = caller.first.split(':').first
4
+ def self.load(arg = nil)
5
+ if arg.is_a? ::Integer
6
+ caller_pos = arg
7
+ elsif
8
+ caller_pos = 0
9
+ end
10
+
11
+ if arg.is_a? ::String
12
+ file = arg
13
+ elsif
14
+ file = caller[caller_pos].split(':').first
7
15
  end
8
16
 
9
17
  templates = {}
@@ -11,15 +19,17 @@ module InlineTemplateLoader
11
19
 
12
20
  app, data = ::IO.read(file).gsub("\r\n", "\n").split(/^__END__$/, 2)
13
21
 
14
- data.each_line do |line|
15
- if line =~ /^@@\s*(.*\S)\s*$/
16
- sym = $1.to_sym
17
- else
18
- if sym
19
- unless templates[sym]
20
- templates[sym] = line
21
- else
22
- templates[sym] += line
22
+ if data
23
+ data.each_line do |line|
24
+ if line =~ /^@@\s*(.*\S)\s*$/
25
+ sym = $1.to_sym
26
+ else
27
+ if sym
28
+ unless templates[sym]
29
+ templates[sym] = line
30
+ else
31
+ templates[sym] += line
32
+ end
23
33
  end
24
34
  end
25
35
  end
@@ -0,0 +1,21 @@
1
+ require 'inline_template_loader'
2
+
3
+ class DslUsesInlineTemplateLoader
4
+ def self.load_template
5
+ # 0: dsl_uses_inline_template_loader.rb: load_templates
6
+ # 1: external_file.rb: &block passed for dsl
7
+ # 2: dsl_uses_inline_template_loader.rb: class_eval
8
+ # 3: dsl_uses_inline_template_loader.rb: dsl
9
+ # 4: external_file.rb: top
10
+
11
+ @@templates = InlineTemplateLoader.load 4
12
+ end
13
+
14
+ def self.dsl(&block)
15
+ class_eval &block
16
+ end
17
+
18
+ def self.templates
19
+ @@templates
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('./dsl_uses_inline_template_loader', File.dirname(__FILE__))
2
+
3
+ DslUsesInlineTemplateLoader.dsl do
4
+ load_template
5
+ end
6
+
7
+ __END__
8
+ @@ foo
9
+ This is an external file
File without changes
@@ -1,21 +1,56 @@
1
1
  require 'inline_template_loader'
2
2
 
3
3
  describe 'InlineTemplateLoader' do
4
+ let(:fixtures_dir) { File.expand_path('../fixtures', File.dirname(__FILE__)) }
5
+ let(:external_file) { fixtures_dir + '/external_file.rb' }
6
+ let(:external_file) { fixtures_dir + '/external_file.rb' }
7
+ let(:external_file_without_templates) { fixtures_dir + '/external_file_without_templates.rb' }
8
+
4
9
  describe '#load' do
5
- shared_examples_for 'load templates correctly from this file' do
6
- it { should == {foo: "Here is template named foo.\n\n", bar: "Here is template named bar.\n"} }
10
+ context 'from this file' do
11
+ shared_examples_for 'load templates correctly from this file' do
12
+ it { should == {foo: "Here is template named foo.\n\n", bar: "Here is template named bar.\n"} }
13
+ end
14
+
15
+ context 'with filepath' do
16
+ subject { InlineTemplateLoader.load(__FILE__) }
17
+
18
+ it_behaves_like 'load templates correctly from this file'
19
+ end
20
+
21
+ context 'without filepath' do
22
+ subject { InlineTemplateLoader.load }
23
+
24
+ it_behaves_like 'load templates correctly from this file'
25
+ end
7
26
  end
8
27
 
9
- context 'with filepath' do
10
- subject { InlineTemplateLoader.load(__FILE__) }
28
+ context 'from external file' do
29
+ shared_examples_for 'load templates correctly from external file' do
30
+ it { should == {foo: "This is an external file\n"} }
31
+ end
32
+
33
+ context 'with filepath' do
34
+ subject { InlineTemplateLoader.load(external_file) }
35
+
36
+ it_behaves_like 'load templates correctly from external file'
37
+ end
38
+
39
+ context 'with module uses inline template loader' do
40
+ before do
41
+ require external_file
42
+ end
43
+
44
+ subject { DslUsesInlineTemplateLoader.templates }
11
45
 
12
- it_behaves_like 'load templates correctly from this file'
46
+ it_behaves_like 'load templates correctly from external file'
47
+ end
13
48
  end
14
49
 
15
- context 'without filepath' do
16
- subject { InlineTemplateLoader.load }
50
+ context 'from external file without templates' do
51
+ subject { InlineTemplateLoader.load(external_file_without_templates) }
17
52
 
18
- it_behaves_like 'load templates correctly from this file'
53
+ it { should == {} }
19
54
  end
20
55
  end
21
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_template_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -94,6 +94,9 @@ files:
94
94
  - inline_template_loader.gemspec
95
95
  - lib/inline_template_loader.rb
96
96
  - lib/inline_template_loader/version.rb
97
+ - spec/fixtures/dsl_uses_inline_template_loader.rb
98
+ - spec/fixtures/external_file.rb
99
+ - spec/fixtures/external_file_without_templates.rb
97
100
  - spec/lib/inline_template_loader_spec.rb
98
101
  homepage: ''
99
102
  licenses: []
@@ -109,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
112
  version: '0'
110
113
  segments:
111
114
  - 0
112
- hash: -3902848851121274865
115
+ hash: 2025653954573289953
113
116
  required_rubygems_version: !ruby/object:Gem::Requirement
114
117
  none: false
115
118
  requirements:
@@ -118,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
121
  version: '0'
119
122
  segments:
120
123
  - 0
121
- hash: -3902848851121274865
124
+ hash: 2025653954573289953
122
125
  requirements: []
123
126
  rubyforge_project:
124
127
  rubygems_version: 1.8.23
@@ -126,4 +129,7 @@ signing_key:
126
129
  specification_version: 3
127
130
  summary: Loads inline template as a Hash
128
131
  test_files:
132
+ - spec/fixtures/dsl_uses_inline_template_loader.rb
133
+ - spec/fixtures/external_file.rb
134
+ - spec/fixtures/external_file_without_templates.rb
129
135
  - spec/lib/inline_template_loader_spec.rb