Sha256: 69caa87f2e2c25d5cd6f122aad2e6f602cac658f9e9ee8cb51f1ea643c3c27d2

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

# Base Module for ViewModels.
#
module ViewModels
  
  # 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.
  #
  class PathStore
    
    attr_reader :view_model_class
    
    def initialize view_model_class
      @view_model_class = view_model_class
      @name_path_mapping = {}
    end
    
    # Install in the metaclass (as an example).
    #
    def self.install_in klass
      klass.path_store = PathStore.new klass
    end
    
    # Cache the result of the rendering.
    #
    def cached options, &block
      prepare options.path_key
      result = block.call
      save options and result if result
    end
    
    # Prepare the key for the next storing procedure.
    #
    # Note: If this is nil, the store will not save the path.
    #
    def prepare key
      @key = key
    end
    
    # Saves the options for the prepared key.
    #
    def save options
      self[@key] = options.file
    end
    
    # Does not save values for nil keys.
    #
    def []= key, path
      return if key.nil?
      @name_path_mapping[key] ||= path
    end
    
    # Simple [] delegation.
    #
    def [] key
      @name_path_mapping[key]
    end
    
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
view_models-2.0.1 lib/shared/lib/view_models/path_store.rb
view_models-2.0.0.ruby19 lib/shared/lib/view_models/path_store.rb
view_models-2.0.0 lib/shared/lib/view_models/path_store.rb
view_models-1.5.7 lib/view_models/path_store.rb
view_models-1.5.6 lib/view_models/path_store.rb
view_models-1.5.5 lib/view_models/path_store.rb
view_models-1.5.4 lib/view_models/path_store.rb
view_models-1.5.3 lib/view_models/path_store.rb
view_models-1.5.2 lib/view_models/path_store.rb