glia 0.1.0 → 0.1.1

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: 8f3643b91dabc177197be3c240ff6de7c21522c0
4
- data.tar.gz: 3f493ca6458e66cf8f39735f7f2e81b8f960a0e6
3
+ metadata.gz: 9a50686cc03676e699d74a01655c296020b723bb
4
+ data.tar.gz: 4ba937471d4071c387dec39827bb2f5ed0b480e4
5
5
  SHA512:
6
- metadata.gz: ddfebae6ff8ffdb40e9cdfd0cb877714e4dfa68b62668f80b36bc7cf0c9997a7c216e0359dbec91eb98a7098d5239b12538ee7ebcc8afbab4a3c7d1db06b6011
7
- data.tar.gz: 45006a08c0f57dfeec98722b6b81ce960057a032516952bb0037d8befcc55098c24d1305f2727d1948a032fe9f39b1d8613afdacc62676ebd2fb41c5aae31a75
6
+ metadata.gz: bcdd053ca3266f8747da57001154d65c31201bffb43c988d1b63db6277e3dcaf5f5c60732953de6648eb998f9a88e888827903db0a4d20df71249a7f8e1902a8
7
+ data.tar.gz: 8e15f7300f86449b0853e6deff45a87d0ef0998a456399c78fe1b9904c9ef3374da2301808482086878cb409e8c44a150aa54e5bae7b5d40863c1f70b2f6b937
data/README.md CHANGED
@@ -41,9 +41,14 @@ Layout files are written with a DSL that is designed to help describe the layout
41
41
  ```ruby
42
42
  # layout/frontend/default.rb
43
43
  Glia.area(:frontend) do
44
+ view_namespace Fixtures::View
44
45
  handle :default do
45
46
  cell name: :root, class: :'core/html', template_name: 'root', missing_accessor: 'ignore_me' do
46
47
  cell name: :header, class: :template, template_name: 'header'
48
+ cell name: :sidebar, class: :list do
49
+ # ...
50
+ end
51
+ cell name: :footer, class: :template, template_name: 'footer'
47
52
  end
48
53
  end
49
54
  handle :cake_view do
@@ -67,6 +72,15 @@ Glia.area(:frontend) do
67
72
  end
68
73
  ```
69
74
 
75
+ ![Cake View](readme/layout_cake.png?raw=true)
76
+
77
+ ![Pav View](readme/layout_pav.png?raw=true)
78
+
79
+ #### View Namespace (Optional)
80
+
81
+ Pass a class/module. Any cell created within this area definition using a string or symbol for `class`
82
+ will use this namespace when getting the actual class.
83
+
70
84
  #### Handle
71
85
 
72
86
  Handles are key tool for customising your layout per page.
@@ -172,14 +186,6 @@ end
172
186
 
173
187
  The handles will be generated in the controller based on the request/loaded objects/controller. Examples to come.
174
188
 
175
- #### Configure the layout
176
-
177
- If you wish to use class codes in place of classes, Tell it what namespace to use when finding classes for a class code.
178
-
179
- ```ruby
180
- Glia::Layout.view_namespace = Fixtures::View
181
- ```
182
-
183
189
  #### Create a layout
184
190
 
185
191
  Pick an 'area' to render, e.g. `:frontend` or `:admin`, and pass this into the layout method along with the handles.
@@ -224,6 +230,50 @@ Try one or more of these options:
224
230
  pass data to, and the cells can read data from.
225
231
  * If more flexibility is required, the controller can pull a specific cell from the layout, and call a method on it.
226
232
 
233
+ ### Customise the ViewFactory
234
+
235
+ Example for Customised Lotus Views
236
+
237
+ ```ruby
238
+ module Frontend
239
+ class Application < Lotus::Application
240
+
241
+ def renderer
242
+ @custom_renderer ||= Client::RenderingPolicy.new(configuration)
243
+ end
244
+
245
+ configure do
246
+ # ...
247
+ end
248
+
249
+ end
250
+ end
251
+ ```
252
+
253
+ ```ruby
254
+ require 'glia'
255
+ class Client::RenderingPolicy < Lotus::RenderingPolicy
256
+
257
+ class ViewFactory < Glia::ViewFactory
258
+ def instantiate(klass, definition, *args)
259
+ #@todo: Allow blocks/cells to subscribe to registry values.
260
+ locals = args[0].nil? ? definition : definition.merge(args[0])
261
+ klass.new({format: :html}.merge(locals))
262
+ end
263
+ end
264
+
265
+ def _render_action(action, response)
266
+ if successful?(response)
267
+ RequestStore.store[:action_exposures] = action.exposures
268
+ layout = Glia.layout(:frontend, action.handles)
269
+ layout.view_factory = ViewFactory.new
270
+ layout.cell(:root, action.exposures).render
271
+ end
272
+ end
273
+
274
+ end
275
+ ```
276
+
227
277
  ## Notes
228
278
  * When the DSL is parsed, the result is a simple hash of values that describes the layout.
229
279
 
data/lib/glia/cell.rb CHANGED
@@ -1,15 +1,22 @@
1
1
  module Glia
2
2
  module Cell
3
+ attr_accessor :child_definitions, :layout
4
+
3
5
  # To be overloaded, but here to prevent errors if we don't define initialize
4
6
  def initialize(config)
5
7
  end
6
8
 
7
9
  def children
8
10
  @children ||= {}
11
+ child_definitions.keys.each{|p| @children[p] ||= cell(p)} unless child_definitions.nil?
12
+ @children
9
13
  end
10
14
 
11
- def cell(code)
12
- @children[code]
15
+ def cell(code, *args)
16
+ @children ||= {}
17
+ name = child_definitions[code]
18
+ raise Errors::MissingCellError, "No child cell in position #{code}" if name.nil?
19
+ @children[code] ||= layout.cell(name, *args)
13
20
  end
14
21
  end
15
22
  end
data/lib/glia/errors.rb CHANGED
@@ -2,5 +2,9 @@ module Glia
2
2
  module Errors
3
3
  class SyntaxError < StandardError
4
4
  end
5
+ class MissingCellError < StandardError
6
+ end
7
+ class InvalidCellError < StandardError
8
+ end
5
9
  end
6
10
  end
data/lib/glia/layout.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Glia
2
2
 
3
3
  class Layout
4
+ attr_writer :view_factory
4
5
  attr_reader :handles, :data, :update, :cells
5
6
  singleton_class.class_eval do
6
7
  attr_accessor :layout_dir, :view_namespace
@@ -13,20 +14,26 @@ module Glia
13
14
  @data = @update.merge(@handles)
14
15
  end
15
16
 
16
- def cell(name)
17
+ def cell(name, *args)
17
18
  if @cells[name].nil?
18
19
  definition = @data[name]
20
+ raise Errors::MissingCellError, "Cell #{name} is missing from layout" if definition.nil?
19
21
  code = definition.delete(:class)
20
22
  actions = definition.delete(:actions)
21
23
  children = definition.delete(:children)
22
- @cells[name] = view_factory.build(code, definition, actions)
23
- children.each{|p, n| @cells[name].children[p] = cell(n)} unless children.nil?
24
+ namespace = definition.delete(:view_namespace) || Object
25
+ @cells[name] = view_factory.build(code, namespace, definition, actions, *args)
26
+ unless @cells[name].respond_to?(:child_definitions=)
27
+ raise Errors::InvalidCellError, @cells[name].class.name+' is not a valid cell. Include Glia::Cell.'
28
+ end
29
+ @cells[name].child_definitions = children
30
+ @cells[name].layout = self
24
31
  end
25
32
  @cells[name]
26
33
  end
27
34
 
28
35
  def view_factory
29
- @view_factory ||= ViewFactory.new(self.class.view_namespace)
36
+ @view_factory ||= ViewFactory.new
30
37
  end
31
38
 
32
39
  end
@@ -6,6 +6,11 @@ module Glia
6
6
  @data = {}
7
7
  end
8
8
 
9
+ def view_namespace(namespace)
10
+ @view_namespace = namespace
11
+ self
12
+ end
13
+
9
14
  def handle(key, &blk)
10
15
  begin
11
16
  @current_scope = @data[key] ||= {}
@@ -15,10 +20,14 @@ module Glia
15
20
  @current_scope = nil
16
21
  @current_cell = nil
17
22
  end
23
+ self
18
24
  end
19
25
 
20
26
  def cell(definition, &blk)
21
27
  raise Glia::Errors::SyntaxError, 'cell must have a class' if definition[:class].nil?
28
+ # Store the namespace here, for use when building layout.
29
+ # We delay resolving the class, in case a block is not used, or is defined later.
30
+ definition[:view_namespace] = @view_namespace unless @view_namespace.nil?
22
31
  _cell(definition, &blk)
23
32
  end
24
33
 
data/lib/glia/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Glia
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,26 +1,32 @@
1
1
  module Glia
2
2
  class ViewFactory
3
- def initialize(namespace)
4
- @namespace = namespace || Object
3
+
4
+ def build(code, view_namespace = Object, definition = {}, actions = [], *args)
5
+ object = instantiate(find_class(code, view_namespace), definition, *args)
6
+ apply_actions(object, actions)
7
+ object
8
+ end
9
+
10
+ def instantiate(klass, definition, *args)
11
+ klass.new(*(args|[definition]))
5
12
  end
6
13
 
7
- def build(code, definition = {}, actions = [])
8
- object = find_class(code).new(definition)
14
+ def apply_actions(object, actions)
9
15
  actions.each do |action|
10
16
  object.send(action[:name], *action[:args])
11
17
  end unless actions.nil?
12
- object
13
18
  end
14
19
 
15
- def find_class(code)
20
+ def find_class(code, view_namespace = Object)
16
21
  if code.is_a? Symbol
17
22
  parts = code.to_s.split('/').map{ |str| str.split('_').map {|w| w.capitalize}.join }
18
- parts.inject(@namespace){|namespace, part| namespace.const_get(part)}
23
+ parts.inject(view_namespace){|namespace, part| namespace.const_get(part)}
19
24
  elsif code.is_a? String
20
- @namespace.const_get(code)
25
+ view_namespace.const_get(code)
21
26
  else
22
27
  code
23
28
  end
24
29
  end
30
+
25
31
  end
26
32
  end
Binary file
Binary file
@@ -1,4 +1,5 @@
1
1
  Glia.area(:frontend) do
2
+ view_namespace Fixtures::View
2
3
 
3
4
  handle :default do
4
5
  cell name: :root, class: :'core/html', template_name: 'root', missing_accessor: 'ignore_me' do
@@ -2,7 +2,6 @@ require 'test_helper'
2
2
  class Glia::Layout::Test < UnitTest
3
3
  describe Glia::Layout do
4
4
 
5
- Glia::Layout.view_namespace = Fixtures::View
6
5
  let(:layout){ Glia.layout(:frontend, [:default, :cake_view, :pavlova_view]) }
7
6
 
8
7
  def test_update
@@ -1,16 +1,17 @@
1
1
  require 'test_helper'
2
2
  class Glia::ViewFactory::Test < UnitTest
3
3
  describe Glia::ViewFactory do
4
- let(:factory) { Glia::ViewFactory.new(Fixtures::View) }
4
+ let(:factory) { Glia::ViewFactory.new }
5
5
 
6
6
  def test_find_class
7
- assert_equal Fixtures::View::Template, factory.find_class(:template)
8
- assert_equal Fixtures::View::Core::Html, factory.find_class(:'core/html')
7
+ assert_equal Fixtures::View::Template, factory.find_class(:template, Fixtures::View)
8
+ assert_equal Fixtures::View::Core::Html, factory.find_class(:'core/html', Fixtures::View)
9
9
  end
10
10
 
11
11
  def test_build
12
12
  cell = factory.build(
13
13
  :template,
14
+ Fixtures::View,
14
15
  {template_name: 'my_template'},
15
16
  [{name: :add_ingredient, args: ['Eggs', '6 Large']}]
16
17
  )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dane Lowe
@@ -74,6 +74,8 @@ files:
74
74
  - lib/glia/update_registry.rb
75
75
  - lib/glia/version.rb
76
76
  - lib/glia/view_factory.rb
77
+ - readme/layout_cake.png
78
+ - readme/layout_pav.png
77
79
  - test/fixtures/layout.rb
78
80
  - test/fixtures/layout/default.rb
79
81
  - test/fixtures/view.rb