Module: AmberComponent::Views::ClassMethods
- Included in:
- Base
- Defined in:
- lib/amber_component/views.rb
Overview
Class methods for views.
Instance Attribute Summary collapse
-
#method_view ⇒ TypedContent
readonly
ERB/Haml/Slim view registered through the ‘view` method.
Instance Method Summary collapse
-
#view(content, type: :erb) ⇒ void
Register an inline view by returning a String from the passed block.
- #view_file_name ⇒ String?
- #view_path ⇒ String?
- #view_template_source ⇒ String
- #view_type ⇒ Symbol
Instance Attribute Details
#method_view ⇒ TypedContent (readonly)
ERB/Haml/Slim view registered through the ‘view` method.
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
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_name ⇒ String?
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_path ⇒ String?
51 52 53 |
# File 'lib/amber_component/views.rb', line 51 def view_path asset_path view_file_name end |
#view_template_source ⇒ 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_type ⇒ Symbol
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 |