Class: Mayu::Resources::Transformers::Haml::RubyBuilder
- Inherits:
-
Object
- Object
- Mayu::Resources::Transformers::Haml::RubyBuilder
- Includes:
- SyntaxTree::DSL
- Defined in:
- lib/mayu/resources/transformers/haml.rb
Instance Method Summary collapse
- #array(elems) ⇒ Object
- #assign_const(name, value) ⇒ Object
- #assocs(**kwargs) ⇒ Object
- #call_freeze(node) ⇒ Object
- #call_helpers(method, *args) ⇒ Object
- #compute(node) ⇒ Object
- #const_path(*names) ⇒ Object
- #create_program(setup, styles, render) ⇒ Object
- #create_render(statements) ⇒ Object
- #first_or_array(nodes) ⇒ Object
- #helper_ident ⇒ Object
-
#initialize(options) ⇒ RubyBuilder
constructor
A new instance of RubyBuilder.
- #mayu_const_path ⇒ Object
- #merge_props(attrs_to_merge) ⇒ Object
- #props_hash(attrs) ⇒ Object
- #ruby_script(statements) ⇒ Object
- #self_var_ref ⇒ Object
- #set_fiber_local(ident, value) ⇒ Object
- #setup_component(styles) ⇒ Object
- #silent(node) ⇒ Object
- #slot(name = nil, fallback: nil) ⇒ Object
- #splat_hash(node) ⇒ Object
- #split_string_literal(string_literal) ⇒ Object
- #string_literal(value) ⇒ Object
- #sym(str) ⇒ Object
- #tag(name, children, attrs_to_merge) ⇒ Object
- #tag_name_or_class(name) ⇒ Object
- #try_split_string_literal(node) ⇒ Object
- #wrap_args(args) ⇒ Object
Constructor Details
#initialize(options) ⇒ RubyBuilder
Returns a new instance of RubyBuilder.
113 114 115 |
# File 'lib/mayu/resources/transformers/haml.rb', line 113 def initialize() @options = end |
Instance Method Details
#array(elems) ⇒ Object
170 171 172 |
# File 'lib/mayu/resources/transformers/haml.rb', line 170 def array(elems) ArrayLiteral(LBracket("["), Args(elems)) end |
#assign_const(name, value) ⇒ Object
117 |
# File 'lib/mayu/resources/transformers/haml.rb', line 117 def assign_const(name, value) = Assign(VarField(Const(name)), value) |
#assocs(**kwargs) ⇒ Object
166 167 168 |
# File 'lib/mayu/resources/transformers/haml.rb', line 166 def assocs(**kwargs) kwargs.map { |key, value| Assoc(Label("#{key}:"), value) } end |
#call_freeze(node) ⇒ Object
407 408 |
# File 'lib/mayu/resources/transformers/haml.rb', line 407 def call_freeze(node) = CallNode(node, Period("."), Ident("freeze"), nil) |
#call_helpers(method, *args) ⇒ Object
384 385 386 387 388 389 390 391 |
# File 'lib/mayu/resources/transformers/haml.rb', line 384 def call_helpers(method, *args) CallNode( Ident("mayu"), Period("."), Ident(method.to_s), wrap_args(args.flatten.compact) ) end |
#compute(node) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/mayu/resources/transformers/haml.rb', line 367 def compute(node) # MethodAddBlock( # call_helpers(:compute), # BlockNode( # Kw("do"), # nil, # BodyStmt(Statements([node]), nil, nil, nil, nil) # ) # ) node end |
#const_path(*names) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mayu/resources/transformers/haml.rb', line 133 def const_path(*names) names.reduce(nil) do |parent, name| const = Const(name) if T.cast(parent, T.untyped) ConstPathRef(parent, const) else TopConstRef(const) end end end |
#create_program(setup, styles, render) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mayu/resources/transformers/haml.rb', line 120 def create_program(setup, styles, render) Program( Statements( [ Comment("# frozen_string_literal: true", false), assign_const("Self", setup_component(styles)), *setup, create_render(render) ] ) ).accept(StateAndPropsTransformer.new.visitor) end |
#create_render(statements) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/mayu/resources/transformers/haml.rb', line 174 def create_render(statements) Command( Ident("public"), Args( [ DefNode( nil, nil, Ident("render"), nil, BodyStmt( Statements( [ # set_fiber_local("current_component", VarRef(Kw("self"))), *statements ] ), nil, nil, nil, nil ) ) ] ), nil ) end |
#first_or_array(nodes) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/mayu/resources/transformers/haml.rb', line 266 def first_or_array(nodes) case nodes in [node] node else ArrayLiteral(LBracket("["), Args(nodes)) end end |
#helper_ident ⇒ Object
393 394 395 396 397 398 399 |
# File 'lib/mayu/resources/transformers/haml.rb', line 393 def helper_ident if @options.enable_new_helper_ident CallNode(VarRef(Kw("self")), Period("."), Ident("Mayu"), nil) else Ident("mayu") end end |
#mayu_const_path ⇒ Object
379 380 381 382 |
# File 'lib/mayu/resources/transformers/haml.rb', line 379 def mayu_const_path ConstPathRef(VarRef(Const("Mayu")), Const("VDOM")) # Const("Mayu") end |
#merge_props(attrs_to_merge) ⇒ Object
260 261 262 263 264 |
# File 'lib/mayu/resources/transformers/haml.rb', line 260 def merge_props(attrs_to_merge) return if attrs_to_merge.empty? splat_hash(call_helpers(:merge_props, attrs_to_merge)) end |
#props_hash(attrs) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/mayu/resources/transformers/haml.rb', line 283 def props_hash(attrs) HashLiteral( LBrace("{"), attrs.map do |key, value| if key.to_s == "class" && value in String Assoc( SymbolLiteral(Ident(key.to_s)), first_or_array(value.split.map { sym(_1) }) ) else Assoc( sym(key.to_s), case value in Symbol SymbolLiteral(Ident(value.to_s)) in String StringLiteral([TStringContent(value.to_s)], :'"') in SyntaxTree::ArrayLiteral value in TrueClass | FalseClass | NilClass VarRef(Kw(value.to_s)) end ) end end ) end |
#ruby_script(statements) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/mayu/resources/transformers/haml.rb', line 337 def ruby_script(statements) case statements in [] nil in [SyntaxTree::StringLiteral => string_literal] split_string_literal(string_literal) in [statement] statement else Begin(BodyStmt(Statements(statements), nil, nil, nil, nil)) end end |
#self_var_ref ⇒ Object
118 |
# File 'lib/mayu/resources/transformers/haml.rb', line 118 def self_var_ref = VarRef(Kw("self")) |
#set_fiber_local(ident, value) ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/mayu/resources/transformers/haml.rb', line 203 def set_fiber_local(ident, value) Assign( ARefField( VarRef(Const("Fiber")), Args([SymbolLiteral(Ident(ident))]) ), value ) end |
#setup_component(styles) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mayu/resources/transformers/haml.rb', line 145 def setup_component(styles) CallNode( nil, nil, Ident("setup_component"), ArgParen( Args( [ BareAssocHash( assocs( assets: array(styles.map { string_literal(_1.filename) }), styles: props_hash(CSS.merge_classnames(styles)) ) ) ] ) ) ) end |
#silent(node) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/mayu/resources/transformers/haml.rb', line 350 def silent(node) case node in SyntaxTree::ReturnNode node else Begin( BodyStmt( Statements([node, VarRef(Kw("nil"))]), nil, nil, nil, nil ) ) end end |
#slot(name = nil, fallback: nil) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/mayu/resources/transformers/haml.rb', line 213 def slot(name = nil, fallback: nil) if fallback in [_, *] return( MethodAddBlock( slot(name, fallback: nil), BlockNode( Kw("do"), nil, BodyStmt(Statements(Array(fallback)), nil, nil, nil, nil) ) ) ) end # call_helpers(:slot, [Ident("children"), name].compact) call_helpers(:slot, [name].compact) end |
#splat_hash(node) ⇒ Object
256 257 258 |
# File 'lib/mayu/resources/transformers/haml.rb', line 256 def splat_hash(node) BareAssocHash([AssocSplat(node)]) end |
#split_string_literal(string_literal) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/mayu/resources/transformers/haml.rb', line 322 def split_string_literal(string_literal) string_literal # string_literal # .parts # .map do |part| # case part # in SyntaxTree::TStringContent # string_literal(part.value) # in SyntaxTree::StringEmbExpr # part.statements # end # end # .flatten end |
#string_literal(value) ⇒ Object
405 406 |
# File 'lib/mayu/resources/transformers/haml.rb', line 405 def string_literal(value) = StringLiteral([TStringContent(value.to_s)], '"') |
#sym(str) ⇒ Object
275 276 277 278 279 280 281 |
# File 'lib/mayu/resources/transformers/haml.rb', line 275 def sym(str) if str.match(/\A[\w_]+\z/) SymbolLiteral(Ident(str)) else DynaSymbol([TStringContent(str)], '"') end end |
#tag(name, children, attrs_to_merge) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/mayu/resources/transformers/haml.rb', line 231 def tag(name, children, attrs_to_merge) ARef( ConstPathRef( ConstPathRef(VarRef(Const("Mayu")), Const("VDOM")), Const("H") ), Args( [ tag_name_or_class(name), *children, merge_props(attrs_to_merge) ].flatten.compact ) ) end |
#tag_name_or_class(name) ⇒ Object
247 248 249 250 251 252 253 254 |
# File 'lib/mayu/resources/transformers/haml.rb', line 247 def tag_name_or_class(name) case name in /\A[A-Z]/ Ident(name) else SymbolLiteral(Ident(name)) end end |
#try_split_string_literal(node) ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/mayu/resources/transformers/haml.rb', line 311 def try_split_string_literal(node) case node in SyntaxTree::StringLiteral split_string_literal(node) in [SyntaxTree::StringLiteral => node] split_string_literal(node) else node end end |
#wrap_args(args) ⇒ Object
401 402 403 |
# File 'lib/mayu/resources/transformers/haml.rb', line 401 def wrap_args(args) args.empty? ? nil : ArgParen(Args(args)) end |