Class: Esse::Jbuilder::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/esse/jbuilder/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_filename = nil, **assigns) ⇒ Template

Returns a new instance of Template.



29
30
31
32
# File 'lib/esse/jbuilder/template.rb', line 29

def initialize(view_filename = nil, **assigns)
  @view_filename = view_filename
  @assigns = assigns
end

Instance Attribute Details

#assignsObject (readonly)

Returns the value of attribute assigns.



23
24
25
# File 'lib/esse/jbuilder/template.rb', line 23

def assigns
  @assigns
end

#view_filenameObject (readonly)

Returns the value of attribute view_filename.



23
24
25
# File 'lib/esse/jbuilder/template.rb', line 23

def view_filename
  @view_filename
end

Class Method Details

.call(view_filename = nil, **assigns, &block) ⇒ Object



25
26
27
# File 'lib/esse/jbuilder/template.rb', line 25

def self.call(view_filename = nil, **assigns, &block)
  new(view_filename, **assigns).to_hash(&block)
end

Instance Method Details

#to_hash(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/esse/jbuilder/template.rb', line 34

def to_hash(&block)
  func = block || begin
    raise ArgumentError, "Missing view or block" unless view_filename || block
    fragments = view_filename.split("/")
    filename = fragments.pop
    filename = "#{filename}.json.jbuilder" unless filename.end_with?(".json.jbuilder")
    rel_path = fragments.join("/")

    ->(json) {
      json.instance_eval(File.read(Esse.config.search_view_path.join("#{rel_path}/#{filename}").to_s), view_filename, 1)
    }
  end

  WithAssigns.new(assigns: assigns, &func).attributes!
end