end_view 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13f724862bcef543e1ca3548cd79e9ede0210500
4
- data.tar.gz: 2711de7e149ee22d53f4837329d8ec84221dc445
3
+ metadata.gz: 04ae1eb2e4ae0c22804eba2dbb8b90ccf3ca8ced
4
+ data.tar.gz: 1bfd69e32013d5579fd689f9dfe61279e646e20c
5
5
  SHA512:
6
- metadata.gz: 56e62c0c920d3598df45cdfcf46f13d73e94820dc4b1caa3c87317ba9154dcfe012634900cdc9914fcf5aaf619b483601f54a3fb094eaf24ce58f30385eef805
7
- data.tar.gz: 65097ea3bbb3c75ccbb88fb5314e1fbc6d396a534b73f54ac20a7ca7624e07579e9afafa38e75ac2c949ed94b09b285b77415394f24fa39acee644bf3568941c
6
+ metadata.gz: ffe6ccde812215302b0de1f56e76af43f4d9228085193e6b31939210d5c6d1b817842eadbedd89e81e85b6767d9493a2f2267a1de49a1a4c125d4d0e41d99097
7
+ data.tar.gz: 0a18ab55eaa81b389365dfda4c61a697d10d6fa094815121820adb3dacfbecdfe3e158ea09f55488f20844178ec923d765e3bbb09c8a8a4b969d020dc7748973
data/README.md CHANGED
@@ -3,17 +3,17 @@
3
3
  [![Build Status](https://travis-ci.org/mushishi78/end_view.svg?branch=master)](https://travis-ci.org/mushishi78/end_view)
4
4
  [![Gem Version](https://badge.fury.io/rb/end_view.svg)](http://badge.fury.io/rb/end_view)
5
5
 
6
- View model and template in same file using the [tilt gem](https://github.com/rtomayko/tilt).
6
+ View model and template in same file.
7
7
 
8
8
  ## Usage
9
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__`.
10
+ Given a ruby file with a template written after `__END__`, the `EndView` mixin will provide a `render` method to execute it:
11
11
 
12
12
  ``` ruby
13
13
  require 'end_view'
14
14
 
15
15
  class Foo
16
- include EndView.new(__FILE__)
16
+ include EndView
17
17
 
18
18
  def my_method
19
19
  'Hello World'
@@ -27,11 +27,11 @@ __END__
27
27
  %h1= my_method
28
28
  ```
29
29
 
30
- Alternatively, an instance of `EndView` can be extended:
30
+ `EndView` can also be use to extend an object:
31
31
 
32
32
  ``` ruby
33
33
  module Baz
34
- extend EndView.new(__FILE__)
34
+ extend EndView
35
35
 
36
36
  def self.my_method
37
37
  'Howdy World'
@@ -47,13 +47,19 @@ __END__
47
47
 
48
48
  ### Template Engine
49
49
 
50
- By default Tilt's Haml template engine is used, but alternative engines can be passed in:
50
+ By default Tilt's Haml template engine is used. To change the default engine:
51
+
52
+ ``` ruby
53
+ EndView.default_engine = Tilt::ERBTemplate
54
+ ```
55
+
56
+ Or to specify an engine for a particular class:
51
57
 
52
58
  ``` ruby
53
59
  require 'tilt/erb'
54
60
 
55
61
  class Ham
56
- include EndView.new(__FILE__, Tilt::ERBTemplate)
62
+ include EndView.new(template_engine: Tilt::ERBTemplate)
57
63
 
58
64
  def my_method
59
65
  'Heya'
@@ -67,19 +73,13 @@ __END__
67
73
  <h1><%= my_method %></h1>
68
74
  ```
69
75
 
70
- To change the default engine:
71
-
72
- ``` ruby
73
- EndView.default_engine = Tilt::LiquidTemplate
74
- ```
75
-
76
76
  ### Layouts
77
77
 
78
78
  For template engines that support it, view models can be passed blocks:
79
79
 
80
80
  ``` ruby
81
81
  module MyLayout
82
- extend EndView.new(__FILE__)
82
+ extend EndView
83
83
  end
84
84
 
85
85
  MyLayout.render { "S'up" } # <html>S'up</html>
@@ -94,8 +94,8 @@ These can then be used as layouts:
94
94
 
95
95
  ``` ruby
96
96
  class Fizz
97
- include EndView.new(__FILE__)
98
- self.layout = MyLayout
97
+ include EndView
98
+ layout MyLayout
99
99
  end
100
100
 
101
101
  Fizz.new.render # <html><h1>Goodbye</h1></html>
@@ -105,11 +105,11 @@ __END__
105
105
  %h1 Goodbye
106
106
  ```
107
107
 
108
- For layouts that need to be dynamically initialized, `self.layout` can be passed a lambda:
108
+ For layouts that need to be dynamically initialized, `layout` can be passed a lambda:
109
109
 
110
110
  ``` ruby
111
111
  class MyDynamicLayout
112
- include EndView.new(__FILE__)
112
+ include EndView
113
113
 
114
114
  def initialize(title)
115
115
  @title = title
@@ -125,8 +125,8 @@ __END__
125
125
 
126
126
  ``` ruby
127
127
  class Whizz
128
- include EndView.new(__FILE__)
129
- self.layout = -> { MyDynamicLayout.new('Hallo') }
128
+ include EndView
129
+ layout -> { MyDynamicLayout.new('Hallo') }
130
130
  end
131
131
 
132
132
  Whizz.new.render # <html> <h1>Hallo</h1> <p>Bonjour</p> </html>
@@ -150,11 +150,11 @@ end
150
150
  Bar.new.render # <h1>Porridge</h1>
151
151
  ```
152
152
 
153
- To override an inherited template, call the `compile` class method:
153
+ To override an inherited template, call the `compile_template` class method:
154
154
 
155
155
  ``` ruby
156
156
  class Pop < Foo
157
- compile(__FILE__)
157
+ compile_template
158
158
  end
159
159
 
160
160
  Pop.new.render # <p class="inherited">Hello World</p>
@@ -164,7 +164,7 @@ __END__
164
164
  %p.inherited= my_method
165
165
  ```
166
166
 
167
- ### locals
167
+ ### Locals
168
168
 
169
169
  Locals can be passed into the render method that override the view models:
170
170
 
@@ -3,13 +3,13 @@ require 'attire'
3
3
  require 'inflecto'
4
4
 
5
5
  module EndView
6
- def self.bootstrap_form_group(*args)
7
- Bootstrap::FormGroup.render(*args)
8
- end
9
-
10
6
  module Bootstrap
7
+ def self.form_group(*args)
8
+ FormGroup.render(*args)
9
+ end
10
+
11
11
  class FormGroup
12
- include EndView.new(__FILE__)
12
+ include EndView
13
13
  attr_method :render, :form_builder, :attribute, required: false,
14
14
  label: nil,
15
15
  left: nil,
@@ -1,13 +1,13 @@
1
1
  require 'end_view/bootstrap/modal'
2
2
 
3
3
  module EndView
4
- def self.bootstrap_form_modal(*args, &b)
5
- Bootstrap::FormModal.render(*args, &b)
6
- end
7
-
8
4
  module Bootstrap
5
+ def self.form_modal(*args, &b)
6
+ FormModal.render(*args, &b)
7
+ end
8
+
9
9
  class FormModal
10
- include EndView.new(__FILE__)
10
+ include EndView
11
11
 
12
12
  def self.render(*args, &b)
13
13
  new(*args).render(&b)
@@ -2,13 +2,13 @@ require 'end_view'
2
2
  require 'attire'
3
3
 
4
4
  module EndView
5
- def self.bootstrap_modal(*args, &b)
6
- Bootstrap::Modal.render(*args, &b)
7
- end
8
-
9
5
  module Bootstrap
6
+ def self.modal(*args, &b)
7
+ Modal.render(*args, &b)
8
+ end
9
+
10
10
  class Modal
11
- include EndView.new(__FILE__)
11
+ include EndView
12
12
  attr_init :id, title: nil,
13
13
  size: nil,
14
14
  label: nil,
@@ -4,19 +4,19 @@ require 'end_view/bootstrap/form_group'
4
4
  require 'end_view/form/record_builder'
5
5
 
6
6
  module EndView
7
- def self.bootstrap_simple_form(*args)
8
- Bootstrap::SimpleForm.render(*args)
9
- end
10
-
11
7
  module Bootstrap
8
+ def self.simple_form(*args)
9
+ SimpleForm.render(*args)
10
+ end
11
+
12
12
  class SimpleForm
13
- include EndView.new(__FILE__)
13
+ include EndView
14
14
  attr_method :render, :url_or_record, :auth_token, :attributes, :'opts = {}'
15
15
 
16
16
  private
17
17
 
18
18
  def form_builder
19
- @form_builder ||= EndView.form_builder(url_or_record, auth_token, opts)
19
+ @form_builder ||= Form.builder(url_or_record, auth_token, opts)
20
20
  end
21
21
  alias_method :f, :form_builder
22
22
 
@@ -3,11 +3,11 @@ require 'end_view/bootstrap/form_group'
3
3
  require 'end_view/form'
4
4
 
5
5
  module EndView
6
- def self.bootstrap_simple_form_modal(*args)
7
- Bootstrap::SimpleFormModal.render(*args)
8
- end
9
-
10
6
  module Bootstrap
7
+ def self.simple_form_modal(*args)
8
+ SimpleFormModal.render(*args)
9
+ end
10
+
11
11
  class SimpleFormModal
12
12
  attr_method :render, :id,
13
13
  :url_or_record,
@@ -25,7 +25,7 @@ module EndView
25
25
  private
26
26
 
27
27
  def form_builder
28
- @form_builder ||= EndView.form_builder(url_or_record, auth_token, form_opts)
28
+ @form_builder ||= Form.builder(url_or_record, auth_token, form_opts)
29
29
  end
30
30
 
31
31
  def form_group(attribute)
@@ -2,11 +2,11 @@ require 'attire'
2
2
  require 'inflecto'
3
3
 
4
4
  module EndView
5
- def self.bootstrap_tab_pane(*args, &b)
6
- Bootstrap::TabPane.new(*args, &b)
7
- end
8
-
9
5
  module Bootstrap
6
+ def self.tab_pane(*args, &b)
7
+ TabPane.new(*args, &b)
8
+ end
9
+
10
10
  class TabPane
11
11
  attr_init :id, { content: nil, label: nil }, :'&content_block'
12
12
  public :id
@@ -3,13 +3,13 @@ require 'attire'
3
3
  require 'end_view/bootstrap/tab_pane'
4
4
 
5
5
  module EndView
6
- def self.bootstrap_tabpanel(*args)
7
- Bootstrap::Tabpanel.new(*args)
8
- end
9
-
10
6
  module Bootstrap
7
+ def self.tabpanel(*args)
8
+ Tabpanel.new(*args)
9
+ end
10
+
11
11
  class Tabpanel
12
- include EndView.new(__FILE__)
12
+ include EndView
13
13
  attr_init id: nil, panes: [], fade: false, active_pane: 0
14
14
  public :panes
15
15
 
@@ -1,11 +1,11 @@
1
1
  require 'attire'
2
2
 
3
3
  module EndView
4
- def self.form_builder(*args)
5
- Form::Builder.new(*args)
6
- end
7
-
8
4
  module Form
5
+ def self.builder(*args)
6
+ Builder.new(*args)
7
+ end
8
+
9
9
  class Builder
10
10
  attr_init :form_url, :auth_token, form_method: 'post'
11
11
 
@@ -2,12 +2,12 @@ require 'inflecto'
2
2
  require 'end_view/form/builder'
3
3
 
4
4
  module EndView
5
- def self.form_builder(url_or_record, *args)
6
- builder_class = url_or_record.is_a?(String) ? Form::Builder : Form::RecordBuilder
7
- builder_class.new(url_or_record, *args)
8
- end
9
-
10
5
  module Form
6
+ def self.builder(url_or_record, *args)
7
+ builder_class = url_or_record.is_a?(String) ? Builder : RecordBuilder
8
+ builder_class.new(url_or_record, *args)
9
+ end
10
+
11
11
  class RecordBuilder < Builder
12
12
  attr_init :record, :auth_token, model_name: nil,
13
13
  model_id: nil,
@@ -3,19 +3,19 @@ require 'inflecto'
3
3
  require 'end_view/form/record_builder'
4
4
 
5
5
  module EndView
6
- def self.simple_form(*args)
7
- Form::SimpleForm.render(*args)
8
- end
9
-
10
6
  module Form
7
+ def self.simple_form(*args)
8
+ SimpleForm.render(*args)
9
+ end
10
+
11
11
  class SimpleForm
12
- include EndView.new(__FILE__)
12
+ include EndView
13
13
  attr_method :render, :url_or_record, :auth_token, :attributes, :'opts = {}'
14
14
 
15
15
  private
16
16
 
17
17
  def form_builder
18
- @form_builder ||= EndView.form_builder(url_or_record, auth_token, opts)
18
+ @form_builder ||= Form.builder(url_or_record, auth_token, opts)
19
19
  end
20
20
  alias_method :f, :form_builder
21
21
 
@@ -2,13 +2,13 @@ require 'end_view'
2
2
  require 'attire'
3
3
 
4
4
  module EndView
5
- def self.rails_layout(*args)
6
- Rails::Layout.new(*args)
7
- end
8
-
9
5
  module Rails
6
+ def self.layout(*args)
7
+ Layout.new(*args)
8
+ end
9
+
10
10
  class Layout
11
- include EndView.new(__FILE__)
11
+ include EndView
12
12
  attr_init :view_context, :title, head: nil
13
13
 
14
14
  private
data/lib/end_view.rb CHANGED
@@ -9,50 +9,72 @@ module EndView
9
9
  @default_engine ||= Tilt::HamlTemplate
10
10
  end
11
11
 
12
- def new(file, template_engine = default_engine)
12
+ def included(base, opts = {})
13
+ base.extend ClassMethods
14
+ base.compile_template(opts.merge(start: 4))
15
+ base.send(:include, InstanceMethods)
16
+ base.send(:include, Methods)
17
+ end
18
+
19
+ def extended(base, opts = {})
20
+ base.extend ClassMethods
21
+ base.compile_template(opts.merge(start: 4))
22
+ base.extend(Methods)
23
+ end
24
+
25
+ def new(opts = {})
13
26
  Module.new do
14
- define_singleton_method(:included) do |base|
15
- base.extend ClassMethods
16
- base.compile(file, template_engine)
17
- base.send(:include, Methods)
18
- base.send(:include, InstanceMethods)
19
- end
20
-
21
- define_singleton_method(:extended) do |base|
22
- base.extend ClassMethods
23
- base.compile(file, template_engine)
24
- base.extend Methods
25
- end
27
+ define_singleton_method(:included) { |base| EndView.included(base, opts) }
28
+ define_singleton_method(:extended) { |base| EndView.extended(base, opts) }
26
29
  end
27
30
  end
28
31
  end
29
32
 
30
33
  module ClassMethods
34
+ attr_accessor :template
35
+
31
36
  def inherited(child)
32
37
  super
33
38
  child.template = template
34
- child.layout = layout
39
+ child.layout(retrieve_layout)
40
+ end
41
+
42
+ def compile_template(opts = {})
43
+ file = opts[:file] || caller_file(opts[:start] || 3)
44
+ template_engine = opts[:template_engine] || EndView.default_engine
45
+ @template = template_engine.new(file) { data(file) }
35
46
  end
36
47
 
37
- def compile(file, template_engine = EndView.default_engine)
38
- data = IO.read(file).gsub("\r\n", "\n").split(/^__END__$/).last
39
- @template = template_engine.new(file) { data }
48
+ def layout(layout)
49
+ @layout = layout
40
50
  end
41
51
 
42
- attr_accessor :layout, :template
52
+ def retrieve_layout
53
+ @layout
54
+ end
55
+
56
+ private
57
+
58
+ def caller_file(start)
59
+ caller(start, 1).first.split(/:\d*:in/).first
60
+ end
61
+
62
+ def data(file)
63
+ IO.read(file).gsub("\r\n", "\n").split(/^__END__$/).last
64
+ end
43
65
  end
44
66
 
45
67
  module Methods
46
68
  def render(*args, &b)
47
69
  rendered_template = template.render(self, *args, &b)
48
- layout = self.layout
70
+ layout = retrieve_layout
49
71
  layout ? layout.render { rendered_template } : rendered_template
50
72
  end
51
73
  end
52
74
 
53
75
  module InstanceMethods
54
- def layout
55
- layout = self.class.layout
76
+ def retrieve_layout
77
+ layout = self.class.retrieve_layout
56
78
  layout.respond_to?(:call) ? instance_exec(&layout) : layout
57
79
  end
58
80
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: end_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max White
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-10 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tilt