end_view 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +153 -0
  4. data/lib/end_view.rb +62 -0
  5. metadata +106 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4af64ce4fffb44629f4196c08bfc6d9860776c7
4
+ data.tar.gz: 5ebd00705ab5e7e0f05984acbf3e85d3af79b4fe
5
+ SHA512:
6
+ metadata.gz: 05d47cef58db14ec29aa8a955697789bc0c1ff0df3c9879172cbab003796f4195df638c7f62809d54b2bd9aa208f9b750c946a7bed5d7cdff314e7ac4cb58d15
7
+ data.tar.gz: 33bef9d3cb91d79eb7d17922ea7170f1c7373f8130720222dbd5e4b2fa089884e24be582d1134ef11b4478efe7acd836e0c1d20b3e124edb4c3a603a4e164878
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Max White
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # EndView
2
+
3
+ [![Build Status](https://travis-ci.org/mushishi78/end_view.svg?branch=master)](https://travis-ci.org/mushishi78/end_view)
4
+ [![Gem Version](https://badge.fury.io/rb/end_view.svg)](http://badge.fury.io/rb/end_view)
5
+
6
+ View model and template in same file using the [tilt gem](https://github.com/rtomayko/tilt).
7
+
8
+ ## Usage
9
+
10
+ To use, include a new instance of `EndView`, passing `__File__` into the constructor and put the template at the bottom of the file after `__END__`.
11
+
12
+ ``` ruby
13
+ require 'end_view'
14
+
15
+ class Foo
16
+ include EndView.new(__FILE__)
17
+
18
+ def my_method
19
+ 'World'
20
+ end
21
+ end
22
+
23
+ Foo.new.render # Hello World
24
+
25
+ __END__
26
+
27
+ Hello <%= my_method %>
28
+ ```
29
+
30
+ Alternatively, an instance of `EndView` can be extended:
31
+
32
+ ``` ruby
33
+ module Baz
34
+ extend EndView.new(__FILE__)
35
+
36
+ def self.my_method
37
+ 'World'
38
+ end
39
+ end
40
+
41
+ Baz.render # Howdy World
42
+
43
+ __END__
44
+
45
+ Howdy <%= my_method %>
46
+ ```
47
+
48
+ ### Layouts
49
+
50
+ For template engines that support it, view models can passed blocks:
51
+
52
+ ``` ruby
53
+ module MyLayout
54
+ extend EndView.new(__FILE__)
55
+ end
56
+
57
+ MyLayout.render { "S'up" } # <html>S'up</html>
58
+
59
+ __END__
60
+
61
+ <html><%= yield %></html>
62
+ ```
63
+
64
+ These can then be used as layouts:
65
+
66
+ ``` ruby
67
+ class Fizz
68
+ include EndView.new(__FILE__)
69
+ self.layout = MyLayout
70
+
71
+ def my_method
72
+ 'World'
73
+ end
74
+ end
75
+
76
+ Fizz.new.render # <html>Goodbye World</html>
77
+
78
+ __END__
79
+
80
+ Goodbye <%= my_method %>
81
+ ```
82
+
83
+ ### Inheritance
84
+
85
+ Templates are inherited:
86
+
87
+ ``` ruby
88
+ class Bar < Foo
89
+ def my_method
90
+ 'Porridge'
91
+ end
92
+ end
93
+
94
+ Bar.new.render # Hello Porridge
95
+ ```
96
+
97
+ To override an inherited template, call the `compile` class method:
98
+
99
+ ``` ruby
100
+ class Pop < Foo
101
+ compile(__FILE__)
102
+ end
103
+
104
+ Pop.new.render # The World is too big!
105
+
106
+ __END__
107
+
108
+ The <%= my_method %> is too big!
109
+ ```
110
+
111
+ ### Template Engine
112
+
113
+ By default Tilt's ERB template engine is used, but alternative engines can be passed in:
114
+
115
+ ``` ruby
116
+ require 'tilt/haml'
117
+
118
+ class Ham
119
+ include EndView.new(__FILE__, Tilt::HamlTemplate)
120
+
121
+ def my_method
122
+ 'Heya'
123
+ end
124
+ end
125
+
126
+ Ham.new.render # <h1>Heya</h1>
127
+
128
+ __END__
129
+
130
+ %h1= my_method
131
+ ```
132
+
133
+ To change the default engine:
134
+
135
+ ``` ruby
136
+ EndView.default_engine = Tilt::LiquidTemplate
137
+ ```
138
+
139
+ ## Installation
140
+
141
+ Add your Gemfile:
142
+
143
+ ```ruby
144
+ gem 'end_view'
145
+ ```
146
+
147
+ ## Contributing
148
+
149
+ 1. Fork it ( https://github.com/[my-github-username]/end_view/fork )
150
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
151
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
152
+ 4. Push to the branch (`git push origin my-new-feature`)
153
+ 5. Create a new Pull Request
data/lib/end_view.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'tilt'
2
+
3
+ class EndView < Module
4
+ class << self
5
+ attr_writer :default_engine
6
+
7
+ def default_engine
8
+ @default_engine ||= Tilt::ERBTemplate
9
+ end
10
+ end
11
+
12
+ def initialize(file, template_engine = EndView.default_engine)
13
+ define_singleton_method(:included) do |base|
14
+ base.extend ClassMethods
15
+ base.compile(file, template_engine)
16
+ base.send(:include, Methods)
17
+ base.send(:include, InstanceMethods)
18
+ end
19
+
20
+ define_singleton_method(:extended) do |base|
21
+ base.extend ClassMethods
22
+ base.compile(file, template_engine)
23
+ base.extend Methods
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ def inherited(child)
29
+ child.template = template
30
+ child.layout = layout
31
+ end
32
+
33
+ def compile(file, template_engine = EndView.default_engine)
34
+ @template = template_engine.new { data(file) }
35
+ end
36
+
37
+ attr_accessor :layout, :template
38
+
39
+ private
40
+
41
+ def data(file)
42
+ IO.read(file).gsub("\r\n", "\n").split(/^__END__$/)[1]
43
+ end
44
+ end
45
+
46
+ module Methods
47
+ def render(&b)
48
+ rendered_template = template.render(self, &b)
49
+ layout ? layout.render { rendered_template } : rendered_template
50
+ end
51
+ end
52
+
53
+ module InstanceMethods
54
+ def layout
55
+ self.class.layout
56
+ end
57
+
58
+ def template
59
+ self.class.template
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: end_view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Max White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tilt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '3.1'
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 3.1.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '3.1'
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 3.1.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: haml
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '4.0'
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 4.0.5
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '4.0'
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: 4.0.5
73
+ description:
74
+ email: mushishi78@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - LICENSE.txt
80
+ - README.md
81
+ - lib/end_view.rb
82
+ homepage: https://github.com/mushishi78/end_view
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: View model and template in same file.
106
+ test_files: []