Class: ViewModels::PathStore

Inherits:
Object
  • Object
show all
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)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (PathStore) initialize(view_model_class)

Initialize the path store

Parameters:

  • view_model_class (ViewModel)

    The view model class



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

- (Object) view_model_class (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

+ (Object) install_in(klass)

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

- (Object) [](key)

Simple [] delegation.



60
61
62
# File 'lib/view_models/path_store.rb', line 60

def [] key
  @name_path_mapping[key]
end

- (Object) []=(key, path)

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

- (Object) cached(options, &block)

Cache the result of the rendering.



31
32
33
34
35
# File 'lib/view_models/path_store.rb', line 31

def cached options, &block
  prepare options.path_key
  result = block.call
  save options and result if result
end

- (Object) prepare(key)

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

- (Object) save(options)

Saves the options for the prepared key.



47
48
49
# File 'lib/view_models/path_store.rb', line 47

def save options
  self[@key] = options.file
end