Class: Lotus::View::Configuration
- Inherits:
-
Object
- Object
- Lotus::View::Configuration
- Defined in:
- lib/lotus/view/configuration.rb
Overview
Configuration for the framework, controllers and actions.
Lotus::Controller has its own global configuration that can be manipulated via ‘Lotus::View.configure`.
Every time that ‘Lotus::View` and `Lotus::Layout` are included, that global configuration is being copied to the recipient. The copy will inherit all the settings from the original, but all the subsequent changes aren’t reflected from the parent to the children, and viceversa.
This architecture allows to have a global configuration that capture the most common cases for an application, and let views and layouts layouts to specify exceptions.
Constant Summary collapse
- DEFAULT_ROOT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default root
'.'.freeze
Instance Attribute Summary collapse
-
#layout(value = nil) ⇒ Object
readonly
Set the global layout.
- #layouts ⇒ Object readonly
- #load_paths ⇒ Object readonly
- #modules ⇒ Object readonly
-
#namespace(value = nil) ⇒ Object
readonly
Set the Ruby namespace where to lookup for views.
-
#root(value = nil) ⇒ Object
readonly
Set the root path where to search for templates.
- #views ⇒ Object readonly
Class Method Summary collapse
-
.for(base) ⇒ Lotus::Controller::Configuration
private
Return the original configuration of the framework instance associated with the given class.
Instance Method Summary collapse
-
#add_layout(layout) ⇒ Object
private
Add a layout to the registry.
-
#add_view(view) ⇒ Object
private
Add a view to the registry.
-
#copy!(base) ⇒ Object
private
Copy the configuration for the given action.
-
#duplicate ⇒ Lotus::View::Configuration
private
Duplicate by copying the settings in a new instance.
-
#initialize ⇒ Lotus::View::Configuration
constructor
Initialize a configuration instance.
-
#load! ⇒ Object
private
Load the configuration for the current framework.
-
#prepare(&blk) ⇒ void
Prepare the views.
-
#reset! ⇒ Object
(also: #unload!)
private
Reset all the values to the defaults.
Constructor Details
#initialize ⇒ Lotus::View::Configuration
Initialize a configuration instance
96 97 98 99 |
# File 'lib/lotus/view/configuration.rb', line 96 def initialize @namespace = Object reset! end |
Instance Attribute Details
#layout(value) ⇒ Object #layout ⇒ Class
Set the global layout
If not set, this value defaults to ‘nil`, while at the rendering time it will use `Lotus::View::Rendering::NullLayout`.
This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.
232 233 234 235 236 237 238 |
# File 'lib/lotus/view/configuration.rb', line 232 def layout(value = nil) if value.nil? Rendering::LayoutFinder.find(@layout, @namespace) else @layout = value end end |
#layouts ⇒ Object (readonly)
34 35 36 |
# File 'lib/lotus/view/configuration.rb', line 34 def layouts @layouts end |
#load_paths ⇒ Object
32 33 34 |
# File 'lib/lotus/view/configuration.rb', line 32 def load_paths @load_paths end |
#modules ⇒ Object
35 36 37 |
# File 'lib/lotus/view/configuration.rb', line 35 def modules @modules end |
#namespace(value) ⇒ Object #namespace ⇒ Class, ...
Set the Ruby namespace where to lookup for views.
When multiple instances of the framework are used, we want to make sure that if a ‘MyApp` wants a `Dashboard::Index` view, we are loading the right one.
If not set, this value defaults to ‘Object`.
This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.
134 135 136 137 138 139 140 |
# File 'lib/lotus/view/configuration.rb', line 134 def namespace(value = nil) if value @namespace = value else @namespace end end |
#root(value) ⇒ Object #root ⇒ Pathname
Set the root path where to search for templates
If not set, this value defaults to the current directory.
This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.
179 180 181 182 183 184 185 |
# File 'lib/lotus/view/configuration.rb', line 179 def root(value = nil) if value @root = Utils::Kernel.Pathname(value).realpath else @root end end |
#views ⇒ Object (readonly)
33 34 35 |
# File 'lib/lotus/view/configuration.rb', line 33 def views @views end |
Class Method Details
.for(base) ⇒ Lotus::Controller::Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the original configuration of the framework instance associated with the given class.
When multiple instances of Lotus::View are used in the same application, we want to make sure that a controller or an action will receive the expected configuration.
84 85 86 87 88 89 |
# File 'lib/lotus/view/configuration.rb', line 84 def self.for(base) # TODO this implementation is similar to Lotus::Controller::Configuration consider to extract it into Lotus::Utils namespace = Utils::String.new(base).namespace framework = Utils::Class.load_from_pattern!("(#{namespace}|Lotus)::View") framework.configuration end |
Instance Method Details
#add_layout(layout) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add a layout to the registry
338 339 340 |
# File 'lib/lotus/view/configuration.rb', line 338 def add_layout(layout) @layouts.add(layout) end |
#add_view(view) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add a view to the registry
330 331 332 |
# File 'lib/lotus/view/configuration.rb', line 330 def add_view(view) @views.add(view) end |
#copy!(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Copy the configuration for the given action
390 391 392 393 394 |
# File 'lib/lotus/view/configuration.rb', line 390 def copy!(base) modules.each do |mod| base.class_eval(&mod) end end |
#duplicate ⇒ Lotus::View::Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Duplicate by copying the settings in a new instance.
348 349 350 351 352 353 354 355 356 |
# File 'lib/lotus/view/configuration.rb', line 348 def duplicate Configuration.new.tap do |c| c.namespace = namespace c.root = root c.layout = @layout # lazy loading of the class c.load_paths = load_paths.dup c.modules = modules.dup end end |
#load! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load the configuration for the current framework
362 363 364 365 366 |
# File 'lib/lotus/view/configuration.rb', line 362 def load! views.each { |v| v.__send__(:load!) } layouts.each { |l| l.__send__(:load!) } freeze end |
#prepare(&blk) ⇒ void
This method returns an undefined value.
Prepare the views.
The given block will be yielded when ‘Lotus::View` will be included by a view.
This method can be called multiple times.
318 319 320 321 322 323 324 |
# File 'lib/lotus/view/configuration.rb', line 318 def prepare(&blk) if block_given? @modules.push(blk) else raise ArgumentError.new('Please provide a block') end end |
#reset! ⇒ Object Also known as: unload!
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reset all the values to the defaults
372 373 374 375 376 377 378 379 380 |
# File 'lib/lotus/view/configuration.rb', line 372 def reset! root(DEFAULT_ROOT) @views = Set.new @layouts = Set.new @load_paths = Utils::LoadPaths.new(root) @layout = nil @modules = [] end |