lib/faml/static_hash_parser.rb in faml-0.6.0 vs lib/faml/static_hash_parser.rb in faml-0.6.1
- old
+ new
@@ -1,5 +1,6 @@
+# frozen-string-literal: true
require 'parser/current'
module Faml
class StaticHashParser
FAILURE_TAG = :failure
@@ -63,36 +64,39 @@
when :sym, :int, :float, :str, :rational, :complex
node.children[0]
end
end
- def try_static_value(key_static, node)
+ def try_static_value(key_static, node, force_static: false)
case node.type
when :sym, :int, :float, :str, :rational, :complex
- @static_attributes[key_static] = node.children[0]
+ set_static_attribute(key_static, node.children[0])
when :true
- @static_attributes[key_static] = true
+ set_static_attribute(key_static, true)
when :false
- @static_attributes[key_static] = false
+ set_static_attribute(key_static, false)
when :nil
- @static_attributes[key_static] = nil
+ set_static_attribute(key_static, nil)
when :dstr
- @dynamic_attributes[key_static] = node.location.expression.source
+ if force_static
+ throw FAILURE_TAG
+ end
+ set_dynamic_attributes(key_static, node.location.expression.source)
when :send
- if SPECIAL_ATTRIBUTES.include?(key_static.to_s)
+ if force_static
throw FAILURE_TAG
- else
- @dynamic_attributes[key_static] = node.location.expression.source
end
+ set_dynamic_attributes(key_static, node.location.expression.source)
when :hash
try_static_hash_value(key_static, node)
when :array
try_static_array_value(key_static, node)
else
throw FAILURE_TAG
end
end
+ protected :try_static_value
def try_static_hash_value(key_static, node)
parser = self.class.new
if parser.walk(node)
merge_attributes(key_static, parser)
@@ -102,11 +106,11 @@
end
end
def merge_attributes(key_static, parser)
unless parser.static_attributes.empty?
- @static_attributes[key_static] = parser.static_attributes
+ set_static_attribute(key_static, parser.static_attributes)
end
unless parser.dynamic_attributes.empty?
expr = parser.dynamic_attributes.map do |k, v|
"#{k.inspect} => #{v}"
@@ -114,12 +118,32 @@
@dynamic_attributes[key_static] = "{#{expr}}"
end
end
def try_static_array_value(key_static, node)
- arr = node.children.map do |child|
- try_static_value(key_static, child)
+ parser = self.class.new
+ arr = node.children.map.with_index do |child, i|
+ # TODO: Support dynamic_attributes?
+ parser.try_static_value(i, child, force_static: true)
end
- @static_attributes[key_static] = arr
+ set_static_attribute(key_static, arr)
+ end
+
+ def set_static_attribute(key, val)
+ case key.to_s
+ when 'id', 'class'
+ @static_attributes[key] ||= []
+ @static_attributes[key].concat(Array(val))
+ else
+ @static_attributes[key] = val
+ end
+ val
+ end
+
+ def set_dynamic_attributes(key, val)
+ if SPECIAL_ATTRIBUTES.include?(key.to_s)
+ throw FAILURE_TAG
+ end
+ @dynamic_attributes[key] = val
end
end
end