module Noraneko::NodeUtility
Public Instance Methods
convert_to_hash(node)
click to toggle source
# File lib/noraneko/node_utility.rb, line 19 def convert_to_hash(node) raise 'This is not hash expression' unless node.type == :hash node.children.each_with_object({}) do |pair, hash| key, value = pair.children if convertable?(key) && convertable?(value) hash[convert!(key)] = convert!(value) end end end
extract_consts(const_node, consts = [])
click to toggle source
# File lib/noraneko/node_utility.rb, line 5 def extract_consts(const_node, consts = []) next_const_node, const_sym = const_node.children consts.unshift(const_sym) if next_const_node extract_consts(next_const_node, consts) else consts end end
extract_syms(nodes)
click to toggle source
# File lib/noraneko/node_utility.rb, line 15 def extract_syms(nodes) nodes.map { |n| n.children.last } end
singleton_class?(node)
click to toggle source
# File lib/noraneko/node_utility.rb, line 29 def singleton_class?(node) node&.children&.first&.type == :self end
Private Instance Methods
convert!(node)
click to toggle source
# File lib/noraneko/node_utility.rb, line 39 def convert!(node) node.children.last end
convertable?(node)
click to toggle source
# File lib/noraneko/node_utility.rb, line 35 def convertable?(node) %i[sym str].include?(node.type) ? true : false end