Class: ViewModels::PathStore
- Inherits:
-
Object
- Object
- ViewModels::PathStore
- Defined in:
- lib/view_models/path_store.rb
Overview
A simple path store. Designed to remove a bit of complexity from the base view model.
Use it to install an instance in the metaclass.
Instance Attribute Summary collapse
-
#view_model_class ⇒ Object
readonly
The view model class attribute.
Class Method Summary collapse
-
.install_in(klass) ⇒ Object
Install in the metaclass (as an example).
Instance Method Summary collapse
-
#[](key) ⇒ Object
Simple [] delegation.
-
#[]=(key, path) ⇒ Object
Does not save values for nil keys.
-
#cached(options, &block) ⇒ Object
Cache the result of the rendering.
-
#initialize(view_model_class) ⇒ PathStore
constructor
Initialize the path store.
-
#prepare(key) ⇒ Object
Prepare the key for the next storing procedure.
-
#save(options) ⇒ Object
Saves the options for the prepared key.
Constructor Details
#initialize(view_model_class) ⇒ PathStore
Initialize the path store
18 19 20 21 |
# File 'lib/view_models/path_store.rb', line 18 def initialize view_model_class @view_model_class = view_model_class @name_path_mapping = {} end |
Instance Attribute Details
#view_model_class ⇒ Object (readonly)
The view model class attribute
13 14 15 |
# File 'lib/view_models/path_store.rb', line 13 def view_model_class @view_model_class end |
Class Method Details
.install_in(klass) ⇒ Object
Install in the metaclass (as an example).
25 26 27 |
# File 'lib/view_models/path_store.rb', line 25 def self.install_in klass klass.path_store = PathStore.new klass end |
Instance Method Details
#[](key) ⇒ Object
Simple [] delegation.
60 61 62 |
# File 'lib/view_models/path_store.rb', line 60 def [] key @name_path_mapping[key] end |
#[]=(key, path) ⇒ Object
Does not save values for nil keys.
53 54 55 56 |
# File 'lib/view_models/path_store.rb', line 53 def []= key, path return if key.nil? @name_path_mapping[key] ||= path end |
#cached(options, &block) ⇒ Object
Cache the result of the rendering.
31 32 33 34 35 |
# File 'lib/view_models/path_store.rb', line 31 def cached , &block prepare .path_key result = block.call save and result if result end |
#prepare(key) ⇒ Object
Note:
If this is nil, the store will not save the path.
Prepare the key for the next storing procedure.
41 42 43 |
# File 'lib/view_models/path_store.rb', line 41 def prepare key @key = key end |
#save(options) ⇒ Object
Saves the options for the prepared key.
47 48 49 |
# File 'lib/view_models/path_store.rb', line 47 def save self[@key] = .file end |