Class: ExpressTemplates::Markup::Tag

Inherits:
Object
  • Object
show all
Includes:
ExpressTemplates::Macro
Defined in:
lib/express_templates/markup/tag.rb

Direct Known Subclasses

HtmlTag

Constant Summary collapse

INDENT =
'  '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExpressTemplates::Macro

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &children) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/express_templates/markup/tag.rb', line 47

def method_missing(name, *args, &children)
  add_css_class(name)
  _process(*args) unless args.empty?
  if children # in the case where CSS classes are specified via method
    unless @expander.nil?
      @expander.process_children! self, &children
    else
      raise "block passed without expander"
    end
  end
  return self
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/express_templates/markup/tag.rb', line 6

def children
  @children
end

Class Method Details

.formattedObject



104
105
106
107
108
109
110
111
112
# File 'lib/express_templates/markup/tag.rb', line 104

def self.formatted
  old_setting = Thread.current[:formatted]
  begin
    Thread.current[:formatted] = true
    yield if block_given?
  ensure
    Thread.current[:formatted] = old_setting
  end
end

.macro_nameObject

These come from Macro but must remain overridden here for performance reasons. To verify, comment these two methods and run rake test and observe the performance hit. Why?



13
14
15
# File 'lib/express_templates/markup/tag.rb', line 13

def self.macro_name
  @macro_name ||= to_s.split('::').last.underscore
end

Instance Method Details

#add_css_class(css_class) ⇒ Object



41
42
43
44
45
# File 'lib/express_templates/markup/tag.rb', line 41

def add_css_class(css_class)
  @options['class'] ||= ''
  css_class = css_class.to_s.gsub('_', '-').gsub(/^-/,'') if css_class.to_s.match /^_.*_/
  @options['class'] = (@options['class'].split + [css_class]).uniq.join(" ")
end

#close_tagObject



37
38
39
# File 'lib/express_templates/markup/tag.rb', line 37

def close_tag
  "</#{macro_name}>"
end

#compileObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/express_templates/markup/tag.rb', line 68

def compile
  ExpressTemplates::Indenter.for(:markup) do |whitespace|
    ruby_fragments = @children.map do |child|
      if child.respond_to?(:compile)
        child.compile
      else
        %Q("#{child}")
      end
    end
    unless ruby_fragments.empty?
      _wrap_with_tags(ruby_fragments, whitespace)
    else
      if should_not_abbreviate?
        _wrap_with_tags(ruby_fragments, whitespace)
      elsif transform_close_tag?
        %Q("#{start_tag.gsub(/>$/, ' />')}")
      else
        %Q("#{start_tag}")
      end
    end
  end
end

#html_optionsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/express_templates/markup/tag.rb', line 20

def html_options
  @options.each_pair.map do |name, value|
    case
    when name.to_sym.eql?(:data) && value.kind_of?(Hash)
      value.each_pair.map {|k,v| %Q(data-#{k}=\\"#{v}\\") }.join(" ")
    when value.kind_of?(Proc)
      %Q(#{name}=\\"\#{(#{value.source}).call}\\")
    else
      %Q(#{name}=\\"#{value}\\")
    end
  end.join(" ")
end

#macro_nameObject



17
# File 'lib/express_templates/markup/tag.rb', line 17

def macro_name ; self.class.macro_name end

#should_not_abbreviate?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/express_templates/markup/tag.rb', line 60

def should_not_abbreviate?
  false
end

#start_tagObject



33
34
35
# File 'lib/express_templates/markup/tag.rb', line 33

def start_tag
  "<#{macro_name}#{html_options.empty? ? '' : ' '+html_options}>"
end

#to_templateObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/express_templates/markup/tag.rb', line 91

def to_template
  # ExpressTemplates::Indenter.for(:template) do
    template_fragments = @children.map do |child|
      if child.respond_to?(:to_template)
        child.to_template
      else
        child
      end
    end
    macro_name + _blockify(template_fragments.join("\n"))
  # end
end

#transform_close_tag?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/express_templates/markup/tag.rb', line 64

def transform_close_tag?
  true
end