Class: Haml::AttributeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/haml/attribute_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available?TrueClass

Deprecated.

Prism is a hard dependency, so this is always true. Haml itself no longer asks, and this will be removed in the future.

Returns - return true if AttributeParser.parse can be used.

Returns:

  • (TrueClass)
    • return true if AttributeParser.parse can be used.


9
10
11
# File 'lib/haml/attribute_parser.rb', line 9

def self.available?
  true
end

.parse(text)



13
14
15
# File 'lib/haml/attribute_parser.rb', line 13

def self.parse(text)
  self.new.parse(text)
end

Instance Method Details

#parse(text) ⇒ Hash?

Returns - keys and values are the attribute source as written, or nil if the text is not a Hash literal whose keys are all static.

Returns:

  • (Hash, nil)
    • keys and values are the attribute source as written, or nil if the text is not a Hash literal whose keys are all static.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/haml/attribute_parser.rb', line 19

def parse(text)
  exp = wrap_bracket(text)
  # A multi-line hash is left to the runtime, which keeps the [:newline] bookkeeping of
  # the compiled code correct. Compiling it statically is a separate optimization.
  return if exp.include?("\n")

  node = hash_node(exp)
  return if node.nil?

  hash = {}
  node.elements.each do |element|
    return unless element.is_a?(Prism::AssocNode)

    key = static_key(element.key)
    return if key.nil?

    hash[key] = value_source(element.value)
  end
  hash
end