Class: Erector::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/erector/tag.rb

Overview

Defines a type of tag (not an actual element with attributes and contents)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *params) ⇒ Tag

Pass the self_closing and inline params as symbols, e.g.

Tag.new(“i”, :inline) Tag.new(“input”, :inline, :self_closing)

Parameters:

  • name

    the name of the tag, e.g. “div”

  • self_closing

    whether it can (false) or cannot (true) contain text or other elements. Default: false

  • inline

    whether it should appear in line with other elements (true) or on a line by itself (false) in pretty mode. Default: false

  • snake

    whether to covert the method name into “snake case” (aka underscorized). Default: false



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/erector/tag.rb', line 16

def initialize(name, *params)
  @name = name.to_s
  @method_name = if params.first.is_a? String
    params.shift
  else
    @name
  end
  @self_closing = params.include?(:self_closing)
  @inline = params.include?(:inline)
  @method_name = snake_case(@method_name) if params.include?(:snake_case)
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



28
29
30
# File 'lib/erector/tag.rb', line 28

def method_name
  @method_name
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/erector/tag.rb', line 28

def name
  @name
end

Instance Method Details

#inline?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/erector/tag.rb', line 38

def inline?
  @inline
end

#newliney?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/erector/tag.rb', line 34

def newliney?
  !@inline
end

#self_closing?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/erector/tag.rb', line 30

def self_closing?
  @self_closing
end

#snake_case(s) ⇒ String

Convert to snake case.

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

borrowed from github.com/datamapper/extlib/blob/master/lib/extlib/string.rb

Returns:

  • (String)

    Receiver converted to snake case.



53
54
55
56
57
58
59
60
61
# File 'lib/erector/tag.rb', line 53

def snake_case(s)
  if s.match(/\A[A-Z]+\z/)
    s.downcase
  else
    s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
     gsub(/([a-z])([A-Z])/, '\1_\2').
     downcase
  end
end