Module: AmberComponent::Views::ClassMethods

Included in:
Base
Defined in:
lib/amber_component/views.rb

Overview

Class methods for views.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#method_viewTypedContent (readonly)

ERB/Haml/Slim view registered through the ‘view` method.

Returns:



41
42
43
# File 'lib/amber_component/views.rb', line 41

def method_view
  @method_view
end

Instance Method Details

#view(content, type: :erb) ⇒ void

This method returns an undefined value.

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

Parameters:

  • content (String, Proc)
  • type (Symbol) (defaults to: :erb)


34
35
36
# File 'lib/amber_component/views.rb', line 34

def view(content, type: :erb)
  @method_view = TypedContent.new(type: type, content: content)
end

#view_file_nameString?

Returns:

  • (String, nil)

Raises:



56
57
58
59
60
61
# File 'lib/amber_component/views.rb', line 56

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

#view_pathString?

Returns:

  • (String, nil)


51
52
53
# File 'lib/amber_component/views.rb', line 51

def view_path
  asset_path view_file_name
end

#view_template_sourceString

Returns:

  • (String)


44
45
46
47
48
# File 'lib/amber_component/views.rb', line 44

def view_template_source
  return @method_view.to_s if @method_view

  ::File.read(view_path)
end

#view_typeSymbol

Returns:

  • (Symbol)

Raises:

  • (ViewFileNotFoundError)


64
65
66
67
68
69
70
# File 'lib/amber_component/views.rb', line 64

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