Sha256: 3fdf305e98a8f2bdd6c3b14bb05d870d0e2e95049d197e96f520274850b7e366

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require 'pathname'

module ::AmberComponent
  # Provides methods concerning view registering and rendering.
  module Views
    # @return [Regexp]
    VIEW_FILE_REGEXP  = /^view\./.freeze

    # Class methods for views.
    module ClassMethods
      # Register an inline view by returning a String from the passed block.
      #
      # Usage:
      #
      #   view <<~ERB
      #     <h1>
      #       Hello <%= @name %>
      #     </h1>
      #   ERB
      #
      # or:
      #
      #   view <<~HAML, type: :haml
      #     %h1
      #       Hello
      #       = @name
      #   HAML
      #
      # @param content [String, Proc]
      # @param type [Symbol]
      # @return [void]
      def view(content, type: :erb)
        @method_view = TypedContent.new(type: type, content: content)
      end

      # ERB/Haml/Slim view registered through the `view` method.
      #
      # @return [TypedContent]
      attr_reader :method_view

      # @return [String]
      def view_template_source
        return @method_view.to_s if @method_view

        ::File.read(view_path)
      end

      # @return [String, nil]
      def view_path
        asset_path view_file_name
      end

      # @return [String, nil]
      def view_file_name
        files = asset_file_names(VIEW_FILE_REGEXP)
        raise MultipleViewsError, "More than one view file for `#{name}` found!" if files.length > 1

        files.first
      end

      # @return [Symbol]
      def view_type
        return @method_view.type if @method_view
        raise ViewFileNotFoundError, "No view file for #{self}" unless view_file_name

        view_file_path = ::Pathname.new view_file_name
        view_file_path.extname.delete_prefix('.').to_sym
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amber_component-1.2.0 lib/amber_component/views.rb