Module: SyntaxTree::HashFormatter
- Defined in:
- lib/syntax_tree.rb
Overview
This module is responsible for formatting the assocs contained within a hash or bare hash. It first determines if every key in the hash can use labels. If it can, it uses labels. Otherwise it uses hash rockets.
Defined Under Namespace
Classes: Base, Labels, Rockets
Class Method Summary collapse
Class Method Details
.for(container) ⇒ Object
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 |
# File 'lib/syntax_tree.rb', line 2153 def self.for(container) labels = container.assocs.all? do |assoc| next true if assoc.is_a?(AssocSplat) case assoc.key when Label true when SymbolLiteral # When attempting to convert a hash rocket into a hash label, # you need to take care because only certain patterns are # allowed. Ruby source says that they have to match keyword # arguments to methods, but don't specify what that is. After # some experimentation, it looks like it's: value = assoc.key.value.value value.match?(/^[_A-Za-z]/) && !value.end_with?("=") when DynaSymbol true else false end end (labels ? Labels : Rockets).new(container) end |