Module: HamlUserTags::Helpers

Included in:
Haml::Helpers
Defined in:
lib/haml_user_tags/helpers.rb,
lib/haml_user_tags/rails/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributes_hash(class_id, obj_ref, *attributes_hashes) ⇒ Object

Same as Haml::Buffer.attributes, but returns the hash instead of writing the attributes to the buffer.



7
8
9
10
11
12
13
14
# File 'lib/haml_user_tags/helpers.rb', line 7

def self.attributes_hash(class_id, obj_ref, *attributes_hashes)
  attributes = class_id
  attributes_hashes.each do |old|
    Haml::Buffer.merge_attrs(attributes, Hash[old.map {|k, v| [k.to_s, v]}])
  end
  Haml::Buffer.merge_attrs(attributes, Haml::Buffer.new.parse_object_ref(obj_ref)) if obj_ref
  attributes
end

.attributes_hash_with_indifference(*args) ⇒ Object



15
16
17
# File 'lib/haml_user_tags/rails/helpers.rb', line 15

def attributes_hash_with_indifference *args
  ActiveSupport::HashWithIndifferentAccess.new attributes_hash_without_indifference *args
end

Instance Method Details

#define_tag(name, &tag) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/haml_user_tags/helpers.rb', line 16

def define_tag name, &tag
  unless name =~ HamlUserTags::TAG_NAME_REGEX
    raise "define_tag: #{name.inspect} is not a valid user tag name. It must match #{HamlUserTags::TAG_NAME_REGEX}"
  end

  func = proc do |attributes = {}, &contents|
    @haml_buffer ||= Haml::Buffer.new(nil, Haml::Options.defaults)
    tag.binding.eval("proc { |v| _hamlout = v }").call @haml_buffer
    # Use a proxy String class that will only evaluate its contents once
    # it is referenced. This make the behavior similar to how "yield"
    # would be in a ruby helper.
    content_getter = LazyContents.new { capture_haml(&contents) || "" } if contents
    capture_haml { instance_exec attributes, content_getter, &tag }
  end

  define_singleton_method name, &func
  if self.is_a?(Module)
    define_method name, &func
  end
end

#include_tags(path) ⇒ Object

Override the base include_tags to take advantage of Rails’ template location features



6
7
8
9
10
# File 'lib/haml_user_tags/rails/helpers.rb', line 6

def include_tags path
  source = File.read path
  HamlUserTags::Engine.new(source, :filename => path).extend_object self
  nil
end

#include_tags_without_railsObject



3
4
5
6
7
# File 'lib/haml_user_tags/rails/helpers.rb', line 3

def include_tags path
  source = File.read path
  HamlUserTags::Engine.new(source, :filename => path).extend_object self
  nil
end