Class: RBlade::CompilesStatements::CompilesComponentHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/rblade/compiler/statements/compiles_component_helpers.rb

Instance Method Summary collapse

Instance Method Details

#compileProps(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rblade/compiler/statements/compiles_component_helpers.rb', line 14

def compileProps args
  if args&.count != 1
    raise StandardError.new "Props statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end

  props = extractProps args[0]
  props.map do |key, value|
    compiled_code = ""
    if value == "_required"
      compiled_code << "if !attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;"
      compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'];attributes.delete :'#{RBlade.escape_quotes(key)}';"
    elsif isValidVariableName key
      compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'].nil? ? #{value} : attributes[:'#{RBlade.escape_quotes(key)}'];"
      compiled_code << "attributes.delete :'#{RBlade.escape_quotes(key)}';"
    else
      compiled_code << "attributes.default(:'#{RBlade.escape_quotes(key)}', #{value});"
    end

    compiled_code
  end.join
end

#compileShouldRender(args) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/rblade/compiler/statements/compiles_component_helpers.rb', line 6

def compileShouldRender args
  if args&.count != 1
    raise StandardError.new "Should render statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end

  "unless(#{args[0]});return'';end;"
end