<commit_before># Base Module for ViewModels.
#
module ViewModels
  
  # Gets raised when render_as, render_the, or render_template cannot
  # find the named template, not even in the hierarchy.
  #
  class MissingTemplateError < StandardError; end
  
  # Base class from which all view_models inherit.
  #
  class Base
    
    # Create a view_model. To create a view_model, you need to have a model (to present) and a context.
    # The context is usually a view, a controller, or an app, but doesn't need to be.
    # 
    # TODO Include this.
    # 
    def initialize model, app_or_controller_or_view
      @model   = model
      @context = ContextExtractor.new(app_or_controller_or_view).extract
    end
    
    class << self
      
      # Installs a path store, a specific store for
      # template inheritance, to remember specific
      # [path, name, format] tuples, pointing to a template path,
      # so the view models don't have to traverse the inheritance chain always.
      #
      attr_accessor :path_store
      def inherited subclass
        ViewModels::PathStore.install_in subclass
        super
      end
      
      # Delegates method calls to the context.
      #
      # Examples:
      #   context_method :current_user
      #   context_method :current_user, :current_profile  # multiple methods to be delegated
      #
      # In the view_model:
      #   self.current_user
      # will call
      #   context.current_user
      #
      def context_method *methods
        delegate *methods << { :to => :context }
      end
      
      # Installs the model_reader Method for filtered
      # model method delegation.
      #
      include Extensions::ModelReader
      
      protected
        
        # Hierarchical rendering.
        #
        
        # Returns the next view model class in the render hierarchy.
        #
        # Note: Just returns the superclass.
        #
        # TODO Think about raising the MissingTemplateError here.
        #
        def next_in_render_hierarchy
          superclass
        end
        # Just raises a fitting template error.
        #
        def raise_template_error_with message
          raise MissingTemplateError.new "No template #{message} found."
        end
        # Check if the view lookup inheritance chain has ended.
        #
        # Raises a MissingTemplateError if yes.
        #
        def inheritance_chain_ends?
           self == ViewModels::Base
        end
        
        
        # Template finding
        #
        
        # Returns a template path for the view with the given options.
        #
        # If no template is found, traverses up the inheritance chain.
        #
        # Raises a MissingTemplateError if none is found during<commit_msg>+ parentheses on raise<commit_after># Base Module for ViewModels.
#
module ViewModels
  
  # Gets raised when render_as, render_the, or render_template cannot
  # find the named template, not even in the hierarchy.
  #
  class MissingTemplateError < StandardError; end
  
  # Base class from which all view_models inherit.
  #
  class Base
    
    # Create a view_model. To create a view_model, you need to have a model (to present) and a context.
    # The context is usually a view, a controller, or an app, but doesn't need to be.
    # 
    # TODO Include this.
    # 
    def initialize model, app_or_controller_or_view
      @model   = model
      @context = ContextExtractor.new(app_or_controller_or_view).extract
    end
    
    class << self
      
      # Installs a path store, a specific store for
      # template inheritance, to remember specific
      # [path, name, format] tuples, pointing to a template path,
      # so the view models don't have to traverse the inheritance chain always.
      #
      attr_accessor :path_store
      def inherited subclass
        ViewModels::PathStore.install_in subclass
        super
      end
      
      # Delegates method calls to the context.
      #
      # Examples:
      #   context_method :current_user
      #   context_method :current_user, :current_profile  # multiple methods to be delegated
      #
      # In the view_model:
      #   self.current_user
      # will call
      #   context.current_user
      #
      def context_method *methods
        delegate *methods << { :to => :context }
      end
      
      # Installs the model_reader Method for filtered
      # model method delegation.
      #
      include Extensions::ModelReader
      
      protected
        
        # Hierarchical rendering.
        #
        
        # Returns the next view model class in the render hierarchy.
        #
        # Note: Just returns the superclass.
        #
        # TODO Think about raising the MissingTemplateError here.
        #
        def next_in_render_hierarchy
          superclass
        end
        # Just raises a fitting template error.
        #
        def raise_template_error_with message
          raise MissingTemplateError.new("No template #{message} found.")
        end
        # Check if the view lookup inheritance chain has ended.
        #
        # Raises a MissingTemplateError if yes.
        #
        def inheritance_chain_ends?
           self == ViewModels::Base
        end
        
        
        # Template finding
        #
        
        # Returns a template path for the view with the given options.
        #
        # If no template is found, traverses up the inheritance chain.
        #
        # Raises a MissingTemplateError if none is found during
{'fraction_non_alphanumeric': 0.05284992784992785, 'fraction_numerical': 0.0, 'mean_word_length': 1.9479000531632111, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504264', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1417, 'n_words': 535}