Module: Card::Set::Format::HamlViews
- Included in:
- AbstractFormat
- Defined in:
- lib/card/set/format/haml_views.rb
Overview
Instance Method Summary collapse
- #each_template_path(source) ⇒ Object
- #haml_block_locals(view_args, &block) ⇒ Object
- #haml_template_path(view = nil, source = nil) ⇒ Object
- #haml_template_proc(template, path, &block) ⇒ Object
- #haml_to_html(haml, locals = {}, a_binding = nil, debug_info = {}) ⇒ Object
- #haml_view_block(view, &block) ⇒ Object
- #template_location(debug_info) ⇒ Object
- #try_haml_template_path(template_path, view, source_dir, ext = "haml") ⇒ Object
- #with_template_path(path) ⇒ Object
Instance Method Details
#each_template_path(source) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/card/set/format/haml_views.rb', line 57 def each_template_path source source ||= source_location basename = ::File.basename source, ".rb" source_dir = ::File.dirname source ["./#{basename}", "."].each do |template_dir| yield template_dir, source_dir end end |
#haml_block_locals(view_args, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/card/set/format/haml_views.rb', line 40 def haml_block_locals view_args, &block instance_exec view_args, &block instance_variables.each_with_object({}) do |var, h| h[var.to_s.tr("@", "").to_sym] = instance_variable_get var end end |
#haml_template_path(view = nil, source = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/card/set/format/haml_views.rb', line 47 def haml_template_path view=nil, source=nil each_template_path(source) do |template_dir, source_dir| path = try_haml_template_path template_dir, view, source_dir return path if path end msg = "can't find haml template" msg += " for #{view}" if view.present? raise Card::Error, msg end |
#haml_template_proc(template, path, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/card/set/format/haml_views.rb', line 30 def haml_template_proc template, path, &block block_locals = block_given? proc do |view_args| with_template_path path do locals = block_locals ? haml_block_locals(view_args, &block) : view_args haml_to_html template, locals, nil, path: path end end end |
#haml_to_html(haml, locals = {}, a_binding = nil, debug_info = {}) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/card/set/format/haml_views.rb', line 74 def haml_to_html haml, locals={}, a_binding=nil, debug_info={} a_binding ||= binding ::Haml::Engine.new(haml).render a_binding, locals || {} rescue Haml::SyntaxError => e raise Card::Error, "haml syntax error #{template_location(debug_info)}: #{e.}" end |
#haml_view_block(view, &block) ⇒ Object
25 26 27 28 |
# File 'lib/card/set/format/haml_views.rb', line 25 def haml_view_block view, &block path = haml_template_path view haml_template_proc ::File.read(path), path, &block end |
#template_location(debug_info) ⇒ Object
82 83 84 85 86 |
# File 'lib/card/set/format/haml_views.rb', line 82 def template_location debug_info return "" unless debug_info[:path] Pathname.new(debug_info[:path]) .relative_path_from(Pathname.new(Dir.pwd)) end |
#try_haml_template_path(template_path, view, source_dir, ext = "haml") ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/card/set/format/haml_views.rb', line 66 def try_haml_template_path template_path, view, source_dir, ext="haml" template_path = File.join(template_path, view.to_s) if view.present? template_path += ".#{ext}" path = ::File.(template_path, source_dir) .sub(%r{(/mod/[^/]+)/set/}, "\\1/#{TEMPLATE_DIR}/") ::File.exist?(path) && path end |
#with_template_path(path) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/card/set/format/haml_views.rb', line 88 def with_template_path path old_path = @template_path @template_path = path yield ensure @template_path = old_path end |